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

Method fromutc

Lib/test/datetimetester.py:6675–6703  ·  view source on GitHub ↗

datetime in UTC -> datetime in local time.

(self, dt)

Source from the content-addressed store, hash-verified

6673 EPOCHORDINAL = date(1970, 1, 1).toordinal()
6674
6675 def fromutc(self, dt):
6676 """datetime in UTC -> datetime in local time."""
6677
6678 if not isinstance(dt, datetime):
6679 raise TypeError("fromutc() requires a datetime argument")
6680 if dt.tzinfo is not self:
6681 raise ValueError("dt.tzinfo is not self")
6682
6683 timestamp = ((dt.toordinal() - self.EPOCHORDINAL) * 86400
6684 + dt.hour * 3600
6685 + dt.minute * 60
6686 + dt.second)
6687
6688 if timestamp < self.ut[1]:
6689 tti = self.ti[0]
6690 fold = 0
6691 else:
6692 idx = bisect.bisect_right(self.ut, timestamp)
6693 assert self.ut[idx-1] <= timestamp
6694 assert idx == len(self.ut) or timestamp < self.ut[idx]
6695 tti_prev, tti = self.ti[idx-2:idx]
6696 # Detect fold
6697 shift = tti_prev[0] - tti[0]
6698 fold = (shift > timedelta(0, timestamp - self.ut[idx-1]))
6699 dt += tti[0]
6700 if fold:
6701 return dt.replace(fold=1)
6702 else:
6703 return dt
6704
6705 def _find_ti(self, dt, i):
6706 timestamp = ((dt.toordinal() - self.EPOCHORDINAL) * 86400

Callers

nothing calls this directly

Calls 3

timedeltaClass · 0.90
toordinalMethod · 0.80
replaceMethod · 0.45

Tested by

no test coverage detected