mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
time.ctime(), gmtime(), time.localtime(), datetime.date.fromtimestamp(), datetime.datetime.fromtimestamp() and datetime.datetime.utcfromtimestamp() now raises an OverflowError, instead of a ValueError, if the timestamp does not fit in time_t. datetime.datetime.fromtimestamp() and datetime.datetime.utcfromtimestamp() now round microseconds towards zero instead of rounding to nearest with ties going away from zero.
This commit is contained in:
parent
3cac309939
commit
5d272cc6a2
16 changed files with 278 additions and 222 deletions
|
@ -977,7 +977,7 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
|
|||
# exempt such platforms (provided they return reasonable
|
||||
# results!).
|
||||
for insane in -1e200, 1e200:
|
||||
self.assertRaises(ValueError, self.theclass.fromtimestamp,
|
||||
self.assertRaises(OverflowError, self.theclass.fromtimestamp,
|
||||
insane)
|
||||
|
||||
def test_today(self):
|
||||
|
@ -1736,12 +1736,32 @@ class TestDateTime(TestDate):
|
|||
self.verify_field_equality(expected, got)
|
||||
|
||||
def test_microsecond_rounding(self):
|
||||
# Test whether fromtimestamp "rounds up" floats that are less
|
||||
# than 1/2 microsecond smaller than an integer.
|
||||
for fts in [self.theclass.fromtimestamp,
|
||||
self.theclass.utcfromtimestamp]:
|
||||
self.assertEqual(fts(0.9999999), fts(1))
|
||||
self.assertEqual(fts(0.99999949).microsecond, 999999)
|
||||
zero = fts(0)
|
||||
self.assertEqual(zero.second, 0)
|
||||
self.assertEqual(zero.microsecond, 0)
|
||||
minus_one = fts(-1e-6)
|
||||
self.assertEqual(minus_one.second, 59)
|
||||
self.assertEqual(minus_one.microsecond, 999999)
|
||||
|
||||
t = fts(-1e-8)
|
||||
self.assertEqual(t, minus_one)
|
||||
t = fts(-9e-7)
|
||||
self.assertEqual(t, minus_one)
|
||||
t = fts(-1e-7)
|
||||
self.assertEqual(t, minus_one)
|
||||
|
||||
t = fts(1e-7)
|
||||
self.assertEqual(t, zero)
|
||||
t = fts(9e-7)
|
||||
self.assertEqual(t, zero)
|
||||
t = fts(0.99999949)
|
||||
self.assertEqual(t.second, 0)
|
||||
self.assertEqual(t.microsecond, 999999)
|
||||
t = fts(0.9999999)
|
||||
self.assertEqual(t.second, 0)
|
||||
self.assertEqual(t.microsecond, 999999)
|
||||
|
||||
def test_insane_fromtimestamp(self):
|
||||
# It's possible that some platform maps time_t to double,
|
||||
|
@ -1749,7 +1769,7 @@ class TestDateTime(TestDate):
|
|||
# exempt such platforms (provided they return reasonable
|
||||
# results!).
|
||||
for insane in -1e200, 1e200:
|
||||
self.assertRaises(ValueError, self.theclass.fromtimestamp,
|
||||
self.assertRaises(OverflowError, self.theclass.fromtimestamp,
|
||||
insane)
|
||||
|
||||
def test_insane_utcfromtimestamp(self):
|
||||
|
@ -1758,7 +1778,7 @@ class TestDateTime(TestDate):
|
|||
# exempt such platforms (provided they return reasonable
|
||||
# results!).
|
||||
for insane in -1e200, 1e200:
|
||||
self.assertRaises(ValueError, self.theclass.utcfromtimestamp,
|
||||
self.assertRaises(OverflowError, self.theclass.utcfromtimestamp,
|
||||
insane)
|
||||
@unittest.skipIf(sys.platform == "win32", "Windows doesn't accept negative timestamps")
|
||||
def test_negative_float_fromtimestamp(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue