Skip to content

Commit

Permalink
Updated fn to parse datetime objects as well
Browse files Browse the repository at this point in the history
  • Loading branch information
keegansmith21 committed May 29, 2024
1 parent f9eada3 commit 314e7c7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
9 changes: 6 additions & 3 deletions observatory_platform/date_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@

# Author: Keegan Smith

from datetime import datetime
from typing import Union
from zoneinfo import ZoneInfo

from dateutil import parser


def normalise_datetime(dt_string: str) -> str:
def datetime_normalise(dt: Union[str, datetime]) -> str:
"""
Converts a date or datetime string to an isoformatted datetime string at +0000UTC
Converts a datetime object or string to an isoformatted datetime string at +0000UTC
:param dt_string: The string to convert
:return: The ISO formatted datetime string
"""
dt = parser.parse(dt_string) # Parse string to datetime object
if isinstance(dt, str):
dt = parser.parse(dt) # Parse string to datetime object
if not dt.utcoffset(): # If no timezone present, assume +0000UTC
dt = dt.replace(tzinfo=ZoneInfo("UTC"))
dt = dt.astimezone(ZoneInfo("UTC"))
Expand Down
31 changes: 25 additions & 6 deletions observatory_platform/tests/test_date_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@

# Author: Keegan Smith

from datetime import datetime
import unittest
from zoneinfo import ZoneInfo

from observatory_platform.date_utils import normalise_datetime
from observatory_platform.date_utils import datetime_normalise


class test_normalise_datetime(unittest.TestCase):
"""Tests for normalise_datetime"""

def test_good_inputs(self):
def test_str_inputs(self):
inputs = [
"2024-01-01 12:00:00+0000",
"2024-01-01 00:00:00+0800",
Expand All @@ -38,12 +40,29 @@ def test_good_inputs(self):
"2024-01-01T13:00:00+00:00",
]
for input, expected_output in zip(inputs, expected_outputs):
actual_output = normalise_datetime(input)
actual_output = datetime_normalise(input)
self.assertEqual(expected_output, actual_output)

def test_dt_inputs(self):
inputs = [
datetime(2024, 1, 1, 12, 0, 0, tzinfo=ZoneInfo("UTC")),
datetime(2024, 1, 1, 12, 0, 0, tzinfo=ZoneInfo("Etc/GMT+1")),
datetime(2024, 1, 1, 0, 0, 0, tzinfo=ZoneInfo("Etc/GMT-1")),
datetime(2023, 12, 31, 23, 0, 0, tzinfo=ZoneInfo("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"]
expected_outputs = ["2024-01-01T00:00:00+00:00", "2024-01-01T12:00:00+00:00"]
inputs = ["2024-01-01 00:00:00", "2024-01-01T12:00:00", 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 = normalise_datetime(input)
actual_output = datetime_normalise(input)
self.assertEqual(expected_output, actual_output)

0 comments on commit 314e7c7

Please sign in to comment.