diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-02-10 10:34:02 +0100 |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2017-02-10 10:34:02 +0100 |
commit | efb9e6050c840ddb095b372abd5a6d04c73aff2b (patch) | |
tree | 9bf843ebe6246952c1dcf621af7a5cf506409c89 /Lib/test | |
parent | c7a3f78cc193bfa7fc874f49ec2e0799b32da817 (diff) | |
download | cpython-efb9e6050c840ddb095b372abd5a6d04c73aff2b.tar.gz |
Fix datetime.fromtimestamp(): check bounds
Issue #29100: Fix datetime.fromtimestamp() regression introduced in Python
3.6.0: check minimum and maximum years.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/datetimetester.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index d65186db0c..7feabced94 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -1989,6 +1989,42 @@ class TestDateTime(TestDate): self.assertEqual(t.second, 0) self.assertEqual(t.microsecond, 7812) + def test_timestamp_limits(self): + # minimum timestamp + min_dt = self.theclass.min.replace(tzinfo=timezone.utc) + min_ts = min_dt.timestamp() + # date 0001-01-01 00:00:00+00:00: timestamp=-62135596800 + self.assertEqual(self.theclass.fromtimestamp(min_ts, tz=timezone.utc), + min_dt) + + # maximum timestamp: set seconds to zero to avoid rounding issues + max_dt = self.theclass.max.replace(tzinfo=timezone.utc, + second=0, microsecond=0) + max_ts = max_dt.timestamp() + # date 9999-12-31 23:59:00+00:00: timestamp 253402300740 + self.assertEqual(self.theclass.fromtimestamp(max_ts, tz=timezone.utc), + max_dt) + + # number of seconds greater than 1 year: make sure that the new date + # is not valid in datetime.datetime limits + delta = 3600 * 24 * 400 + + # too small + ts = min_ts - delta + # converting a Python int to C time_t can raise a OverflowError, + # especially on 32-bit platforms. + with self.assertRaises((ValueError, OverflowError)): + self.theclass.fromtimestamp(ts) + with self.assertRaises((ValueError, OverflowError)): + self.theclass.utcfromtimestamp(ts) + + # too big + ts = max_dt.timestamp() + delta + with self.assertRaises((ValueError, OverflowError)): + self.theclass.fromtimestamp(ts) + with self.assertRaises((ValueError, OverflowError)): + self.theclass.utcfromtimestamp(ts) + def test_insane_fromtimestamp(self): # It's possible that some platform maps time_t to double, # and that this test will fail there. This test should |