MCPcopy Create free account
hub / github.com/scottrogowski/code2flow / USTimeZone

Class USTimeZone

tests/test_code/py/pytz/reference.py:97–135  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

95
96# A complete implementation of current DST rules for major US time zones.
97class USTimeZone(tzinfo):
98
99 def __init__(self, hours, reprname, stdname, dstname):
100 self.stdoffset = timedelta(hours=hours)
101 self.reprname = reprname
102 self.stdname = stdname
103 self.dstname = dstname
104
105 def __repr__(self):
106 return self.reprname
107
108 def tzname(self, dt):
109 if self.dst(dt):
110 return self.dstname
111 else:
112 return self.stdname
113
114 def utcoffset(self, dt):
115 return self.stdoffset + self.dst(dt)
116
117 def dst(self, dt):
118 if dt is None or dt.tzinfo is None:
119 # An exception may be sensible here, in one or both cases.
120 # It depends on how you want to treat them. The default
121 # fromutc() implementation (called by the default astimezone()
122 # implementation) passes a datetime with dt.tzinfo is self.
123 return ZERO
124 assert dt.tzinfo is self
125
126 # Find first Sunday in April & the last in October.
127 start = first_sunday_on_or_after(DSTSTART.replace(year=dt.year))
128 end = first_sunday_on_or_after(DSTEND.replace(year=dt.year))
129
130 # Can't compare naive to aware objects, so strip the timezone from
131 # dt first.
132 if start <= dt.replace(tzinfo=None) < end:
133 return HOUR
134 else:
135 return ZERO
136
137Eastern = USTimeZone(-5, "Eastern", "EST", "EDT")
138Central = USTimeZone(-6, "Central", "CST", "CDT")

Callers 1

reference.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected