Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use better date/time validation #59

Open
sajith opened this issue Sep 19, 2022 · 0 comments
Open

Use better date/time validation #59

sajith opened this issue Sep 19, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@sajith
Copy link
Member

sajith commented Sep 19, 2022

We're using regular expressions to validate date and time strings:

ISO_FORMAT = r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[-+]\d{2}:\d{2}'
ISO_TIME_FORMAT = r"(^(?:[1-9]\d{3}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1\d|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[1-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00)-02-29)T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:Z|[+-][01]\d:[0-5]\d)$)"

if not match(ISO_TIME_FORMAT, time):

ISO_FORMAT = r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[-+]\d{2}:\d{2}'

elif not match(ISO_FORMAT, version):
errors.append(
'{} Version must be datetime ISO format'.format(
topology.id,
)
)
if not match(ISO_FORMAT, time_stamp):
errors.append(
'{} time_stamp needs to be in full ISO format'.format(
time_stamp,
)
)

But this is harder to understand, and they do not really check for invalid data (such as February 31). We probably should be using a real date/time parser. From this StackOverflow question, one way seems to be using datetime library:

from datetime import datetime

def datetime_valid(dt_str):
    try:
        datetime.fromisoformat(dt_str.replace('Z', '+00:00'))
    except:
        return False
    return True

However datetime's own documentation suggests using isoparse from third-party dateutil library.

@sajith sajith added the enhancement New feature or request label Sep 19, 2022
@sajith sajith self-assigned this Sep 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant