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

Method fromutc

Lib/test/datetimetester.py:6173–6201  ·  view source on GitHub ↗

datetime in UTC -> datetime in local time.

(self, dt)

Source from the content-addressed store, hash-verified

6171class tzinfo2(tzinfo):
6172
6173 def fromutc(self, dt):
6174 "datetime in UTC -> datetime in local time."
6175
6176 if not isinstance(dt, datetime):
6177 raise TypeError("fromutc() requires a datetime argument")
6178 if dt.tzinfo is not self:
6179 raise ValueError("dt.tzinfo is not self")
6180 # Returned value satisfies
6181 # dt + ldt.utcoffset() = ldt
6182 off0 = dt.replace(fold=0).utcoffset()
6183 off1 = dt.replace(fold=1).utcoffset()
6184 if off0 is None or off1 is None or dt.dst() is None:
6185 raise ValueError
6186 if off0 == off1:
6187 ldt = dt + off0
6188 off1 = ldt.utcoffset()
6189 if off0 == off1:
6190 return ldt
6191 # Now, we discovered both possible offsets, so
6192 # we can just try four possible solutions:
6193 for off in [off0, off1]:
6194 ldt = dt + off
6195 if ldt.utcoffset() == off:
6196 return ldt
6197 ldt = ldt.replace(fold=1)
6198 if ldt.utcoffset() == off:
6199 return ldt
6200
6201 raise ValueError("No suitable local time found")
6202
6203# Reimplementing simplified US timezones to respect the "fold" flag:
6204

Callers 8

test_issue23600Method · 0.45
test_fromutcMethod · 0.45
test_fromutcMethod · 0.45
test_fromutcMethod · 0.45
test_foldsMethod · 0.45
test_gapsMethod · 0.45
test_fromutc_errorsMethod · 0.45
test_fold_mutateMethod · 0.45

Calls 3

utcoffsetMethod · 0.45
replaceMethod · 0.45
dstMethod · 0.45

Tested by

no test coverage detected