Skip to content

Commit

Permalink
Use only timezones for datetime parser (#108)
Browse files Browse the repository at this point in the history
take into account the timezone when parsing datetime and getting the time from the computer
  • Loading branch information
renaudjester committed Aug 20, 2024
1 parent 7c792c6 commit 9466020
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
1 change: 1 addition & 0 deletions conda_environment_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ dependencies:
- syrupy==4.6.1
- pip:
- pytest-order==1.2.1
- freezegun==1.5.1
2 changes: 1 addition & 1 deletion copernicusmarine/catalogue_parser/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def sort_parts(self) -> tuple[Optional[str], Optional[str]]:
for part in self.parts
if part.retired_date
}
max_retired_timestamp = 0
max_retired_timestamp = 0.0
if will_be_retired_parts:
max_retired_timestamp = max(will_be_retired_parts.values()) + 1
self.parts = sorted(
Expand Down
32 changes: 24 additions & 8 deletions copernicusmarine/core_functions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import pathlib
import re
from datetime import datetime
from datetime import datetime, timezone
from importlib.metadata import version
from typing import (
Any,
Expand Down Expand Up @@ -43,6 +43,13 @@
"%Y-%m-%d",
"%Y-%m-%dT%H:%M:%S",
"%Y-%m-%d %H:%M:%S",
"%Y-%m-%dT%H:%M:%S.%f",
"%Y-%m-%dT%H:%M:%S.%fZ",
"%Y-%m-%d %H:%M:%S.%f%Z",
]

DATETIME_NON_ISO_FORMATS = [
"%Y",
"%Y-%m-%dT%H:%M:%S.%fZ",
]

Expand Down Expand Up @@ -122,14 +129,23 @@ class WrongDatetimeFormat(Exception):
...


def datetime_parser(string: str):
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:
parsed_datetime = datetime.fromisoformat(string)
if parsed_datetime.tzinfo is None:
return parsed_datetime
else:
return parsed_datetime.astimezone(timezone.utc).replace(
tzinfo=None
)
except ValueError:
for datetime_format in DATETIME_NON_ISO_FORMATS:
try:
return datetime.strptime(string, datetime_format)
except ValueError:
pass
raise WrongDatetimeFormat(string)


Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/test_help_command_interface.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,12 @@
' z-axis) as it is in the dataset originally',
' produced, named `depth` with descending',
' positive values. [default: True]',
' -t, --start-datetime [%Y|%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S|%Y-%m-%dT%H:%M:%S.%fZ]',
' -t, --start-datetime [%Y|%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S|%Y-%m-%dT%H:%M:%S.%f|%Y-%m-%dT%H:%M:%S.%fZ|%Y-%m-%d %H:%M:%S.%f%Z]',
' The start datetime of the temporal subset.',
' Caution: encapsulate date with " " to ensure',
' valid expression for format "%Y-%m-%d',
' %H:%M:%S".',
' -T, --end-datetime [%Y|%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S|%Y-%m-%dT%H:%M:%S.%fZ]',
' -T, --end-datetime [%Y|%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S|%Y-%m-%dT%H:%M:%S.%f|%Y-%m-%dT%H:%M:%S.%fZ|%Y-%m-%d %H:%M:%S.%f%Z]',
' The end datetime of the temporal subset.',
' Caution: encapsulate date with " " to ensure',
' valid expression for format "%Y-%m-%d',
Expand Down
31 changes: 31 additions & 0 deletions tests/test_utility_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from datetime import datetime

from freezegun import freeze_time

from copernicusmarine.core_functions.utils import datetime_parser


class TestUtilityFunctions:
@freeze_time("2012-01-14 03:21:34", tz_offset=-2)
def test_datetime_parser(self):
# all parsed dates are in UTC
assert datetime_parser("now") == datetime(2012, 1, 14, 1, 21, 34)
assert datetime_parser("2012-01-14T03:21:34.000000+02:00") == datetime(
2012, 1, 14, 1, 21, 34
)

# All format are supported
assert datetime_parser("2012") == datetime(2012, 1, 1, 0, 0, 0)
assert datetime_parser("2012-01-14") == datetime(2012, 1, 14, 0, 0, 0)
assert datetime_parser("2012-01-14T03:21:34") == datetime(
2012, 1, 14, 3, 21, 34
)
assert datetime_parser("2012-01-14 03:21:34") == datetime(
2012, 1, 14, 3, 21, 34
)
assert datetime_parser("2012-01-14T03:21:34.000000") == datetime(
2012, 1, 14, 3, 21, 34
)
assert datetime_parser("2012-01-14T03:21:34.000000Z") == datetime(
2012, 1, 14, 3, 21, 34
)

0 comments on commit 9466020

Please sign in to comment.