1616TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1 ) - 1
1717TIME_MINYEAR = - TIME_MAXYEAR - 1
1818
19+ SEC_TO_NS = 10 ** 9
1920
2021class _PyTime (enum .IntEnum ):
2122 # Round towards zero
@@ -770,9 +771,7 @@ def test_short_times(self):
770771@support .cpython_only
771772class TestPyTime_t (unittest .TestCase ):
772773 def test_FromSecondsObject (self ):
773- from _testcapi import pytime_fromsecondsobject
774- SEC_TO_NS = 10 ** 9
775- MAX_SEC = 2 ** 63 // 10 ** 9
774+ from _testcapi import PyTime_FromSecondsObject
776775
777776 # Conversion giving the same result for all rounding methods
778777 for rnd in ALL_ROUNDING_METHODS :
@@ -811,21 +810,21 @@ def test_FromSecondsObject(self):
811810 (2 ** 25 , 33554432000000000 ),
812811 (2 ** 25 + 1e-9 , 33554432000000000 ),
813812
814- # close to 2^63 nanoseconds
813+ # close to 2^63 nanoseconds (_PyTime_t limit)
815814 (9223372036 , 9223372036 * SEC_TO_NS ),
816815 (9223372036.0 , 9223372036 * SEC_TO_NS ),
817816 (- 9223372036 , - 9223372036 * SEC_TO_NS ),
818817 (- 9223372036.0 , - 9223372036 * SEC_TO_NS ),
819818 ):
820819 with self .subTest (obj = obj , round = rnd , timestamp = ts ):
821- self .assertEqual (pytime_fromsecondsobject (obj , rnd ), ts )
820+ self .assertEqual (PyTime_FromSecondsObject (obj , rnd ), ts )
822821
823822 with self .subTest (round = rnd ):
824823 with self .assertRaises (OverflowError ):
825- pytime_fromsecondsobject (9223372037 , rnd )
826- pytime_fromsecondsobject (9223372037.0 , rnd )
827- pytime_fromsecondsobject (- 9223372037 , rnd )
828- pytime_fromsecondsobject (- 9223372037.0 , rnd )
824+ PyTime_FromSecondsObject (9223372037 , rnd )
825+ PyTime_FromSecondsObject (9223372037.0 , rnd )
826+ PyTime_FromSecondsObject (- 9223372037 , rnd )
827+ PyTime_FromSecondsObject (- 9223372037.0 , rnd )
829828
830829 # Conversion giving different results depending on the rounding method
831830 UP = _PyTime .ROUND_UP
@@ -850,7 +849,52 @@ def test_FromSecondsObject(self):
850849 (- 0.9999999999 , - 1000000000 , UP ),
851850 ):
852851 with self .subTest (obj = obj , round = rnd , timestamp = ts ):
853- self .assertEqual (pytime_fromsecondsobject (obj , rnd ), ts )
852+ self .assertEqual (PyTime_FromSecondsObject (obj , rnd ), ts )
853+
854+ def test_AsSecondsDouble (self ):
855+ from _testcapi import PyTime_AsSecondsDouble
856+
857+ for nanoseconds , seconds in (
858+ # near 1 nanosecond
859+ ( 0 , 0.0 ),
860+ ( 1 , 1e-9 ),
861+ (- 1 , - 1e-9 ),
862+
863+ # near 1 second
864+ (SEC_TO_NS + 1 , 1.0 + 1e-9 ),
865+ (SEC_TO_NS , 1.0 ),
866+ (SEC_TO_NS - 1 , 1.0 - 1e-9 ),
867+
868+ # a few seconds
869+ (123 * SEC_TO_NS , 123.0 ),
870+ (- 567 * SEC_TO_NS , - 567.0 ),
871+
872+ # nanosecond are kept for value <= 2^23 seconds
873+ (4194303999999999 , 2 ** 22 - 1e-9 ),
874+ (4194304000000000 , 2 ** 22 ),
875+ (4194304000000001 , 2 ** 22 + 1e-9 ),
876+
877+ # start loosing precision for value > 2^23 seconds
878+ (8388608000000002 , 2 ** 23 + 1e-9 ),
879+
880+ # nanoseconds are lost for value > 2^23 seconds
881+ (16777215999999998 , 2 ** 24 - 1e-9 ),
882+ (16777215999999999 , 2 ** 24 - 1e-9 ),
883+ (16777216000000000 , 2 ** 24 ),
884+ (16777216000000001 , 2 ** 24 ),
885+ (16777216000000002 , 2 ** 24 + 2e-9 ),
886+
887+ (33554432000000000 , 2 ** 25 ),
888+ (33554432000000002 , 2 ** 25 ),
889+ (33554432000000004 , 2 ** 25 + 4e-9 ),
890+
891+ # close to 2^63 nanoseconds (_PyTime_t limit)
892+ (9223372036 * SEC_TO_NS , 9223372036.0 ),
893+ (- 9223372036 * SEC_TO_NS , - 9223372036.0 ),
894+ ):
895+ with self .subTest (nanoseconds = nanoseconds , seconds = seconds ):
896+ self .assertEqual (PyTime_AsSecondsDouble (nanoseconds ),
897+ seconds )
854898
855899
856900if __name__ == "__main__" :
0 commit comments