ReMe/reme/utils/timezone_utils.py
jinliyl b476690c60
fix(config): default to the system timezone (#778)
* Default the application timezone to the system timezone

* Use an explicit timezone in the retention boundary test

* fix(timezone): handle TZ overrides and DST scheduling

* refactor(docker)!: use TZ for the default timezone

BREAKING CHANGE: Use TZ in place of REME_TIMEZONE for Docker deployments. An explicit application timezone takes precedence.

* fix(timezone)!: use IANA names and current cron scheduling

Require croniter 7.0, remove legacy timezone formats and scheduler branches, and preserve triggers across partial-hour DST rollback.

* fix(timezone): preserve final rollback triggers and validate local TZ

* fix(timezone): preserve native rules for local scheduling

* fix(timezone): separate named defaults from active host clock rules

* fix(timezone): share clock rules across scheduling and local dates
2026-11-25 17:59:34 +08:00

61 lines
1.9 KiB
Python

"""Resolve the process timezone from an IANA name or the host configuration."""
import os
import sys
from datetime import datetime, tzinfo
from pathlib import Path
from zoneinfo import ZoneInfo
from tzlocal import get_localzone, get_localzone_name
_LOCALTIME_PATHS = (Path("/etc/localtime"), Path("/usr/local/etc/localtime"))
def _timezone_from_env() -> ZoneInfo | None:
"""Validate the current process override without caching it as a host setting."""
name = os.environ.get("TZ")
if name is not None:
try:
return ZoneInfo(name)
except (LookupError, ValueError) as error:
raise ValueError(f"TZ must be an IANA timezone name: {name!r}") from error
return None
def default_timezone_name() -> str:
"""Use a detected IANA name, with the application's fallback when discovery fails."""
configured = _timezone_from_env()
if configured is not None:
return configured.key
try:
name = get_localzone_name()
if name:
return ZoneInfo(name).key
except (LookupError, OSError, ValueError):
pass
return "Asia/Shanghai"
def system_timezone() -> tzinfo:
"""Resolve process or host rules shared by local-time scheduling and calendar dates."""
configured = _timezone_from_env()
if configured is not None:
return configured
if sys.platform == "win32":
try:
return get_localzone()
except (LookupError, OSError, ValueError):
pass
else:
for path in _LOCALTIME_PATHS:
try:
with path.open("rb") as source:
return ZoneInfo.from_file(source)
except (LookupError, OSError, ValueError):
continue
return datetime.now().astimezone().tzinfo
def resolve_timezone(name: str | None) -> tzinfo:
"""Apply an explicit IANA name or the process's local-time rules consistently."""
return ZoneInfo(name) if name else system_timezone()