From d6d80a17444c90259c5bfdacb84c61e6bfece655 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 5 Jan 2023 11:38:41 +0100 Subject: Merging upstream version 3.0.0~a1. Signed-off-by: Daniel Baumann --- pendulum/tz/__init__.py | 140 +++++++++++++++++++++++++++--------------------- 1 file changed, 80 insertions(+), 60 deletions(-) (limited to 'pendulum/tz/__init__.py') diff --git a/pendulum/tz/__init__.py b/pendulum/tz/__init__.py index b085f37..45c9855 100644 --- a/pendulum/tz/__init__.py +++ b/pendulum/tz/__init__.py @@ -1,60 +1,80 @@ -from typing import Tuple -from typing import Union - -import pytzdata - -from .local_timezone import get_local_timezone -from .local_timezone import set_local_timezone -from .local_timezone import test_local_timezone -from .timezone import UTC -from .timezone import FixedTimezone as _FixedTimezone -from .timezone import Timezone as _Timezone - - -PRE_TRANSITION = "pre" -POST_TRANSITION = "post" -TRANSITION_ERROR = "error" - -timezones = pytzdata.timezones # type: Tuple[str, ...] - - -_tz_cache = {} - - -def timezone(name, extended=True): # type: (Union[str, int], bool) -> _Timezone - """ - Return a Timezone instance given its name. - """ - if isinstance(name, int): - return fixed_timezone(name) - - if name.lower() == "utc": - return UTC - - if name in _tz_cache: - return _tz_cache[name] - - tz = _Timezone(name, extended=extended) - _tz_cache[name] = tz - - return tz - - -def fixed_timezone(offset): # type: (int) -> _FixedTimezone - """ - Return a Timezone instance given its offset in seconds. - """ - if offset in _tz_cache: - return _tz_cache[offset] # type: ignore - - tz = _FixedTimezone(offset) - _tz_cache[offset] = tz - - return tz - - -def local_timezone(): # type: () -> _Timezone - """ - Return the local timezone. - """ - return get_local_timezone() +from __future__ import annotations + +import sys + +from pendulum.tz.local_timezone import get_local_timezone +from pendulum.tz.local_timezone import set_local_timezone +from pendulum.tz.local_timezone import test_local_timezone +from pendulum.tz.timezone import UTC +from pendulum.tz.timezone import FixedTimezone +from pendulum.tz.timezone import Timezone + +if sys.version_info >= (3, 9): + from importlib import resources +else: + import importlib_resources as resources + +PRE_TRANSITION = "pre" +POST_TRANSITION = "post" +TRANSITION_ERROR = "error" + +_timezones = None + +_tz_cache: dict[int, FixedTimezone] = {} + + +def timezones() -> tuple[str, ...]: + global _timezones + + if _timezones is None: + with resources.files("tzdata").joinpath("zones").open() as f: + _timezones = tuple(tz.strip() for tz in f.readlines()) + + return _timezones + + +def timezone(name: str | int) -> Timezone | FixedTimezone: + """ + Return a Timezone instance given its name. + """ + if isinstance(name, int): + return fixed_timezone(name) + + if name.lower() == "utc": + return UTC + + return Timezone(name) + + +def fixed_timezone(offset: int) -> FixedTimezone: + """ + Return a Timezone instance given its offset in seconds. + """ + if offset in _tz_cache: + return _tz_cache[offset] + + tz = FixedTimezone(offset) + _tz_cache[offset] = tz + + return tz + + +def local_timezone() -> Timezone | FixedTimezone: + """ + Return the local timezone. + """ + return get_local_timezone() + + +__all__ = [ + "UTC", + "Timezone", + "FixedTimezone", + "set_local_timezone", + "get_local_timezone", + "test_local_timezone", + "timezone", + "fixed_timezone", + "local_timezone", + "timezones", +] -- cgit v1.2.3