@@ -1989,6 +1989,47 @@ def test_microsecond_rounding(self):
19891989 self .assertEqual (t .second , 0 )
19901990 self .assertEqual (t .microsecond , 7812 )
19911991
1992+ def test_timestamp_limits (self ):
1993+ # minimum timestamp
1994+ min_dt = self .theclass .min .replace (tzinfo = timezone .utc )
1995+ min_ts = min_dt .timestamp ()
1996+ try :
1997+ # date 0001-01-01 00:00:00+00:00: timestamp=-62135596800
1998+ self .assertEqual (self .theclass .fromtimestamp (min_ts , tz = timezone .utc ),
1999+ min_dt )
2000+ except (OverflowError , OSError ) as exc :
2001+ # the date 0001-01-01 doesn't fit into 32-bit time_t,
2002+ # or platform doesn't support such very old date
2003+ self .skipTest (str (exc ))
2004+
2005+ # maximum timestamp: set seconds to zero to avoid rounding issues
2006+ max_dt = self .theclass .max .replace (tzinfo = timezone .utc ,
2007+ second = 0 , microsecond = 0 )
2008+ max_ts = max_dt .timestamp ()
2009+ # date 9999-12-31 23:59:00+00:00: timestamp 253402300740
2010+ self .assertEqual (self .theclass .fromtimestamp (max_ts , tz = timezone .utc ),
2011+ max_dt )
2012+
2013+ # number of seconds greater than 1 year: make sure that the new date
2014+ # is not valid in datetime.datetime limits
2015+ delta = 3600 * 24 * 400
2016+
2017+ # too small
2018+ ts = min_ts - delta
2019+ # converting a Python int to C time_t can raise a OverflowError,
2020+ # especially on 32-bit platforms.
2021+ with self .assertRaises ((ValueError , OverflowError )):
2022+ self .theclass .fromtimestamp (ts )
2023+ with self .assertRaises ((ValueError , OverflowError )):
2024+ self .theclass .utcfromtimestamp (ts )
2025+
2026+ # too big
2027+ ts = max_dt .timestamp () + delta
2028+ with self .assertRaises ((ValueError , OverflowError )):
2029+ self .theclass .fromtimestamp (ts )
2030+ with self .assertRaises ((ValueError , OverflowError )):
2031+ self .theclass .utcfromtimestamp (ts )
2032+
19922033 def test_insane_fromtimestamp (self ):
19932034 # It's possible that some platform maps time_t to double,
19942035 # and that this test will fail there. This test should
0 commit comments