blob: 5a2b8ad1a7fccf219aeb9bf510936f2aa7e8e15c [file] [log] [blame]
Tim Petersadf64202003-01-04 06:03:15 +00001from datetime import tzinfo, timedelta, datetime
Tim Peters397301e2003-01-02 21:28:08 +00002
3ZERO = timedelta(0)
Tim Petersadf64202003-01-04 06:03:15 +00004HOUR = timedelta(hours=1)
Fred Drakece520082002-12-23 18:52:19 +00005
Tim Peters8827d0c2003-01-03 22:26:57 +00006# A UTC class.
7
Fred Drakece520082002-12-23 18:52:19 +00008class UTC(tzinfo):
9 """UTC"""
10
11 def utcoffset(self, dt):
Tim Peters397301e2003-01-02 21:28:08 +000012 return ZERO
Fred Drakece520082002-12-23 18:52:19 +000013
14 def tzname(self, dt):
15 return "UTC"
16
17 def dst(self, dt):
Tim Peters397301e2003-01-02 21:28:08 +000018 return ZERO
Fred Drakece520082002-12-23 18:52:19 +000019
Tim Peters8827d0c2003-01-03 22:26:57 +000020utc = UTC()
21
22# A class building tzinfo objects for fixed-offset time zones.
23# Note that FixedOffset(0, "UTC") is a different way to build a
24# UTC tzinfo object.
25
Fred Drakece520082002-12-23 18:52:19 +000026class FixedOffset(tzinfo):
27 """Fixed offset in minutes east from UTC."""
28
29 def __init__(self, offset, name):
Tim Petersf196a0a2003-01-22 04:45:50 +000030 self.__offset = timedelta(minutes = offset)
Fred Drakece520082002-12-23 18:52:19 +000031 self.__name = name
32
33 def utcoffset(self, dt):
34 return self.__offset
35
36 def tzname(self, dt):
37 return self.__name
38
39 def dst(self, dt):
Tim Peters397301e2003-01-02 21:28:08 +000040 return ZERO
Fred Drakece520082002-12-23 18:52:19 +000041
Tim Peters8827d0c2003-01-03 22:26:57 +000042# A class capturing the platform's idea of local time.
Fred Drakece520082002-12-23 18:52:19 +000043
Tim Peters8827d0c2003-01-03 22:26:57 +000044import time as _time
Fred Drakece520082002-12-23 18:52:19 +000045
Tim Peters8827d0c2003-01-03 22:26:57 +000046STDOFFSET = timedelta(seconds = -_time.timezone)
47if _time.daylight:
48 DSTOFFSET = timedelta(seconds = -_time.altzone)
49else:
50 DSTOFFSET = STDOFFSET
51
52DSTDIFF = DSTOFFSET - STDOFFSET
53
54class LocalTimezone(tzinfo):
Fred Drakece520082002-12-23 18:52:19 +000055
56 def utcoffset(self, dt):
57 if self._isdst(dt):
Tim Peters8827d0c2003-01-03 22:26:57 +000058 return DSTOFFSET
Fred Drakece520082002-12-23 18:52:19 +000059 else:
Tim Peters8827d0c2003-01-03 22:26:57 +000060 return STDOFFSET
61
62 def dst(self, dt):
63 if self._isdst(dt):
64 return DSTDIFF
65 else:
66 return ZERO
Fred Drakece520082002-12-23 18:52:19 +000067
68 def tzname(self, dt):
Tim Peters8827d0c2003-01-03 22:26:57 +000069 return _time.tzname[self._isdst(dt)]
70
71 def _isdst(self, dt):
72 tt = (dt.year, dt.month, dt.day,
73 dt.hour, dt.minute, dt.second,
74 dt.weekday(), 0, -1)
75 stamp = _time.mktime(tt)
76 tt = _time.localtime(stamp)
77 return tt.tm_isdst > 0
78
79Local = LocalTimezone()
Tim Petersadf64202003-01-04 06:03:15 +000080
81
82# A complete implementation of current DST rules for major US time zones.
83
84def first_sunday_on_or_after(dt):
85 days_to_go = 6 - dt.weekday()
86 if days_to_go:
87 dt += timedelta(days_to_go)
88 return dt
89
90# In the US, DST starts at 2am (standard time) on the first Sunday in April.
91DSTSTART = datetime(1, 4, 1, 2)
92# and ends at 2am (DST time; 1am standard time) on the last Sunday of Oct.
93# which is the first Sunday on or after Oct 25.
Tim Peters327098a2003-01-20 22:54:38 +000094DSTEND = datetime(1, 10, 25, 1)
Tim Petersadf64202003-01-04 06:03:15 +000095
96class USTimeZone(tzinfo):
97
98 def __init__(self, hours, reprname, stdname, dstname):
99 self.stdoffset = timedelta(hours=hours)
100 self.reprname = reprname
101 self.stdname = stdname
102 self.dstname = dstname
103
104 def __repr__(self):
105 return self.reprname
106
107 def tzname(self, dt):
108 if self.dst(dt):
109 return self.dstname
110 else:
111 return self.stdname
112
113 def utcoffset(self, dt):
114 return self.stdoffset + self.dst(dt)
115
116 def dst(self, dt):
117 if dt is None or dt.tzinfo is None:
118 # An exception may be sensible here, in one or both cases.
Tim Petersf196a0a2003-01-22 04:45:50 +0000119 # It depends on how you want to treat them. The default
120 # fromutc() implementation (called by the default astimezone()
121 # implementation) passes a datetime with dt.tzinfo is self.
Tim Petersadf64202003-01-04 06:03:15 +0000122 return ZERO
123 assert dt.tzinfo is self
124
125 # Find first Sunday in April & the last in October.
126 start = first_sunday_on_or_after(DSTSTART.replace(year=dt.year))
127 end = first_sunday_on_or_after(DSTEND.replace(year=dt.year))
128
129 # Can't compare naive to aware objects, so strip the timezone from
130 # dt first.
131 if start <= dt.replace(tzinfo=None) < end:
132 return HOUR
133 else:
134 return ZERO
135
136Eastern = USTimeZone(-5, "Eastern", "EST", "EDT")
137Central = USTimeZone(-6, "Central", "CST", "CDT")
138Mountain = USTimeZone(-7, "Mountain", "MST", "MDT")
139Pacific = USTimeZone(-8, "Pacific", "PST", "PDT")