summaryrefslogtreecommitdiffstats
path: root/Lib/datetime.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-09-02 17:16:07 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-09-02 17:16:07 (GMT)
commit2ec558739e6bd32365e1a883889f9d5372b35719 (patch)
tree96eebd9fb83fcc59feb0ee478f6cbd0d105a3d29 /Lib/datetime.py
parent8cbb013553c07e2577d08c7046c0eee70d0c0b66 (diff)
downloadcpython-2ec558739e6bd32365e1a883889f9d5372b35719.zip
cpython-2ec558739e6bd32365e1a883889f9d5372b35719.tar.gz
cpython-2ec558739e6bd32365e1a883889f9d5372b35719.tar.bz2
Issue #23517: datetime.timedelta constructor now rounds microseconds to nearest
with ties going away from zero (ROUND_HALF_UP), as Python 2 and Python older than 3.3, instead of rounding to nearest with ties going to nearest even integer (ROUND_HALF_EVEN).
Diffstat (limited to 'Lib/datetime.py')
-rw-r--r--Lib/datetime.py12
1 files changed, 10 insertions, 2 deletions
diff --git a/Lib/datetime.py b/Lib/datetime.py
index db13b12..d661460 100644
--- a/Lib/datetime.py
+++ b/Lib/datetime.py
@@ -316,6 +316,14 @@ def _divide_and_round(a, b):
return q
+def _round_half_up(x):
+ """Round to nearest with ties going away from zero."""
+ if x >= 0.0:
+ return _math.floor(x + 0.5)
+ else:
+ return _math.ceil(x - 0.5)
+
+
class timedelta:
"""Represent the difference between two datetime objects.
@@ -399,7 +407,7 @@ class timedelta:
# secondsfrac isn't referenced again
if isinstance(microseconds, float):
- microseconds = round(microseconds + usdouble)
+ microseconds = _round_half_up(microseconds + usdouble)
seconds, microseconds = divmod(microseconds, 1000000)
days, seconds = divmod(seconds, 24*3600)
d += days
@@ -410,7 +418,7 @@ class timedelta:
days, seconds = divmod(seconds, 24*3600)
d += days
s += seconds
- microseconds = round(microseconds + usdouble)
+ microseconds = _round_half_up(microseconds + usdouble)
assert isinstance(s, int)
assert isinstance(microseconds, int)
assert abs(s) <= 3 * 24 * 3600