Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes rounding in seconds to tc conversion #433

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading