diff --git a/src/main/python/ttconv/time_code.py b/src/main/python/ttconv/time_code.py index 6f6915b5..5915b411 100644 --- a/src/main/python/ttconv/time_code.py +++ b/src/main/python/ttconv/time_code.py @@ -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) diff --git a/src/test/python/test_time_code.py b/src/test/python/test_time_code.py index 7f6ffd86..64068514 100644 --- a/src/test/python/test_time_code.py +++ b/src/test/python/test_time_code.py @@ -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() diff --git a/src/test/python/test_vtt_writer.py b/src/test/python/test_vtt_writer.py index 9f07163d..8aec6d67 100644 --- a/src/test/python/test_vtt_writer.py +++ b/src/test/python/test_vtt_writer.py @@ -131,7 +131,7 @@ def test_position(self): expected_vtt="""WEBVTT 1 -00:00:03.501 --> 00:00:12.000 line:90%,end +00:00:03.500 --> 00:00:12.000 line:90%,end Only one or two short samples are needed to make sure the conversion basically works @@ -177,7 +177,7 @@ def test_align(self): expected_vtt="""WEBVTT 1 -00:00:03.501 --> 00:00:12.000 align:center +00:00:03.500 --> 00:00:12.000 align:center Only one or two short samples are needed to make sure the conversion basically works @@ -217,7 +217,7 @@ def test_cue_id(self): expected_vtt="""WEBVTT -00:00:03.501 --> 00:00:12.000 +00:00:03.500 --> 00:00:12.000 Only one or two short samples are needed to make sure the conversion basically works