52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
"""
|
|
Timezone utilities for consistent timestamp handling across the gateway.
|
|
Ensures all timestamps are properly handled as UTC.
|
|
"""
|
|
|
|
from datetime import datetime, timezone, timedelta
|
|
from typing import Union, Optional
|
|
|
|
def get_utc_now() -> datetime:
|
|
"""
|
|
Get current time in UTC with timezone info.
|
|
|
|
Returns:
|
|
datetime: Current UTC time with timezone info
|
|
"""
|
|
return datetime.now(timezone.utc)
|
|
|
|
def get_utc_timestamp() -> float:
|
|
"""
|
|
Get current UTC timestamp (seconds since epoch).
|
|
|
|
Returns:
|
|
float: Current UTC timestamp in seconds
|
|
"""
|
|
return datetime.now(timezone.utc).timestamp()
|
|
|
|
def to_utc_timestamp(dt: datetime) -> float:
|
|
"""
|
|
Convert datetime object to UTC timestamp.
|
|
|
|
Args:
|
|
dt (datetime): Datetime object to convert
|
|
|
|
Returns:
|
|
float: UTC timestamp in seconds
|
|
"""
|
|
if dt.tzinfo is None:
|
|
# If naive datetime, assume it's UTC
|
|
dt = dt.replace(tzinfo=timezone.utc)
|
|
return dt.timestamp()
|
|
|
|
def create_expiration_timestamp(expires_in_seconds: int) -> float:
|
|
"""
|
|
Create a new expiration timestamp from seconds until expiration.
|
|
|
|
Args:
|
|
expires_in_seconds (int): Seconds until expiration
|
|
|
|
Returns:
|
|
float: UTC timestamp in seconds
|
|
"""
|
|
return get_utc_timestamp() + expires_in_seconds
|