Skip to content

Commit

Permalink
Added pendulum dt objects to normalisation hander
Browse files Browse the repository at this point in the history
  • Loading branch information
keegansmith21 committed Jun 4, 2024
1 parent 7b06f0f commit 212445d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions observatory_platform/date_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from zoneinfo import ZoneInfo

from dateutil import parser
import pendulum


def datetime_normalise(dt: Union[str, datetime]) -> str:
Expand All @@ -28,6 +29,8 @@ def datetime_normalise(dt: Union[str, datetime]) -> str:
:param dt_string: The string to convert
:return: The ISO formatted datetime string
"""
if isinstance(dt, pendulum.DateTime):
dt = dt.to_iso8601_string()
if isinstance(dt, str):
dt = parser.parse(dt) # Parse string to datetime object
if not dt.utcoffset(): # If no timezone present, assume +0000UTC
Expand Down
21 changes: 20 additions & 1 deletion observatory_platform/tests/test_date_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import unittest
from zoneinfo import ZoneInfo

import pendulum

from observatory_platform.date_utils import datetime_normalise


Expand Down Expand Up @@ -60,8 +62,25 @@ def test_dt_inputs(self):
actual_output = datetime_normalise(input)
self.assertEqual(expected_output, actual_output)

def test_pendulum_inputs(self):
inputs = [
pendulum.datetime(2024, 1, 1, 12, 0, 0, tz="UTC"),
pendulum.datetime(2024, 1, 1, 12, 0, 0, tz="Etc/GMT+1"),
pendulum.datetime(2024, 1, 1, 0, 0, 0, tz="Etc/GMT-1"),
pendulum.datetime(2023, 12, 31, 23, 0, 0, tz="Etc/GMT+1"),
]
expected_outputs = [
"2024-01-01T12:00:00+00:00",
"2024-01-01T13:00:00+00:00",
"2023-12-31T23:00:00+00:00",
"2024-01-01T00:00:00+00:00",
]
for input, expected_output in zip(inputs, expected_outputs):
actual_output = datetime_normalise(input)
self.assertEqual(expected_output, actual_output)

def test_missing_tz(self):
inputs = ["2024-01-01 00:00:00", "2024-01-01T12:00:00", datetime(2024, 1, 1, 12, 0, 0)]
inputs = ["2024-01-01 00:00:00", "2024-01-01T12:00:00", datetime(2024, 1, 1, 12, 0, 0), pendulum.datetime(2024, 1, 1, 12, 0, 0)]
expected_outputs = ["2024-01-01T00:00:00+00:00", "2024-01-01T12:00:00+00:00", "2024-01-01T12:00:00+00:00"]
for input, expected_output in zip(inputs, expected_outputs):
actual_output = datetime_normalise(input)
Expand Down

0 comments on commit 212445d

Please sign in to comment.