Skip to content

Commit

Permalink
fix: add tests for datetime parser
Browse files Browse the repository at this point in the history
  • Loading branch information
renaudjester committed Aug 8, 2024
1 parent 2697eff commit 7fa8519
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
1 change: 1 addition & 0 deletions conda_environment_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ dependencies:
- tox==4.11.4
- netcdf4==1.6.5
- syrupy==4.6.1
- freezegun==1.5.1
- pip:
- pytest-order==1.2.1
23 changes: 16 additions & 7 deletions copernicusmarine/core_functions/utils.py
Original file line number Diff line number Diff line change
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 @@ -126,15 +133,17 @@ def datetime_parser(string: str) -> datetime:
if string == "now":
return datetime.now(tz=timezone.utc).replace(tzinfo=None)
try:
return (
datetime.fromisoformat(string)
.astimezone(timezone.utc)
.replace(tzinfo=None)
)
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 format in DATETIME_SUPPORTED_FORMATS:
for datetime_format in DATETIME_NON_ISO_FORMATS:
try:
return datetime.strptime(string, format)
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 7fa8519

Please sign in to comment.