Issue #23517: fromtimestamp() and utcfromtimestamp() methods of

datetime.datetime now round microseconds to nearest with ties going away from
zero (ROUND_HALF_UP), as Python 2 and Python older than 3.3, instead of
rounding towards -Infinity (ROUND_FLOOR).
This commit is contained in:
Victor Stinner 2015-09-03 09:06:44 +02:00
parent 0fa5ef72b7
commit 2ec5bd6fb2
4 changed files with 14 additions and 8 deletions

View file

@ -1384,7 +1384,7 @@ class datetime(date):
converter = _time.localtime if tz is None else _time.gmtime
t, frac = divmod(t, 1.0)
us = int(frac * 1e6)
us = _round_half_up(frac * 1e6)
# If timestamp is less than one microsecond smaller than a
# full second, us can be rounded up to 1000000. In this case,
@ -1404,7 +1404,7 @@ class datetime(date):
def utcfromtimestamp(cls, t):
"""Construct a naive UTC datetime from a POSIX timestamp."""
t, frac = divmod(t, 1.0)
us = int(frac * 1e6)
us = _round_half_up(frac * 1e6)
# If timestamp is less than one microsecond smaller than a
# full second, us can be rounded up to 1000000. In this case,