[pypy-commit] pypy default: Copy function utcfromtimestamp from CPython 3.2's datetime.py
dripton
noreply at buildbot.pypy.org
Sat Jan 21 17:26:08 CET 2012
Author: David Ripton <dripton at ripton.net>
Branch:
Changeset: r51584:235d8b8434a8
Date: 2012-01-18 20:07 -0500
http://bitbucket.org/pypy/pypy/changeset/235d8b8434a8/
Log: Copy function utcfromtimestamp from CPython 3.2's datetime.py
Fixes issue972, which was caused by rounding differences between
fromtimestamp and utcfromtimestamp.
diff --git a/lib_pypy/datetime.py b/lib_pypy/datetime.py
--- a/lib_pypy/datetime.py
+++ b/lib_pypy/datetime.py
@@ -1440,17 +1440,22 @@
return result
fromtimestamp = classmethod(fromtimestamp)
+ @classmethod
def utcfromtimestamp(cls, t):
"Construct a UTC datetime from a POSIX timestamp (like time.time())."
- if 1 - (t % 1.0) < 0.0000005:
- 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)
- utcfromtimestamp = classmethod(utcfromtimestamp)
# XXX This is supposed to do better than we *can* do by using time.time(),
# XXX if the platform supports a more accurate way. The C implementation
More information about the pypy-commit
mailing list