Skip to content

Commit

Permalink
Fixes rounding in seconds to tc conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
palemieux committed Oct 30, 2024
1 parent c0f6929 commit ccb3985
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/python/ttconv/time_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ def parse(time_code: str) -> ClockTime:

@staticmethod
def from_seconds(seconds: Union[float, Fraction]) -> ClockTime:
"""Creates a time code from time offset in seconds"""
"""Creates a time code from time offset in seconds (rounds to the closest millisecond)"""

if seconds < 0:
raise ValueError("Seconds must not be less than zero")

seconds = float(seconds)
seconds = round(seconds, 3)

h = floor(seconds / 3600)
m = floor(seconds / 60 % 60)
Expand Down
17 changes: 17 additions & 0 deletions src/test/python/test_time_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,22 @@ def test_time_code(self):
self.assertEqual("24:00:01,500", str(time_code))
self.assertEqual(seconds, time_code.to_seconds())

def test_from_seconds_rounding_up(self):
seconds = Fraction(41039999,7500)
time_code = ClockTime.from_seconds(seconds)
self.assertEqual(time_code.get_hours(), 1)
self.assertEqual(time_code.get_minutes(), 31)
self.assertEqual(time_code.get_seconds(), 12)
self.assertEqual(time_code.get_milliseconds(), 0)


def test_from_seconds_rounding_down(self):
seconds = 5471.9994
time_code = ClockTime.from_seconds(seconds)
self.assertEqual(time_code.get_hours(), 1)
self.assertEqual(time_code.get_minutes(), 31)
self.assertEqual(time_code.get_seconds(), 11)
self.assertEqual(time_code.get_milliseconds(), 999)

if __name__ == '__main__':
unittest.main()

0 comments on commit ccb3985

Please sign in to comment.