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

Method fromutc

Lib/test/datetimetester.py:5986–6014  ·  view source on GitHub ↗

datetime in UTC -> datetime in local time.

(self, dt)

Source from the content-addressed store, hash-verified

5984class tzinfo2(tzinfo):
5985
5986 def fromutc(self, dt):
5987 "datetime in UTC -> datetime in local time."
5988
5989 if not isinstance(dt, datetime):
5990 raise TypeError("fromutc() requires a datetime argument")
5991 if dt.tzinfo is not self:
5992 raise ValueError("dt.tzinfo is not self")
5993 # Returned value satisfies
5994 # dt + ldt.utcoffset() = ldt
5995 off0 = dt.replace(fold=0).utcoffset()
5996 off1 = dt.replace(fold=1).utcoffset()
5997 if off0 is None or off1 is None or dt.dst() is None:
5998 raise ValueError
5999 if off0 == off1:
6000 ldt = dt + off0
6001 off1 = ldt.utcoffset()
6002 if off0 == off1:
6003 return ldt
6004 # Now, we discovered both possible offsets, so
6005 # we can just try four possible solutions:
6006 for off in [off0, off1]:
6007 ldt = dt + off
6008 if ldt.utcoffset() == off:
6009 return ldt
6010 ldt = ldt.replace(fold=1)
6011 if ldt.utcoffset() == off:
6012 return ldt
6013
6014 raise ValueError("No suitable local time found")
6015
6016# Reimplementing simplified US timezones to respect the "fold" flag:
6017

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 4

isinstanceFunction · 0.85
utcoffsetMethod · 0.45
replaceMethod · 0.45
dstMethod · 0.45

Tested by

no test coverage detected