cpython: 3c29d05c0710 (original) (raw)

Mercurial > cpython

changeset 97679:3c29d05c0710

Issue #23517: Fix implementation of the ROUND_HALF_UP rounding mode in datetime.datetime.fromtimestamp() and datetime.datetime.utcfromtimestamp(). microseconds sign should be kept before rounding. [#23517]

Victor Stinner victor.stinner@gmail.com
date Fri, 04 Sep 2015 23:57:25 +0200
parents d4089ff1e656
children d755f75019c8
files Lib/datetime.py Lib/test/datetimetester.py Lib/test/test_time.py Python/pytime.c
diffstat 4 files changed, 43 insertions(+), 40 deletions(-)[+] [-] Lib/datetime.py 50 Lib/test/datetimetester.py 12 Lib/test/test_time.py 13 Python/pytime.c 8

line wrap: on

line diff

--- a/Lib/datetime.py +++ b/Lib/datetime.py @@ -1374,6 +1374,26 @@ class datetime(date): return self._tzinfo @classmethod

+

+

+

@@ -1381,21 +1401,7 @@ class datetime(date): """ _check_tzinfo_arg(tz)

-

-

@@ -1403,19 +1409,7 @@ class datetime(date): @classmethod def utcfromtimestamp(cls, t): """Construct a naive UTC datetime from a POSIX timestamp."""

-

@classmethod def now(cls, tz=None):

--- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -668,6 +668,8 @@ class TestTimeDelta(HarmlessMixedCompari eq(td(milliseconds=-0.6/1000), td(microseconds=-1)) eq(td(seconds=0.5/106), td(microseconds=1)) eq(td(seconds=-0.5/106), td(microseconds=-1))

# Rounding due to contributions from more than one field. us_per_hour = 3600e6 @@ -1842,8 +1844,8 @@ class TestDateTime(TestDate): 18000 + 3600 + 260 + 3 + 41e-6) def test_microsecond_rounding(self):

@@ -1874,6 +1876,12 @@ class TestDateTime(TestDate): t = fts(0.9999999) self.assertEqual(t.second, 1) self.assertEqual(t.microsecond, 0)

def test_insane_fromtimestamp(self): # It's possible that some platform maps time_t to double,

--- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -655,7 +655,7 @@ class TestPytime(unittest.TestCase): pytime_object_to_time_t, invalid, rnd) @support.cpython_only

# Conversion giving the same result for all rounding methods @@ -666,7 +666,7 @@ class TestPytime(unittest.TestCase): (-1, (-1, 0)), # float

@@ -693,7 +693,7 @@ class TestPytime(unittest.TestCase): (1.1234567890, (1, 123456789), FLOOR), (1.1234567899, (1, 123456789), FLOOR),

@@ -1155,7 +1155,7 @@ class TestOldPyTime(unittest.TestCase): self.assertRaises(OverflowError, pytime_object_to_time_t, invalid, rnd)

# Conversion giving the same result for all rounding methods @@ -1167,7 +1167,8 @@ class TestOldPyTime(unittest.TestCase): # float (-1.0, (-1, 0)),

@@ -1225,7 +1226,7 @@ class TestOldPyTime(unittest.TestCase): (-1.0, (-1, 0)), (-1e-9, (-1, 999999999)), (1e-9, (0, 1)),

--- a/Python/pytime.c +++ b/Python/pytime.c @@ -82,10 +82,6 @@ static int volatile double floatpart; floatpart = modf(d, &intpart);

floatpart *= denominator; if (round == _PyTime_ROUND_HALF_UP) @@ -98,6 +94,10 @@ static int floatpart -= denominator; intpart += 1.0; }