Skip to content

Commit

Permalink
Added function to normalise date strings
Browse files Browse the repository at this point in the history
  • Loading branch information
keegansmith21 committed May 27, 2024
1 parent 9188399 commit b8ed689
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
34 changes: 34 additions & 0 deletions observatory_platform/date_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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 zoneinfo import ZoneInfo

from dateutil import parser


def normalise_datetime(dt_string: str) -> str:
"""
Converts a date or datetime 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 not dt.utcoffset(): # If no timezone present, assume +0000UTC
dt = dt.replace(tzinfo=ZoneInfo("UTC"))
dt = dt.astimezone(ZoneInfo("UTC"))

return dt.isoformat()
49 changes: 49 additions & 0 deletions observatory_platform/tests/test_date_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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

import unittest

from observatory_platform.date_utils import normalise_datetime


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

def test_good_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 = normalise_datetime(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"]
for input, expected_output in zip(inputs, expected_outputs):
actual_output = normalise_datetime(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",
"dateutils",
]

[project.optional-dependencies]
Expand Down

0 comments on commit b8ed689

Please sign in to comment.