mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-01-23 09:16:51 +00:00
refactor(time): more time functions
This commit is contained in:
parent
e78bcca9f2
commit
8453f62c74
|
@ -1,6 +1,29 @@
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_tz_aware(dt: datetime) -> datetime:
|
||||||
|
"""
|
||||||
|
returns timezone-aware datetime
|
||||||
|
assumes utc on naive datetime input
|
||||||
|
"""
|
||||||
|
if dt.tzinfo is None:
|
||||||
|
dt = dt.astimezone(timezone.utc)
|
||||||
|
return dt
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_tz_aware_strict(dt: datetime) -> datetime:
|
||||||
|
"""
|
||||||
|
returns timezone-aware datetime
|
||||||
|
raises error if input is a naive datetime
|
||||||
|
"""
|
||||||
|
if dt.tzinfo is None:
|
||||||
|
raise ValueError(
|
||||||
|
"no timezone in datetime (tz-aware datetime is required for this operation)",
|
||||||
|
dt,
|
||||||
|
)
|
||||||
|
return dt
|
||||||
|
|
||||||
|
|
||||||
def tzaware_parse_time(iso_timestamp: str) -> datetime:
|
def tzaware_parse_time(iso_timestamp: str) -> datetime:
|
||||||
"""
|
"""
|
||||||
parse an iso8601 timestamp into timezone-aware datetime
|
parse an iso8601 timestamp into timezone-aware datetime
|
||||||
|
@ -10,8 +33,7 @@ def tzaware_parse_time(iso_timestamp: str) -> datetime:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
dt = datetime.fromisoformat(iso_timestamp)
|
dt = datetime.fromisoformat(iso_timestamp)
|
||||||
if dt.tzinfo is None:
|
dt = ensure_tz_aware(dt)
|
||||||
dt = dt.astimezone(timezone.utc)
|
|
||||||
return dt
|
return dt
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,7 +46,5 @@ def tzaware_parse_time_strict(iso_timestamp: str) -> datetime:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
dt = datetime.fromisoformat(iso_timestamp)
|
dt = datetime.fromisoformat(iso_timestamp)
|
||||||
if dt.tzinfo is None:
|
dt = ensure_tz_aware_strict(dt)
|
||||||
raise ValueError("no timezone in timestamp", iso_timestamp)
|
|
||||||
dt = dt.astimezone(timezone.utc)
|
|
||||||
return dt
|
return dt
|
||||||
|
|
Loading…
Reference in a new issue