Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions src/timecode/timecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,10 @@ def to_systemtime(self, as_float: bool = False) -> str | float: # type:ignore
return self.float - (1e-3) if as_float else str(self)

hh, mm, ss, ff = self.frames_to_tc(self.frames + 1, skip_rollover=True)
framerate = (
float(self.framerate) if self._ntsc_framerate else self._int_framerate
)
ms = ff / framerate
# System time is in the integer frame grid, so always divide by _int_framerate.
# Using float(self.framerate) for NTSC rates gives wrong sub-second values
# because the stored string (e.g. "47.95") loses precision vs the true rate.
ms = ff / self._int_framerate
if as_float:
return hh * 3600 + mm * 60 + ss + ms
return f"{hh:02d}:{mm:02d}:{ss:02d}.{round(ms * 1000):03d}"
Expand Down
15 changes: 15 additions & 0 deletions tests/test_timecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1764,3 +1764,18 @@ def test_generalized_ntsc_rational_formats(rational_str, int_framerate, is_drop)
assert tc._ntsc_framerate is True
assert tc._int_framerate == int_framerate
assert tc.drop_frame is is_drop


def test_systemtime_subframe_ntsc():
"""to_systemtime uses integer framerate for sub-second calculation.

Non-standard NTSC rates (e.g. 48000/1001) are stored as a rounded float
string ("47.95"), so dividing by float(framerate) gives wrong milliseconds
for frames mid-second. The integer framerate (48) must be used instead.
"""
from fractions import Fraction
# Frame 24 of 48000/1001 fps is exactly half a second in system time (24/48)
tc = Timecode(Fraction(48000, 1001), frames=24)
assert tc._ntsc_framerate is True
assert tc._int_framerate == 48
assert tc.to_systemtime() == "00:00:00.500"
Loading