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 only timezones for datetime parser #108

Conversation

renaudjester
Copy link
Collaborator

Fix CMT-64

@renaudjester renaudjester changed the base branch from main to copernicusmarine-toolbox-v2 August 6, 2024 13:04
@renaudjester renaudjester requested a review from uriii3 August 6, 2024 13:04
Comment on lines 125 to 148
def datetime_parser(string: str) -> datetime:
if string == "now":
return datetime.now()
for format in DATETIME_SUPPORTED_FORMATS:
try:
return datetime.strptime(string, format)
except ValueError:
pass
return datetime.now(tz=timezone.utc).replace(tzinfo=None)
try:
return (
datetime.fromisoformat(string)
.astimezone(timezone.utc)
.replace(tzinfo=None)
)
except ValueError:
for format in DATETIME_SUPPORTED_FORMATS:
try:
return datetime.strptime(string, format)
except ValueError:
pass
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this basically ensures that all strings are setted with a timezone to UTC. But do we know if all the 'dates' are being held first as strings?
Should we check that all dates are set with a certain timezone?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# now
>>> datetime_parser("now")
datetime.datetime(2024, 8, 8, 7, 58, 37, 248673)

# with timezone in the name
>>> datetime_parser("2024-06-18T13:00:00.000+02:00")
datetime.datetime(2024, 6, 18, 11, 0)

# full date in the timezone of the computer
>>> datetime_parser("2024-06-18T13:00:00.000")
datetime.datetime(2024, 6, 18, 11, 0)

# full date UTC
>>> datetime_parser("2024-06-18T13:00:00.000Z")
datetime.datetime(2024, 6, 18, 13, 0)

# year month day WRONG
>>> datetime_parser("2024-06-18")
datetime.datetime(2024, 6, 17, 22, 0)

So yeah I will look a bit more into it

@renaudjester renaudjester requested a review from uriii3 August 8, 2024 10:35
@uriii3
Copy link
Collaborator

uriii3 commented Aug 19, 2024

Then, for any date that is being played with, should be passed within this function, no? Should we review the code or it0s already done and we keep in mind for the following implementations?

@renaudjester renaudjester force-pushed the use-only-timezones-for-datetime-parser branch from bf04505 to 3d27ae3 Compare August 19, 2024 14:32
@renaudjester renaudjester merged commit a1b773f into copernicusmarine-toolbox-v2 Aug 19, 2024
2 checks passed
@renaudjester renaudjester deleted the use-only-timezones-for-datetime-parser branch August 19, 2024 16:04
renaudjester added a commit that referenced this pull request Aug 20, 2024
take into account the timezone when parsing datetime and getting the time from the computer
renaudjester added a commit that referenced this pull request Aug 20, 2024
take into account the timezone when parsing datetime and getting the time from the computer
renaudjester added a commit that referenced this pull request Aug 29, 2024
take into account the timezone when parsing datetime and getting the time from the computer
renaudjester added a commit that referenced this pull request Oct 28, 2024
take into account the timezone when parsing datetime and getting the time from the computer
renaudjester added a commit that referenced this pull request Oct 28, 2024
take into account the timezone when parsing datetime and getting the time from the computer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants