diff --git a/babel/localtime/__init__.py b/babel/localtime/__init__.py index 8dcbbed13..29577fa37 100644 --- a/babel/localtime/__init__.py +++ b/babel/localtime/__init__.py @@ -11,47 +11,22 @@ import datetime import sys -import time if sys.platform == 'win32': from babel.localtime._win32 import _get_localzone else: from babel.localtime._unix import _get_localzone -STDOFFSET = datetime.timedelta(seconds=-time.timezone) -if time.daylight: - DSTOFFSET = datetime.timedelta(seconds=-time.altzone) -else: - DSTOFFSET = STDOFFSET - -DSTDIFF = DSTOFFSET - STDOFFSET -ZERO = datetime.timedelta(0) - - -class _FallbackLocalTimezone(datetime.tzinfo): - - def utcoffset(self, dt: datetime.datetime) -> datetime.timedelta: - if self._isdst(dt): - return DSTOFFSET - else: - return STDOFFSET - - def dst(self, dt: datetime.datetime) -> datetime.timedelta: - if self._isdst(dt): - return DSTDIFF - else: - return ZERO - - def tzname(self, dt: datetime.datetime) -> str: - return time.tzname[self._isdst(dt)] - def _isdst(self, dt: datetime.datetime) -> bool: - tt = (dt.year, dt.month, dt.day, - dt.hour, dt.minute, dt.second, - dt.weekday(), 0, -1) - stamp = time.mktime(tt) - tt = time.localtime(stamp) - return tt.tm_isdst > 0 +# TODO(3.0): the offset constants are not part of the public API +# and should be removed +from babel.localtime._fallback import ( + DSTDIFF, # noqa: F401 + DSTOFFSET, # noqa: F401 + STDOFFSET, # noqa: F401 + ZERO, # noqa: F401 + _FallbackLocalTimezone, +) def get_localzone() -> datetime.tzinfo: diff --git a/babel/localtime/_fallback.py b/babel/localtime/_fallback.py new file mode 100644 index 000000000..836f5962c --- /dev/null +++ b/babel/localtime/_fallback.py @@ -0,0 +1,47 @@ +""" + babel.localtime._fallback + ~~~~~~~~~~~~~~~~~~~~~~~~~ + + Emulated fallback local timezone when all else fails. + + :copyright: (c) 2013-2023 by the Babel Team. + :license: BSD, see LICENSE for more details. +""" + +import datetime +import time + +STDOFFSET = datetime.timedelta(seconds=-time.timezone) +if time.daylight: + DSTOFFSET = datetime.timedelta(seconds=-time.altzone) +else: + DSTOFFSET = STDOFFSET + +DSTDIFF = DSTOFFSET - STDOFFSET +ZERO = datetime.timedelta(0) + + +class _FallbackLocalTimezone(datetime.tzinfo): + + def utcoffset(self, dt: datetime.datetime) -> datetime.timedelta: + if self._isdst(dt): + return DSTOFFSET + else: + return STDOFFSET + + def dst(self, dt: datetime.datetime) -> datetime.timedelta: + if self._isdst(dt): + return DSTDIFF + else: + return ZERO + + def tzname(self, dt: datetime.datetime) -> str: + return time.tzname[self._isdst(dt)] + + def _isdst(self, dt: datetime.datetime) -> bool: + tt = (dt.year, dt.month, dt.day, + dt.hour, dt.minute, dt.second, + dt.weekday(), 0, -1) + stamp = time.mktime(tt) + tt = time.localtime(stamp) + return tt.tm_isdst > 0 diff --git a/babel/util.py b/babel/util.py index a5403e653..100e37dd1 100644 --- a/babel/util.py +++ b/babel/util.py @@ -255,10 +255,10 @@ def dst(self, dt: datetime.datetime) -> datetime.timedelta: # Export the localtime functionality here because that's # where it was in the past. +# TODO(3.0): remove these aliases UTC = dates.UTC LOCALTZ = dates.LOCALTZ get_localzone = localtime.get_localzone - STDOFFSET = localtime.STDOFFSET DSTOFFSET = localtime.DSTOFFSET DSTDIFF = localtime.DSTDIFF