Skip to content

Commit 5afe484

Browse files
author
Philip de Nier
committed
Add Timestamp conversion to float and unix seconds float
sem-ver: feature
1 parent b6e3d24 commit 5afe484

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

mediatimestamp/immutable/timestamp.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,11 @@ def to_tai_sec_nsec(self) -> str:
405405
def to_tai_sec_frac(self, fixed_size: bool = False) -> str:
406406
return self.to_sec_frac(fixed_size=fixed_size)
407407

408+
def to_float(self) -> float:
409+
""" Convert to a floating point number of seconds
410+
"""
411+
return self._value / Timestamp.MAX_NANOSEC
412+
408413
def to_datetime(self) -> datetime:
409414
sec, nsec, leap = self.to_unix()
410415
microsecond = int(round(nsec/1000))
@@ -437,6 +442,15 @@ def to_utc(self) -> Tuple[int, int, bool]:
437442
"""
438443
return self.to_unix()
439444

445+
def to_unix_float(self) -> float:
446+
""" Convert to unix seconds since the epoch as a floating point number
447+
"""
448+
if self._value < 0:
449+
return self.to_float()
450+
else:
451+
(sec, ns, _) = self.to_unix()
452+
return sec + ns / Timestamp.MAX_NANOSEC
453+
440454
def to_iso8601_utc(self) -> str:
441455
""" Get printed representation in ISO8601 format (UTC)
442456
YYYY-MM-DDThh:mm:ss.s

tests/test_timestamp.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)