summaryrefslogtreecommitdiffstats
path: root/Doc/lib/tzinfo-examples.py
blob: 1d3b3c4d78e17fba28d30573ec701fd2cea128fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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)]