Construct a datetime from a POSIX timestamp (like time.time()). A timezone info object may be passed in as well.
(cls, t, utc, tz)
| 1737 | |
| 1738 | @classmethod |
| 1739 | def _fromtimestamp(cls, t, utc, tz): |
| 1740 | """Construct a datetime from a POSIX timestamp (like time.time()). |
| 1741 | |
| 1742 | A timezone info object may be passed in as well. |
| 1743 | """ |
| 1744 | frac, t = _math.modf(t) |
| 1745 | us = round(frac * 1e6) |
| 1746 | if us >= 1000000: |
| 1747 | t += 1 |
| 1748 | us -= 1000000 |
| 1749 | elif us < 0: |
| 1750 | t -= 1 |
| 1751 | us += 1000000 |
| 1752 | |
| 1753 | converter = _time.gmtime if utc else _time.localtime |
| 1754 | y, m, d, hh, mm, ss, weekday, jday, dst = converter(t) |
| 1755 | ss = min(ss, 59) # clamp out leap seconds if the platform has them |
| 1756 | result = cls(y, m, d, hh, mm, ss, us, tz) |
| 1757 | if tz is None and not utc: |
| 1758 | # As of version 2015f max fold in IANA database is |
| 1759 | # 23 hours at 1969-09-30 13:00:00 in Kwajalein. |
| 1760 | # Let's probe 24 hours in the past to detect a transition: |
| 1761 | max_fold_seconds = 24 * 3600 |
| 1762 | |
| 1763 | # On Windows localtime_s throws an OSError for negative values, |
| 1764 | # thus we can't perform fold detection for values of time less |
| 1765 | # than the max time fold. See comments in _datetimemodule's |
| 1766 | # version of this method for more details. |
| 1767 | if t < max_fold_seconds and sys.platform.startswith("win"): |
| 1768 | return result |
| 1769 | |
| 1770 | y, m, d, hh, mm, ss = converter(t - max_fold_seconds)[:6] |
| 1771 | probe1 = cls(y, m, d, hh, mm, ss, us, tz) |
| 1772 | trans = result - probe1 - timedelta(0, max_fold_seconds) |
| 1773 | if trans.days < 0: |
| 1774 | y, m, d, hh, mm, ss = converter(t + trans // timedelta(0, 1))[:6] |
| 1775 | probe2 = cls(y, m, d, hh, mm, ss, us, tz) |
| 1776 | if probe2 == result: |
| 1777 | result._fold = 1 |
| 1778 | elif tz is not None: |
| 1779 | result = tz.fromutc(result) |
| 1780 | return result |
| 1781 | |
| 1782 | @classmethod |
| 1783 | def fromtimestamp(cls, t, tz=None): |
no test coverage detected