mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-11-18 08:29:14 +00:00
feature(time): timestamp parsers
This commit is contained in:
parent
b545a400c3
commit
73a847f288
30
selfprivacy_api/utils/time.py
Normal file
30
selfprivacy_api/utils/time.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
from datetime import datetime, timezone
|
||||
|
||||
|
||||
def tzaware_parse_time(iso_timestamp: str) -> datetime:
|
||||
"""
|
||||
parse an iso8601 timestamp into timezone-aware datetime
|
||||
assume utc if no timezone in stamp
|
||||
example of timestamp:
|
||||
2023-11-10T12:07:47.868788+00:00
|
||||
|
||||
"""
|
||||
dt = datetime.fromisoformat(iso_timestamp)
|
||||
if dt.tzinfo is None:
|
||||
dt = dt.astimezone(timezone.utc)
|
||||
return dt
|
||||
|
||||
|
||||
def tzaware_parse_time_strict(iso_timestamp: str) -> datetime:
|
||||
"""
|
||||
parse an iso8601 timestamp into timezone-aware datetime
|
||||
raise an error if no timezone in stamp
|
||||
example of timestamp:
|
||||
2023-11-10T12:07:47.868788+00:00
|
||||
|
||||
"""
|
||||
dt = datetime.fromisoformat(iso_timestamp)
|
||||
if dt.tzinfo is None:
|
||||
raise ValueError("no timezone in timestamp", iso_timestamp)
|
||||
dt = dt.astimezone(timezone.utc)
|
||||
return dt
|
Loading…
Reference in a new issue