MCPcopy Index your code
hub / github.com/python/cpython / USTimeZone

Class USTimeZone

Doc/includes/tzinfo_examples.py:113–172  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

111
112
113class USTimeZone(dt.tzinfo):
114
115 def __init__(self, hours, reprname, stdname, dstname):
116 self.stdoffset = dt.timedelta(hours=hours)
117 self.reprname = reprname
118 self.stdname = stdname
119 self.dstname = dstname
120
121 def __repr__(self):
122 return self.reprname
123
124 def tzname(self, when):
125 if self.dst(when):
126 return self.dstname
127 else:
128 return self.stdname
129
130 def utcoffset(self, when):
131 return self.stdoffset + self.dst(when)
132
133 def dst(self, when):
134 if when is None or when.tzinfo is None:
135 # An exception may be sensible here, in one or both cases.
136 # It depends on how you want to treat them. The default
137 # fromutc() implementation (called by the default astimezone()
138 # implementation) passes a datetime with when.tzinfo is self.
139 return ZERO
140 assert when.tzinfo is self
141 start, end = us_dst_range(when.year)
142 # Can't compare naive to aware objects, so strip the timezone from
143 # when first.
144 when = when.replace(tzinfo=None)
145 if start + HOUR <= when < end - HOUR:
146 # DST is in effect.
147 return HOUR
148 if end - HOUR <= when < end:
149 # Fold (an ambiguous hour): use when.fold to disambiguate.
150 return ZERO if when.fold else HOUR
151 if start <= when < start + HOUR:
152 # Gap (a non-existent hour): reverse the fold rule.
153 return HOUR if when.fold else ZERO
154 # DST is off.
155 return ZERO
156
157 def fromutc(self, when):
158 assert when.tzinfo is self
159 start, end = us_dst_range(when.year)
160 start = start.replace(tzinfo=self)
161 end = end.replace(tzinfo=self)
162 std_time = when + self.stdoffset
163 dst_time = std_time + HOUR
164 if end <= dst_time < end + HOUR:
165 # Repeated hour
166 return std_time.replace(fold=1)
167 if std_time < start or dst_time >= end:
168 # Standard time
169 return std_time
170 if start <= std_time < end - HOUR:

Callers 1

tzinfo_examples.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…