MCPcopy Index your code
hub / github.com/RustPython/RustPython / fromutc

Method fromutc

Lib/zoneinfo/_zoneinfo.py:114–156  ·  view source on GitHub ↗

Convert from datetime in UTC to datetime in local time

(self, dt)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 5

_get_local_timestampMethod · 0.95
isinstanceFunction · 0.85
lenFunction · 0.85
total_secondsMethod · 0.80
replaceMethod · 0.45

Tested by

no test coverage detected