Construct a datetime from a POSIX timestamp (like time.time()). A timezone info object may be passed in as well.
(cls, t, utc, tz)
| 1818 | |
| 1819 | @classmethod |
| 1820 | def _fromtimestamp(cls, t, utc, tz): |
| 1821 | """Construct a datetime from a POSIX timestamp (like time.time()). |
| 1822 | |
| 1823 | A timezone info object may be passed in as well. |
| 1824 | """ |
| 1825 | frac, t = _math.modf(t) |
| 1826 | us = round(frac * 1e6) |
| 1827 | if us >= 1000000: |
| 1828 | t += 1 |
| 1829 | us -= 1000000 |
| 1830 | elif us < 0: |
| 1831 | t -= 1 |
| 1832 | us += 1000000 |
| 1833 | |
| 1834 | converter = _time.gmtime if utc else _time.localtime |
| 1835 | y, m, d, hh, mm, ss, weekday, jday, dst = converter(t) |
| 1836 | ss = min(ss, 59) # clamp out leap seconds if the platform has them |
| 1837 | result = cls(y, m, d, hh, mm, ss, us, tz) |
| 1838 | if tz is None and not utc: |
| 1839 | # As of version 2015f max fold in IANA database is |
| 1840 | # 23 hours at 1969-09-30 13:00:00 in Kwajalein. |
| 1841 | # Let's probe 24 hours in the past to detect a transition: |
| 1842 | max_fold_seconds = 24 * 3600 |
| 1843 | |
| 1844 | # On Windows localtime_s throws an OSError for negative values, |
| 1845 | # thus we can't perform fold detection for values of time less |
| 1846 | # than the max time fold. See comments in _datetimemodule's |
| 1847 | # version of this method for more details. |
| 1848 | if t < max_fold_seconds and sys.platform.startswith("win"): |
| 1849 | return result |
| 1850 | |
| 1851 | y, m, d, hh, mm, ss = converter(t - max_fold_seconds)[:6] |
| 1852 | probe1 = cls(y, m, d, hh, mm, ss, us, tz) |
| 1853 | trans = result - probe1 - timedelta(0, max_fold_seconds) |
| 1854 | if trans.days < 0: |
| 1855 | y, m, d, hh, mm, ss = converter(t + trans // timedelta(0, 1))[:6] |
| 1856 | probe2 = cls(y, m, d, hh, mm, ss, us, tz) |
| 1857 | if probe2 == result: |
| 1858 | result._fold = 1 |
| 1859 | elif tz is not None: |
| 1860 | result = tz.fromutc(result) |
| 1861 | return result |
| 1862 | |
| 1863 | @classmethod |
| 1864 | def fromtimestamp(cls, timestamp, tz=None): |