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

Method _fromtimestamp

Lib/_pydatetime.py:1855–1896  ·  view source on GitHub ↗

Construct a datetime from a POSIX timestamp (like time.time()). A timezone info object may be passed in as well.

(cls, t, utc, tz)

Source from the content-addressed store, hash-verified

1853
1854 @classmethod
1855 def _fromtimestamp(cls, t, utc, tz):
1856 """Construct a datetime from a POSIX timestamp (like time.time()).
1857
1858 A timezone info object may be passed in as well.
1859 """
1860 frac, t = _math.modf(t)
1861 us = round(frac * 1e6)
1862 if us >= 1000000:
1863 t += 1
1864 us -= 1000000
1865 elif us < 0:
1866 t -= 1
1867 us += 1000000
1868
1869 converter = _time.gmtime if utc else _time.localtime
1870 y, m, d, hh, mm, ss, weekday, jday, dst = converter(t)
1871 ss = min(ss, 59) # clamp out leap seconds if the platform has them
1872 result = cls(y, m, d, hh, mm, ss, us, tz)
1873 if tz is None and not utc:
1874 # As of version 2015f max fold in IANA database is
1875 # 23 hours at 1969-09-30 13:00:00 in Kwajalein.
1876 # Let's probe 24 hours in the past to detect a transition:
1877 max_fold_seconds = 24 * 3600
1878
1879 # On Windows localtime_s throws an OSError for negative values,
1880 # thus we can't perform fold detection for values of time less
1881 # than the max time fold. See comments in _datetimemodule's
1882 # version of this method for more details.
1883 if t < max_fold_seconds and sys.platform.startswith("win"):
1884 return result
1885
1886 y, m, d, hh, mm, ss = converter(t - max_fold_seconds)[:6]
1887 probe1 = cls(y, m, d, hh, mm, ss, us, tz)
1888 trans = result - probe1 - timedelta(0, max_fold_seconds)
1889 if trans.days < 0:
1890 y, m, d, hh, mm, ss = converter(t + trans // timedelta(0, 1))[:6]
1891 probe2 = cls(y, m, d, hh, mm, ss, us, tz)
1892 if probe2 == result:
1893 result._fold = 1
1894 elif tz is not None:
1895 result = tz.fromutc(result)
1896 return result
1897
1898 @classmethod
1899 def fromtimestamp(cls, timestamp, tz=None):

Callers 3

fromtimestampMethod · 0.80
utcfromtimestampMethod · 0.80
utcnowMethod · 0.80

Calls 4

timedeltaClass · 0.85
clsClass · 0.50
startswithMethod · 0.45
fromutcMethod · 0.45

Tested by

no test coverage detected