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

Method fromutc

Lib/zoneinfo/_zoneinfo.py:118–160  ·  view source on GitHub ↗

Convert from datetime in UTC to datetime in local time

(self, dt)

Source from the content-addressed store, hash-verified

116 return self._find_trans(dt).tzname
117
118 def fromutc(self, dt):
119 """Convert from datetime in UTC to datetime in local time"""
120
121 if not isinstance(dt, datetime):
122 raise TypeError("fromutc() requires a datetime argument")
123 if dt.tzinfo is not self:
124 raise ValueError("dt.tzinfo is not self")
125
126 timestamp = self._get_local_timestamp(dt)
127 num_trans = len(self._trans_utc)
128
129 if num_trans >= 1 and timestamp < self._trans_utc[0]:
130 tti = self._tti_before
131 fold = 0
132 elif (
133 num_trans == 0 or timestamp > self._trans_utc[-1]
134 ) and not isinstance(self._tz_after, _ttinfo):
135 tti, fold = self._tz_after.get_trans_info_fromutc(
136 timestamp, dt.year
137 )
138 elif num_trans == 0:
139 tti = self._tz_after
140 fold = 0
141 else:
142 idx = bisect.bisect_right(self._trans_utc, timestamp)
143
144 if num_trans > 1 and timestamp >= self._trans_utc[1]:
145 tti_prev, tti = self._ttinfos[idx - 2 : idx]
146 elif timestamp > self._trans_utc[-1]:
147 tti_prev = self._ttinfos[-1]
148 tti = self._tz_after
149 else:
150 tti_prev = self._tti_before
151 tti = self._ttinfos[0]
152
153 # Detect fold
154 shift = tti_prev.utcoff - tti.utcoff
155 fold = shift.total_seconds() > timestamp - self._trans_utc[idx - 1]
156 dt += tti.utcoff
157 if fold:
158 return dt.replace(fold=1)
159 else:
160 return dt
161
162 def _find_trans(self, dt):
163 if dt is None:

Callers

nothing calls this directly

Calls 3

_get_local_timestampMethod · 0.95
total_secondsMethod · 0.80
replaceMethod · 0.45

Tested by

no test coverage detected