diff options
author | Fred Drake <fdrake@acm.org> | 2002-12-23 18:52:19 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2002-12-23 18:52:19 (GMT) |
commit | ce5200842ec30a86703bf1e2669fef3b28319e42 (patch) | |
tree | 855aa216d27b556a27bf4344b1bb3bba310aa590 /Doc/lib/tzinfo-examples.py | |
parent | 1fc1fe840e0d306e4211fdb9099e911dca5b9933 (diff) | |
download | cpython-ce5200842ec30a86703bf1e2669fef3b28319e42.zip cpython-ce5200842ec30a86703bf1e2669fef3b28319e42.tar.gz cpython-ce5200842ec30a86703bf1e2669fef3b28319e42.tar.bz2 |
Move the examples of concrete tzinfo classes to a separate file, so the
verbatim environment does not bollux page breaking.
Diffstat (limited to 'Doc/lib/tzinfo-examples.py')
-rw-r--r-- | Doc/lib/tzinfo-examples.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/Doc/lib/tzinfo-examples.py b/Doc/lib/tzinfo-examples.py new file mode 100644 index 0000000..1d3b3c4 --- /dev/null +++ b/Doc/lib/tzinfo-examples.py @@ -0,0 +1,51 @@ +from datetime import tzinfo + +class UTC(tzinfo): + """UTC""" + + def utcoffset(self, dt): + return 0 + + def tzname(self, dt): + return "UTC" + + def dst(self, dt): + return 0 + +class FixedOffset(tzinfo): + """Fixed offset in minutes east from UTC.""" + + def __init__(self, offset, name): + self.__offset = offset + self.__name = name + + def utcoffset(self, dt): + return self.__offset + + def tzname(self, dt): + return self.__name + + def dst(self, dt): + # It depends on more than we know in an example. + return None # Indicate we don't know + +import time + +class LocalTime(tzinfo): + """Local time as defined by the operating system.""" + + 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 + + def utcoffset(self, dt): + if self._isdst(dt): + return -time.timezone/60 + else: + return -time.altzone/60 + + def tzname(self, dt): + return time.tzname[self._isdst(dt)] |