I'd like to convert a number of milliseconds to a formatted string using Python's datetime.
def milliseconds_to_str(t):
"""Converts the number of milliseconds to HH:MM:SS,ms time"""
return str(datetime.timedelta(milliseconds=t)).replace('.', ',')[:-3]
The above works extremely well, for example:
> milliseconds_to_str(8712313): '2:25:12,313'
The problem arises when the number of milliseconds is an even multiple of 1000, for example:
milliseconds_to_srt_time(1000) > 0:00 (Should be 0:00:01,000)
milliseconds_to_srt_time(1000000) > 0:16 (Should be 0:16:00,000)
milliseconds_to_srt_time(4000400) > 1:06:40,400 (GOOD)
milliseconds_to_srt_time(80808231) > 22:26:48,231 (GOOD)
milliseconds_to_srt_time(80808000) > 22:26 (Should be 22:26:00,000)
How can I avoid this rounding?