summaryrefslogtreecommitdiff
path: root/Lib/test/datetimetester.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-09-18 14:42:05 +0200
committerVictor Stinner <victor.stinner@gmail.com>2015-09-18 14:42:05 +0200
commit511491ade0bb77febb176bc75f049797f0c71ed0 (patch)
tree1981db4c2c00eef86f32c03589174f98e46df857 /Lib/test/datetimetester.py
parente3bcbd2bbade81a3591d69f607e1722c6016a489 (diff)
downloadcpython-git-511491ade0bb77febb176bc75f049797f0c71ed0.tar.gz
Issue #23517: Fix rounding in fromtimestamp() and utcfromtimestamp() methods
of datetime.datetime: microseconds are now rounded to nearest with ties going to nearest even integer (ROUND_HALF_EVEN), instead of being rounding towards zero (ROUND_DOWN). It's important that these methods use the same rounding mode than datetime.timedelta to keep the property: (datetime(1970,1,1) + timedelta(seconds=t)) == datetime.utcfromtimestamp(t) It also the rounding mode used by round(float) for example. Add more unit tests on the rounding mode in test_datetime.
Diffstat (limited to 'Lib/test/datetimetester.py')
-rw-r--r--Lib/test/datetimetester.py24
1 files changed, 20 insertions, 4 deletions
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index 8e48b9fd53..a942d4de81 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -650,8 +650,16 @@ class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase):
# Single-field rounding.
eq(td(milliseconds=0.4/1000), td(0)) # rounds to 0
eq(td(milliseconds=-0.4/1000), td(0)) # rounds to 0
+ eq(td(milliseconds=0.5/1000), td(microseconds=0))
+ eq(td(milliseconds=-0.5/1000), td(microseconds=-0))
eq(td(milliseconds=0.6/1000), td(microseconds=1))
eq(td(milliseconds=-0.6/1000), td(microseconds=-1))
+ eq(td(milliseconds=1.5/1000), td(microseconds=2))
+ eq(td(milliseconds=-1.5/1000), td(microseconds=-2))
+ eq(td(seconds=0.5/10**6), td(microseconds=0))
+ eq(td(seconds=-0.5/10**6), td(microseconds=-0))
+ eq(td(seconds=1/2**7), td(microseconds=7812))
+ eq(td(seconds=-1/2**7), td(microseconds=-7812))
# Rounding due to contributions from more than one field.
us_per_hour = 3600e6
@@ -1824,12 +1832,14 @@ class TestDateTime(TestDate):
tzinfo=timezone(timedelta(hours=-5), 'EST'))
self.assertEqual(t.timestamp(),
18000 + 3600 + 2*60 + 3 + 4*1e-6)
+
def test_microsecond_rounding(self):
for fts in [self.theclass.fromtimestamp,
self.theclass.utcfromtimestamp]:
zero = fts(0)
self.assertEqual(zero.second, 0)
self.assertEqual(zero.microsecond, 0)
+ one = fts(1e-6)
try:
minus_one = fts(-1e-6)
except OSError:
@@ -1840,22 +1850,28 @@ class TestDateTime(TestDate):
self.assertEqual(minus_one.microsecond, 999999)
t = fts(-1e-8)
- self.assertEqual(t, minus_one)
+ self.assertEqual(t, zero)
t = fts(-9e-7)
self.assertEqual(t, minus_one)
t = fts(-1e-7)
- self.assertEqual(t, minus_one)
+ self.assertEqual(t, zero)
+ t = fts(-1/2**7)
+ self.assertEqual(t.second, 59)
+ self.assertEqual(t.microsecond, 992188)
t = fts(1e-7)
self.assertEqual(t, zero)
t = fts(9e-7)
- self.assertEqual(t, zero)
+ self.assertEqual(t, one)
t = fts(0.99999949)
self.assertEqual(t.second, 0)
self.assertEqual(t.microsecond, 999999)
t = fts(0.9999999)
+ self.assertEqual(t.second, 1)
+ self.assertEqual(t.microsecond, 0)
+ t = fts(1/2**7)
self.assertEqual(t.second, 0)
- self.assertEqual(t.microsecond, 999999)
+ self.assertEqual(t.microsecond, 7812)
def test_insane_fromtimestamp(self):
# It's possible that some platform maps time_t to double,