diff options
author | Tim Peters <tim.peters@gmail.com> | 2003-01-03 22:26:57 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2003-01-03 22:26:57 (GMT) |
commit | 8827d0c33848d9c1c6489ee31f0a75d7e20c70ef (patch) | |
tree | 5758e77d6db559355cf1568e1440c071863ab391 /Doc/lib/tzinfo-examples.py | |
parent | 6f2bb2362a5f5b7065495456c3caf025c65561d5 (diff) | |
download | cpython-8827d0c33848d9c1c6489ee31f0a75d7e20c70ef.zip cpython-8827d0c33848d9c1c6489ee31f0a75d7e20c70ef.tar.gz cpython-8827d0c33848d9c1c6489ee31f0a75d7e20c70ef.tar.bz2 |
Replaced the flawed "local time" example tzinfo class with the guts
of Guido's later Local.py (from the datetime sandbox).
Diffstat (limited to 'Doc/lib/tzinfo-examples.py')
-rw-r--r-- | Doc/lib/tzinfo-examples.py | 52 |
1 files changed, 39 insertions, 13 deletions
diff --git a/Doc/lib/tzinfo-examples.py b/Doc/lib/tzinfo-examples.py index 868755a..92f298c 100644 --- a/Doc/lib/tzinfo-examples.py +++ b/Doc/lib/tzinfo-examples.py @@ -2,6 +2,8 @@ from datetime import tzinfo, timedelta ZERO = timedelta(0) +# A UTC class. + class UTC(tzinfo): """UTC""" @@ -14,11 +16,17 @@ class UTC(tzinfo): def dst(self, dt): return ZERO +utc = UTC() + +# A class building tzinfo objects for fixed-offset time zones. +# Note that FixedOffset(0, "UTC") is a different way to build a +# UTC tzinfo object. + class FixedOffset(tzinfo): """Fixed offset in minutes east from UTC.""" def __init__(self, offset, name): - self.__offset = offset + self.__offset = timdelta(minutes = offset) self.__name = name def utcoffset(self, dt): @@ -30,23 +38,41 @@ class FixedOffset(tzinfo): def dst(self, dt): return ZERO -import time +# A class capturing the platform's idea of local time. -class LocalTime(tzinfo): - """Local time as defined by the operating system.""" +import time as _time - def _isdst(self, dt): - t = (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, - -1, -1, -1) - # XXX This may fail for years < 1970 or >= 2038 - t = time.localtime(time.mktime(t)) - return t.tm_isdst > 0 +STDOFFSET = timedelta(seconds = -_time.timezone) +if _time.daylight: + DSTOFFSET = timedelta(seconds = -_time.altzone) +else: + DSTOFFSET = STDOFFSET + +DSTDIFF = DSTOFFSET - STDOFFSET + +class LocalTimezone(tzinfo): def utcoffset(self, dt): if self._isdst(dt): - return timedelta(seconds=-time.timezone) + return DSTOFFSET else: - return timedelta(seconds=-time.altzone) + return STDOFFSET + + def dst(self, dt): + if self._isdst(dt): + return DSTDIFF + else: + return ZERO def tzname(self, dt): - return time.tzname[self._isdst(dt)] + return _time.tzname[self._isdst(dt)] + + def _isdst(self, dt): + 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 + +Local = LocalTimezone() |