Initial commit (Clean history)

This commit is contained in:
anhduy-tech
2025-12-30 11:27:14 +07:00
commit ef48c93de0
19255 changed files with 3248867 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
"""
Timezone utility functions.
"""
# Copyright (C) 2020 The Psycopg Team
from __future__ import annotations
import logging
from datetime import timezone, tzinfo
from zoneinfo import ZoneInfo
from .pq.abc import PGconn
logger = logging.getLogger("psycopg")
_timezones: dict[bytes | None, tzinfo] = {
None: timezone.utc,
b"UTC": timezone.utc,
}
def get_tzinfo(pgconn: PGconn | None) -> tzinfo:
"""Return the Python timezone info of the connection's timezone."""
tzname = pgconn.parameter_status(b"TimeZone") if pgconn else None
try:
return _timezones[tzname]
except KeyError:
sname = tzname.decode() if tzname else "UTC"
try:
zi: tzinfo = ZoneInfo(sname)
except (KeyError, OSError):
logger.warning("unknown PostgreSQL timezone: %r; will use UTC", sname)
zi = timezone.utc
except Exception as ex:
logger.warning(
"error handling PostgreSQL timezone: %r; will use UTC (%s - %s)",
sname,
type(ex).__name__,
ex,
)
zi = timezone.utc
_timezones[tzname] = zi
return zi