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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
"""
This module provides some datetime.tzinfo implementations.
All those classes are taken from the Python documentation.
"""
from datetime import timedelta, tzinfo
import time
ZERO = timedelta(0)
# constant for zero time offset.
class Utc(tzinfo):
"""UTC
Universal time coordinated time zone.
"""
def utcoffset(self, dt):
"""
Return offset from UTC in minutes east of UTC, which is ZERO for UTC.
"""
return ZERO
def tzname(self, dt):
"""
Return the time zone name corresponding to the datetime object dt,
as a string.
"""
return "UTC"
def dst(self, dt):
"""
Return the daylight saving time (DST) adjustment, in minutes east
of UTC.
"""
return ZERO
def __reduce__(self):
"""
When unpickling a Utc object, return the default instance below, UTC.
"""
return _Utc, ()
UTC = Utc()
# the default instance for UTC.
def _Utc():
"""
Helper function for unpickling a Utc object.
"""
return UTC
class FixedOffset(tzinfo):
"""
A class building tzinfo objects for fixed-offset time zones.
Note that FixedOffset(0, 0, "UTC") or FixedOffset() is a different way to
build a UTC tzinfo object.
"""
def __init__(self, offset_hours=0, offset_minutes=0, name="UTC"):
"""
Initialise an instance with time offset and name.
The time offset should be positive for time zones east of UTC
and negate for time zones west of UTC.
"""
self.__offset = timedelta(hours=offset_hours, minutes=offset_minutes)
self.__name = name
def utcoffset(self, dt):
"""
Return offset from UTC in minutes of UTC.
"""
return self.__offset
def tzname(self, dt):
"""
Return the time zone name corresponding to the datetime object dt, as a
string.
"""
return self.__name
def dst(self, dt):
"""
Return the daylight saving time (DST) adjustment, in minutes east of
UTC.
"""
return ZERO
def __repr__(self):
"""
Return nicely formatted repr string.
"""
return "<FixedOffset %r>" % self.__name
STDOFFSET = timedelta(seconds=-time.timezone)
# locale time zone offset
# calculate local daylight saving offset if any.
if time.daylight:
DSTOFFSET = timedelta(seconds=-time.altzone)
else:
DSTOFFSET = STDOFFSET
DSTDIFF = DSTOFFSET - STDOFFSET
# difference between local time zone and local DST time zone
class LocalTimezone(tzinfo):
"""
A class capturing the platform's idea of local time.
"""
def utcoffset(self, dt):
"""
Return offset from UTC in minutes of UTC.
"""
if self._isdst(dt):
return DSTOFFSET
else:
return STDOFFSET
def dst(self, dt):
"""
Return daylight saving offset.
"""
if self._isdst(dt):
return DSTDIFF
else:
return ZERO
def tzname(self, dt):
"""
Return the time zone name corresponding to the datetime object dt, as a
string.
"""
return time.tzname[self._isdst(dt)]
def _isdst(self, dt):
"""
Returns true if DST is active for given datetime object 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
# the default instance for local time zone.
LOCAL = LocalTimezone()
|