def test_FromSecondsObject(self): from _testcapi import PyTime_FromSecondsObject self.check_int_rounding(PyTime_FromSecondsObject, lambda secs: secs * SEC_TO_NS) self.check_float_rounding( PyTime_FromSecondsObject, lambda ns: self.decimal_round(ns * SEC_TO_NS)) # test nan for time_rnd, _ in ROUNDING_MODES: with self.assertRaises(ValueError): PyTime_FromSecondsObject(float('nan'), time_rnd)
def test_FromSecondsObject(self): from _testcapi import PyTime_FromSecondsObject # Conversion giving the same result for all rounding methods for rnd in ALL_ROUNDING_METHODS: for obj, ts in ( # integers (0, 0), (1, SEC_TO_NS), (-3, -3 * SEC_TO_NS), # float: subseconds (0.0, 0), (1e-9, 1), (1e-6, 10**3), (1e-3, 10**6), # float: seconds (2.0, 2 * SEC_TO_NS), (123.0, 123 * SEC_TO_NS), (-7.0, -7 * SEC_TO_NS), # nanosecond are kept for value <= 2^23 seconds (2**22 - 1e-9, 4194303999999999), (2**22, 4194304000000000), (2**22 + 1e-9, 4194304000000001), (2**23 - 1e-9, 8388607999999999), (2**23, 8388608000000000), # start losing precision for value > 2^23 seconds (2**23 + 1e-9, 8388608000000002), # nanoseconds are lost for value > 2^23 seconds (2**24 - 1e-9, 16777215999999998), (2**24, 16777216000000000), (2**24 + 1e-9, 16777216000000000), (2**25 - 1e-9, 33554432000000000), (2**25, 33554432000000000), (2**25 + 1e-9, 33554432000000000), # close to 2^63 nanoseconds (_PyTime_t limit) (9223372036, 9223372036 * SEC_TO_NS), (9223372036.0, 9223372036 * SEC_TO_NS), (-9223372036, -9223372036 * SEC_TO_NS), (-9223372036.0, -9223372036 * SEC_TO_NS), ): with self.subTest(obj=obj, round=rnd, timestamp=ts): self.assertEqual(PyTime_FromSecondsObject(obj, rnd), ts) with self.subTest(round=rnd): with self.assertRaises(OverflowError): PyTime_FromSecondsObject(9223372037, rnd) PyTime_FromSecondsObject(9223372037.0, rnd) PyTime_FromSecondsObject(-9223372037, rnd) PyTime_FromSecondsObject(-9223372037.0, rnd) # Conversion giving different results depending on the rounding method FLOOR = _PyTime.ROUND_FLOOR CEILING = _PyTime.ROUND_CEILING for obj, ts, rnd in ( # close to zero (1e-10, 0, FLOOR), (1e-10, 1, CEILING), (-1e-10, -1, FLOOR), (-1e-10, 0, CEILING), # test rounding of the last nanosecond (1.1234567899, 1123456789, FLOOR), (1.1234567899, 1123456790, CEILING), (-1.1234567899, -1123456790, FLOOR), (-1.1234567899, -1123456789, CEILING), # close to 1 second (0.9999999999, 999999999, FLOOR), (0.9999999999, 1000000000, CEILING), (-0.9999999999, -1000000000, FLOOR), (-0.9999999999, -999999999, CEILING), ): with self.subTest(obj=obj, round=rnd, timestamp=ts): self.assertEqual(PyTime_FromSecondsObject(obj, rnd), ts)