ยปCore Development>Code coverage>Doc/includes/tzinfo-examples.py

# Python code coverage for Doc/includes/tzinfo-examples.py

#countcontent
1n/afrom datetime import tzinfo, timedelta, datetime, timezone
2n/a
3n/aZERO = timedelta(0)
4n/aHOUR = timedelta(hours=1)
5n/aSECOND = timedelta(seconds=1)
6n/a
7n/a# A class capturing the platform's idea of local time.
8n/a# (May result in wrong values on historical times in
9n/a# timezones where UTC offset and/or the DST rules had
10n/a# changed in the past.)
11n/aimport time as _time
12n/a
13n/aSTDOFFSET = timedelta(seconds = -_time.timezone)
14n/aif _time.daylight:
15n/a DSTOFFSET = timedelta(seconds = -_time.altzone)
16n/aelse:
17n/a DSTOFFSET = STDOFFSET
18n/a
20n/a
21n/aclass LocalTimezone(tzinfo):
22n/a
23n/a def fromutc(self, dt):
24n/a assert dt.tzinfo is self
25n/a stamp = (dt - datetime(1970, 1, 1, tzinfo=self)) // SECOND
26n/a args = _time.localtime(stamp)[:6]
27n/a dst_diff = DSTDIFF // SECOND
28n/a # Detect fold
29n/a fold = (args == _time.localtime(stamp - dst_diff))
30n/a return datetime(*args, microsecond=dt.microsecond,
31n/a tzinfo=self, fold=fold)
32n/a
33n/a def utcoffset(self, dt):
34n/a if self._isdst(dt):
35n/a return DSTOFFSET
36n/a else:
37n/a return STDOFFSET
38n/a
39n/a def dst(self, dt):
40n/a if self._isdst(dt):
41n/a return DSTDIFF
42n/a else:
43n/a return ZERO
44n/a
45n/a def tzname(self, dt):
46n/a return _time.tzname[self._isdst(dt)]
47n/a
48n/a def _isdst(self, dt):
49n/a tt = (dt.year, dt.month, dt.day,
50n/a dt.hour, dt.minute, dt.second,
51n/a dt.weekday(), 0, 0)
52n/a stamp = _time.mktime(tt)
53n/a tt = _time.localtime(stamp)
54n/a return tt.tm_isdst > 0
55n/a
56n/aLocal = LocalTimezone()
57n/a
58n/a
59n/a# A complete implementation of current DST rules for major US time zones.
60n/a
62n/a days_to_go = 6 - dt.weekday()
63n/a if days_to_go:
64n/a dt += timedelta(days_to_go)
65n/a return dt
66n/a
67n/a
68n/a# US DST Rules
69n/a#
70n/a# This is a simplified (i.e., wrong for a few cases) set of rules for US
71n/a# DST start and end times. For a complete and up-to-date set of DST rules
72n/a# and timezone definitions, visit the Olson Database (or try pytz):
74n/a# http://sourceforge.net/projects/pytz/ (might not be up-to-date)
75n/a#
76n/a# In the US, since 2007, DST starts at 2am (standard time) on the second
77n/a# Sunday in March, which is the first Sunday on or after Mar 8.
78n/aDSTSTART_2007 = datetime(1, 3, 8, 2)
79n/a# and ends at 2am (DST time) on the first Sunday of Nov.
80n/aDSTEND_2007 = datetime(1, 11, 1, 2)
81n/a# From 1987 to 2006, DST used to start at 2am (standard time) on the first
82n/a# Sunday in April and to end at 2am (DST time) on the last
83n/a# Sunday of October, which is the first Sunday on or after Oct 25.
84n/aDSTSTART_1987_2006 = datetime(1, 4, 1, 2)
85n/aDSTEND_1987_2006 = datetime(1, 10, 25, 2)
86n/a# From 1967 to 1986, DST used to start at 2am (standard time) on the last
87n/a# Sunday in April (the one on or after April 24) and to end at 2am (DST time)
88n/a# on the last Sunday of October, which is the first Sunday
89n/a# on or after Oct 25.
90n/aDSTSTART_1967_1986 = datetime(1, 4, 24, 2)
92n/a
94n/a # Find start and end times for US DST. For years before 1967, return
95n/a # start = end for no DST.
96n/a if 2006 < year:
97n/a dststart, dstend = DSTSTART_2007, DSTEND_2007
98n/a elif 1986 < year < 2007:
99n/a dststart, dstend = DSTSTART_1987_2006, DSTEND_1987_2006
100n/a elif 1966 < year < 1987:
101n/a dststart, dstend = DSTSTART_1967_1986, DSTEND_1967_1986
102n/a else:
103n/a return (datetime(year, 1, 1), ) * 2
104n/a
105n/a start = first_sunday_on_or_after(dststart.replace(year=year))
106n/a end = first_sunday_on_or_after(dstend.replace(year=year))
107n/a return start, end
108n/a
109n/a
110n/aclass USTimeZone(tzinfo):
111n/a
112n/a def __init__(self, hours, reprname, stdname, dstname):
113n/a self.stdoffset = timedelta(hours=hours)
114n/a self.reprname = reprname
115n/a self.stdname = stdname
116n/a self.dstname = dstname
117n/a
118n/a def __repr__(self):
119n/a return self.reprname
120n/a
121n/a def tzname(self, dt):
122n/a if self.dst(dt):
123n/a return self.dstname
124n/a else:
125n/a return self.stdname
126n/a
127n/a def utcoffset(self, dt):
128n/a return self.stdoffset + self.dst(dt)
129n/a
130n/a def dst(self, dt):
131n/a if dt is None or dt.tzinfo is None:
132n/a # An exception may be sensible here, in one or both cases.
133n/a # It depends on how you want to treat them. The default
134n/a # fromutc() implementation (called by the default astimezone()
135n/a # implementation) passes a datetime with dt.tzinfo is self.
136n/a return ZERO
137n/a assert dt.tzinfo is self
138n/a start, end = us_dst_range(dt.year)
139n/a # Can't compare naive to aware objects, so strip the timezone from
140n/a # dt first.
141n/a dt = dt.replace(tzinfo=None)
142n/a if start + HOUR <= dt < end - HOUR:
143n/a # DST is in effect.
144n/a return HOUR
145n/a if end - HOUR <= dt < end:
146n/a # Fold (an ambiguous hour): use dt.fold to disambiguate.
147n/a return ZERO if dt.fold else HOUR
148n/a if start <= dt < start + HOUR:
149n/a # Gap (a non-existent hour): reverse the fold rule.
150n/a return HOUR if dt.fold else ZERO
151n/a # DST is off.
152n/a return ZERO
153n/a
154n/a def fromutc(self, dt):
155n/a assert dt.tzinfo is self
156n/a start, end = us_dst_range(dt.year)
157n/a start = start.replace(tzinfo=self)
158n/a end = end.replace(tzinfo=self)
159n/a std_time = dt + self.stdoffset
160n/a dst_time = std_time + HOUR
161n/a if end <= dst_time < end + HOUR:
162n/a # Repeated hour
163n/a return std_time.replace(fold=1)
164n/a if std_time < start or dst_time >= end:
165n/a # Standard time
166n/a return std_time
167n/a if start <= std_time < end - HOUR:
168n/a # Daylight saving time
169n/a return dst_time
170n/a
171n/a
172n/aEastern = USTimeZone(-5, "Eastern", "EST", "EDT")
173n/aCentral = USTimeZone(-6, "Central", "CST", "CDT")
174n/aMountain = USTimeZone(-7, "Mountain", "MST", "MDT")
175n/aPacific = USTimeZone(-8, "Pacific", "PST", "PDT")