Commits

Brian Kearns committed 91b8e27

speed up datetime creation from timestamp significantly

Comments (0)

Files changed (1)

 import time as _time
 import math as _math
 
+def _round(x):
+    return _math.floor(x + 0.5) if x >= 0.0 else _math.ceil(x - 0.5)
+
 MINYEAR = 1
 MAXYEAR = 9999
 
         converter = _time.localtime if tz is None else _time.gmtime
 
         t, frac = divmod(t, 1.0)
-        us = int(round(frac * 1e6))
+        us = int(_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,
     def utcfromtimestamp(cls, t):
         "Construct a UTC datetime from a POSIX timestamp (like time.time())."
         t, frac = divmod(t, 1.0)
-        us = int(round(frac * 1e6))
+        us = int(_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,