-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added function to normalise date strings
- Loading branch information
1 parent
9188399
commit b8ed689
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters