32 lines
873 B
Python
32 lines
873 B
Python
"""Timezone handling.
|
|
|
|
The container may run in UTC, but timestamps (history, filenames) and the cron
|
|
schedule are evaluated in a user-configurable timezone stored in the JSON config
|
|
(`timezone`), defaulting to the TZ env var, then UTC.
|
|
"""
|
|
import datetime
|
|
import logging
|
|
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
|
|
|
from . import settings, store
|
|
|
|
log = logging.getLogger("timeutil")
|
|
|
|
|
|
def tz_name() -> str:
|
|
return store.load().get("timezone") or settings.DEFAULT_TZ or "UTC"
|
|
|
|
|
|
def get_tz() -> ZoneInfo:
|
|
name = tz_name()
|
|
try:
|
|
return ZoneInfo(name)
|
|
except (ZoneInfoNotFoundError, ValueError, OSError):
|
|
log.warning("unknown timezone '%s', falling back to UTC", name)
|
|
return ZoneInfo("UTC")
|
|
|
|
|
|
def now() -> datetime.datetime:
|
|
"""Timezone-aware 'now' in the configured timezone."""
|
|
return datetime.datetime.now(get_tz())
|