Skip to content

Instantly share code, notes, and snippets.

@fauxneticien
Created October 2, 2022 23:30
Show Gist options
  • Save fauxneticien/5b0067a7c2540055ce49c5be25e8f75a to your computer and use it in GitHub Desktop.
Save fauxneticien/5b0067a7c2540055ce49c5be25e8f75a to your computer and use it in GitHub Desktop.
Convert milliseconds to hours-minutes-seconds string format to use within identifiers
def ms_to_hms(start_ms: int) -> str:
import math
"""
Convert milliseconds to hours-minutes-seconds string format to use within identifiers
e.g. 3990060 ms -> '01h06m30.060'
"""
s, ms = divmod(start_ms, 1000)
m, s = divmod(s, 60)
h, m = divmod(m, 60)
ms = str(round(ms)).zfill(3)
s = str(math.floor(s)).zfill(2)
m = str(math.floor(m)).zfill(2)
h = str(math.floor(h)).zfill(2)
return f"{h}h{m}m{s}.{ms}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment