diff options
author | Alexander Belopolsky <alexander.belopolsky@gmail.com> | 2010-09-21 16:30:56 (GMT) |
---|---|---|
committer | Alexander Belopolsky <alexander.belopolsky@gmail.com> | 2010-09-21 16:30:56 (GMT) |
commit | 3e62f78c4ef51972258b8f6bf76cb725cabddcad (patch) | |
tree | 92b455da50cc66f8adaa1a2fd93477780aea39b6 /Lib/datetime.py | |
parent | b3bfc3d88b9693f9a21dfcee2530f889025f239d (diff) | |
download | cpython-3e62f78c4ef51972258b8f6bf76cb725cabddcad.zip cpython-3e62f78c4ef51972258b8f6bf76cb725cabddcad.tar.gz cpython-3e62f78c4ef51972258b8f6bf76cb725cabddcad.tar.bz2 |
Fixed microsecond rounding in python version of utcfromtimestamp
Diffstat (limited to 'Lib/datetime.py')
-rw-r--r-- | Lib/datetime.py | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/Lib/datetime.py b/Lib/datetime.py index 23ded3e..d640f75 100644 --- a/Lib/datetime.py +++ b/Lib/datetime.py @@ -1379,12 +1379,17 @@ class datetime(date): @classmethod def utcfromtimestamp(cls, t): "Construct a UTC datetime from a POSIX timestamp (like time.time())." - if 1 - (t % 1.0) < 0.000001: - t = float(int(t)) + 1 - if t < 0: - t -= 1 + 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 = _time.gmtime(t) - us = int((t % 1.0) * 1000000) ss = min(ss, 59) # clamp out leap seconds if the platform has them return cls(y, m, d, hh, mm, ss, us) |