|
| 1 | +import re |
| 2 | + |
| 3 | +# this regex is based on the one from the rfc3339-validator package |
| 4 | +# credit to the original author |
| 5 | +# original license: |
| 6 | +# |
| 7 | +# MIT License |
| 8 | +# |
| 9 | +# Copyright (c) 2019, Nicolas Aimetti |
| 10 | +# |
| 11 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | +# of this software and associated documentation files (the "Software"), to deal |
| 13 | +# in the Software without restriction, including without limitation the rights |
| 14 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | +# copies of the Software, and to permit persons to whom the Software is |
| 16 | +# furnished to do so, subject to the following conditions: |
| 17 | +# |
| 18 | +# The above copyright notice and this permission notice shall be included in all |
| 19 | +# copies or substantial portions of the Software. |
| 20 | +# |
| 21 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | +# SOFTWARE. |
| 28 | +# |
| 29 | +# modifications have been made for additional corner cases and speed |
| 30 | +RFC3339_REGEX = re.compile( |
| 31 | + r""" |
| 32 | + ^ |
| 33 | + (?:\d{4}) |
| 34 | + - |
| 35 | + (?:0[1-9]|1[0-2]) |
| 36 | + - |
| 37 | + (?:\d{2}) |
| 38 | + (?:T|t) |
| 39 | + (?:[01]\d|2[0123]) |
| 40 | + : |
| 41 | + (?:[0-5]\d) |
| 42 | + : |
| 43 | + (?:[0-5]\d) |
| 44 | + # (optional) fractional seconds |
| 45 | + (?:(\.|,)\d+)? |
| 46 | + # UTC or offset |
| 47 | + (?: |
| 48 | + Z |
| 49 | + | z |
| 50 | + | [+-](?:[01]\d|2[0123]):[0-5]\d |
| 51 | + ) |
| 52 | + $ |
| 53 | +""", |
| 54 | + re.VERBOSE | re.ASCII, |
| 55 | +) |
| 56 | + |
| 57 | + |
| 58 | +def validate(date_str: object) -> bool: |
| 59 | + """Validate a string as a RFC3339 date-time.""" |
| 60 | + if not isinstance(date_str, str): |
| 61 | + return False |
| 62 | + if not RFC3339_REGEX.match(date_str): |
| 63 | + return False |
| 64 | + |
| 65 | + year, month, day = int(date_str[:4]), int(date_str[5:7]), int(date_str[8:10]) |
| 66 | + |
| 67 | + if month in {4, 6, 9, 11}: |
| 68 | + max_day = 30 |
| 69 | + elif month == 2: |
| 70 | + max_day = 29 if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) else 28 |
| 71 | + else: |
| 72 | + max_day = 31 |
| 73 | + if not 1 <= day <= max_day: |
| 74 | + return False |
| 75 | + return True |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + import timeit |
| 80 | + |
| 81 | + N = 100_000 |
| 82 | + tests = ( |
| 83 | + ("long_fracsec", "2018-12-31T23:59:59.8446519776713Z"), |
| 84 | + ("basic", "2018-12-31T23:59:59Z"), |
| 85 | + ("in_february", "2018-02-12T23:59:59Z"), |
| 86 | + ("in_february_invalid", "2018-02-29T23:59:59Z"), |
| 87 | + ) |
| 88 | + |
| 89 | + print("benchmarking") |
| 90 | + for name, val in tests: |
| 91 | + all_times = timeit.repeat( |
| 92 | + f"validate({val!r})", globals=globals(), repeat=3, number=N |
| 93 | + ) |
| 94 | + print(f"{name} (valid={validate(val)}): {int(min(all_times) / N * 10**9)}ns") |
0 commit comments