Skip to content

Commit

Permalink
Give _FallbackLocalTimezone its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Jan 20, 2023
1 parent e66f74b commit 66214c2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 35 deletions.
43 changes: 9 additions & 34 deletions babel/localtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
47 changes: 47 additions & 0 deletions babel/localtime/_fallback.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion babel/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 66214c2

Please sign in to comment.