-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Give _FallbackLocalTimezone its own module
- Loading branch information
Showing
3 changed files
with
57 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters