Skip to content

Commit

Permalink
updated missed utc timestamp references (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwfraustro authored Dec 8, 2023
1 parent b0228d6 commit b484cdc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
38 changes: 19 additions & 19 deletions tests/voresource/voresource_types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 6 additions & 6 deletions vo_models/xml/voresource/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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):
Expand All @@ -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)

Expand Down

0 comments on commit b484cdc

Please sign in to comment.