From b484cdc61a76a4970793e18d91f0df017ce43a8a Mon Sep 17 00:00:00 2001 From: Joshua Fraustro <36318163+jwfraustro@users.noreply.github.com> Date: Fri, 8 Dec 2023 17:45:16 -0500 Subject: [PATCH] updated missed utc timestamp references (#3) --- tests/voresource/voresource_types_test.py | 38 +++++++++++------------ vo_models/xml/voresource/types.py | 12 +++---- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/tests/voresource/voresource_types_test.py b/tests/voresource/voresource_types_test.py index 350dbf3..ddebf88 100644 --- a/tests/voresource/voresource_types_test.py +++ b/tests/voresource/voresource_types_test.py @@ -4,39 +4,39 @@ from vo_models.xml.voresource.types import UTCTimestamp -class TestVODatetimeModel(TestCase): - """Test VODatetime parsing""" +class TestUTCTimestampModel(TestCase): + """Test UTCTimestamp parsing""" - def test_vodatetime_parse(self): + def test_utctimestamp_parse(self): """Test that datetimes are parsed and output in correct format""" # Test allowed string formats - good_vo_dt = "2023-03-15T18:27:18.758Z" + good_vo_utc = "2023-03-15T18:27:18.758Z" # 2023-03-15T18:27:18.758 (No timezone - Z UTC assumed) - vo_dt = UTCTimestamp.fromisoformat("2023-03-15T18:27:18.758") - self.assertIsInstance(vo_dt, UTCTimestamp) - self.assertEqual(vo_dt.isoformat(), good_vo_dt) + vo_utc = UTCTimestamp.fromisoformat("2023-03-15T18:27:18.758") + self.assertIsInstance(vo_utc, UTCTimestamp) + self.assertEqual(vo_utc.isoformat(), good_vo_utc) # 2023-03-15T18:27:18.758Z (Zulu UTC - T separator) - vo_dt = UTCTimestamp.fromisoformat("2023-03-15T18:27:18.758Z") - self.assertIsInstance(vo_dt, UTCTimestamp) - self.assertEqual(vo_dt.isoformat(), good_vo_dt) + vo_utc = UTCTimestamp.fromisoformat("2023-03-15T18:27:18.758Z") + self.assertIsInstance(vo_utc, UTCTimestamp) + self.assertEqual(vo_utc.isoformat(), good_vo_utc) # 2023-03-15 18:27:18.758Z (Zulu UTC - space separator) - vo_dt = UTCTimestamp.fromisoformat("2023-03-15 18:27:18.758Z") - self.assertIsInstance(vo_dt, UTCTimestamp) - self.assertEqual(vo_dt.isoformat(), good_vo_dt) + vo_utc = UTCTimestamp.fromisoformat("2023-03-15 18:27:18.758Z") + self.assertIsInstance(vo_utc, UTCTimestamp) + self.assertEqual(vo_utc.isoformat(), good_vo_utc) # 2023-03-15T18:27:18.758+00:00 (UTC w/ offset - T separator) - vo_dt = UTCTimestamp.fromisoformat("2023-03-15T18:27:18.758+00:00") - self.assertIsInstance(vo_dt, UTCTimestamp) - self.assertEqual(vo_dt.isoformat(), good_vo_dt) + vo_utc = UTCTimestamp.fromisoformat("2023-03-15T18:27:18.758+00:00") + self.assertIsInstance(vo_utc, UTCTimestamp) + self.assertEqual(vo_utc.isoformat(), good_vo_utc) # 2023-03-15 18:27:18.758+00:00 (UTC w/ offset - space separator) - vo_dt = UTCTimestamp.fromisoformat("2023-03-15 18:27:18.758+00:00") - self.assertIsInstance(vo_dt, UTCTimestamp) - self.assertEqual(vo_dt.isoformat(), good_vo_dt) + vo_utc = UTCTimestamp.fromisoformat("2023-03-15 18:27:18.758+00:00") + self.assertIsInstance(vo_utc, UTCTimestamp) + self.assertEqual(vo_utc.isoformat(), good_vo_utc) # Test that we reject non-UTC datetimes with self.assertRaises(ValueError): diff --git a/vo_models/xml/voresource/types.py b/vo_models/xml/voresource/types.py index c462f50..d59ad9a 100644 --- a/vo_models/xml/voresource/types.py +++ b/vo_models/xml/voresource/types.py @@ -18,7 +18,7 @@ class UTCTimestamp(datetime): # vodt_regex = r"\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?Z?" # Expanded regex to accept Zulu but also +00:00 offset UTC times - exp_vodt_regex = r"(\d{4}-\d\d-\d\d(T|\s)\d\d:\d\d:\d\d(\.\d+)?)(Z|\+\d\d:\d\d)?" + exp_utc_regex = r"(\d{4}-\d\d-\d\d(T|\s)\d\d:\d\d:\d\d(\.\d+)?)(Z|\+\d\d:\d\d)?" # Will match: # 2023-03-15T18:27:18.758 (UTC assumed - T separator) # 2023-03-15 18:27:18.758 (UTC assumed - space separator) @@ -29,7 +29,7 @@ class UTCTimestamp(datetime): # TODO: Python 3.11 datetime.fromisoformat() does accept a 'Z' indicated UTC time. Revisit this when upgrading. - vodt_regex_match = re.compile(exp_vodt_regex) + utc_regex_match = re.compile(exp_utc_regex) def __str__(self) -> str: return self.isoformat(sep="T", timespec="milliseconds") @@ -58,7 +58,7 @@ def _validate(cls, value: str): value (str): datetime string. Comes from either a user's POST (destruction) or from the cache Returns: - VODateTime: VO-compliant datetime subclass + UTCTimestamp: VO-compliant datetime subclass """ if isinstance(value, UTCTimestamp): @@ -72,13 +72,13 @@ def _validate(cls, value: str): value = value.upper() - valid_vodt = cls.vodt_regex_match.fullmatch(value) - if not valid_vodt: + valid_utc = cls.utc_regex_match.fullmatch(value) + if not valid_utc: # If there was no full match, reject it raise ValueError("Invalid VOResource ISO-8601 date format") # Grab only the date/time match and manually add a UTC offset for an aware python datetime object - value = valid_vodt.group(1) + "+00:00" + value = valid_utc.group(1) + "+00:00" return super().fromisoformat(value)