@@ -277,6 +277,22 @@ def test_from_float(self):
277277 self .assertEqual (r , case [1 ],
278278 msg = "Timestamp.from_float{!r} == {!r}, expected {!r}" .format (case [0 ], r , case [1 ]))
279279
280+ def test_to_float (self ):
281+ """This tests that timestamps can be created from a float."""
282+ cases = [
283+ ((float (1.0 )), Timestamp (1 , 0 )),
284+ ((float (1_000_000_000 )), Timestamp (1_000_000_000 , 0 )),
285+ ((float (2.76 )), Timestamp (2 , 760_000_000 )),
286+ ((float (- 3.14 )), Timestamp (3 , 140_000_000 , - 1 )),
287+ ((float (0.02 )), Timestamp (0 , 20_000_000 ))
288+ ]
289+
290+ for case in cases :
291+ with self .subTest (case = case ):
292+ r = case [1 ].to_float ()
293+ self .assertEqual (r , case [1 ],
294+ msg = "{!r},to_float() == {!r}, expected {!r}" .format (case [1 ], r , case [0 ]))
295+
280296 def test_set_value (self ):
281297 """This tests that timestamps cannot have their value set."""
282298 tests_ts = [
@@ -879,3 +895,23 @@ def test_get_leap_seconds(self):
879895
880896 for t in tests :
881897 self .assertEqual (t [0 ].get_leap_seconds (), t [1 ])
898+
899+ def test_to_unix (self ):
900+ tests = [
901+ (Timestamp (63072008 , 999999999 ), (63072008 , 999999999 , False )), # 0 leap seconds
902+ (Timestamp (63072009 , 0 ), (63071999 , 0 , True )), # 10 leap seconds at leap
903+ (Timestamp (1512491629 , 0 ), (1512491592 , 0 , False )), # 37 leap seconds
904+ ]
905+
906+ for t in tests :
907+ self .assertEqual (t [0 ].to_unix (), t [1 ])
908+
909+ def test_to_unix_float (self ):
910+ tests = [
911+ (Timestamp (63072008 , 999999999 ), 63072008 + 999999999 / 1000000000 ),
912+ (Timestamp (63072009 , 0 ), 63071999 ),
913+ (Timestamp (1000 , 0 , - 1 ), - 1000 )
914+ ]
915+
916+ for t in tests :
917+ self .assertEqual (t [0 ].to_unix_float (), t [1 ])
0 commit comments