diff options
author | Alexander Belopolsky <alexander.belopolsky@gmail.com> | 2010-07-26 02:36:41 (GMT) |
---|---|---|
committer | Alexander Belopolsky <alexander.belopolsky@gmail.com> | 2010-07-26 02:36:41 (GMT) |
commit | aeb039863d2962dcffa60823d9afab2c39c8d8fc (patch) | |
tree | 5844b8d3c9245c4bc365d39e9454c78944305f4f /Lib/datetime.py | |
parent | 0f0c3320ee82a16d57f76ac96a781379e61e3296 (diff) | |
download | cpython-aeb039863d2962dcffa60823d9afab2c39c8d8fc.zip cpython-aeb039863d2962dcffa60823d9afab2c39c8d8fc.tar.gz cpython-aeb039863d2962dcffa60823d9afab2c39c8d8fc.tar.bz2 |
Make python version of fromtimestamp behave more like C.
Diffstat (limited to 'Lib/datetime.py')
-rw-r--r-- | Lib/datetime.py | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/Lib/datetime.py b/Lib/datetime.py index 19f20e7..23ded3e 100644 --- a/Lib/datetime.py +++ b/Lib/datetime.py @@ -1356,16 +1356,20 @@ class datetime(date): """ _check_tzinfo_arg(tz) - if tz is None: - converter = _time.localtime - else: - converter = _time.gmtime - if 1 - (t % 1.0) < 0.000001: - t = float(int(t)) + 1 - if t < 0: - t -= 1 + + converter = _time.localtime if tz is None else _time.gmtime + + t, frac = divmod(t, 1.0) + us = round(frac * 1e6) + + # If timestamp is less than one microsecond smaller than a + # full second, us can be rounded up to 1000000. In this case, + # roll over to seconds, otherwise, ValueError is raised + # by the constructor. + if us == 1000000: + t += 1 + us = 0 y, m, d, hh, mm, ss, weekday, jday, dst = converter(t) - us = int((t % 1.0) * 1000000) ss = min(ss, 59) # clamp out leap seconds if the platform has them result = cls(y, m, d, hh, mm, ss, us, tz) if tz is not None: |