mirror of
https://github.com/python/cpython.git
synced 2025-11-01 18:51:43 +00:00
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.
This commit is contained in:
parent
e3bcbd2bba
commit
511491ade0
4 changed files with 114 additions and 42 deletions
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue