Skip to content

Commit

Permalink
Added function to normalise date strings (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
keegansmith21 committed May 30, 2024
1 parent 9188399 commit 17e2949
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
37 changes: 37 additions & 0 deletions observatory_platform/date_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2024 Curtin University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Author: Keegan Smith

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

from dateutil import parser


def datetime_normalise(dt: Union[str, datetime]) -> str:
"""
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
"""
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"))

return dt.isoformat()
68 changes: 68 additions & 0 deletions observatory_platform/tests/test_date_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright 2024 Curtin University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Author: Keegan Smith

from datetime import datetime
import unittest
from zoneinfo import ZoneInfo

from observatory_platform.date_utils import datetime_normalise


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

def test_str_inputs(self):
inputs = [
"2024-01-01 12:00:00+0000",
"2024-01-01 00:00:00+0800",
"2024-01-01 12:00:00-0800",
"2024-01-01 12:00:00Z",
"2024-01-01 12:00:00UTC+1",
]
expected_outputs = [
"2024-01-01T12:00:00+00:00",
"2023-12-31T16:00:00+00:00",
"2024-01-01T20:00:00+00:00",
"2024-01-01T12:00:00+00:00",
"2024-01-01T13: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_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", 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)
self.assertEqual(expected_output, actual_output)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dependencies = [
"validators<=0.20.0",
"xmltodict",
"tenacity",
"python-dateutil",
]

[project.optional-dependencies]
Expand Down

0 comments on commit 17e2949

Please sign in to comment.