Commits

Maciej Fijalkowski  committed c6f9f4e Merge
  • Participants
  • Parent commits 6a0c4cc, eeedeff

Comments (0)

Files changed (2)

File lib_pypy/datetime.py

         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

File pypy/module/test_lib_pypy/test_datetime.py

+"""Additional tests for datetime."""
+
+import time
+import datetime
+import os
+
+def test_utcfromtimestamp():
+    """Confirm that utcfromtimestamp and fromtimestamp give consistent results.
+
+    Based on danchr's test script in https://bugs.pypy.org/issue986
+    """
+    try:
+        prev_tz = os.environ.get("TZ")
+        os.environ["TZ"] = "GMT"
+        for unused in xrange(100):
+            now = time.time()
+            delta = (datetime.datetime.utcfromtimestamp(now) -
+                     datetime.datetime.fromtimestamp(now))
+            assert delta.days * 86400 + delta.seconds == 0
+    finally:
+        if prev_tz is None:
+            del os.environ["TZ"]
+        else:
+            os.environ["TZ"] = prev_tz