jpdiazpardo commited on
Commit
2809c68
1 Parent(s): 5440420

Create timestamp.py

Browse files
Files changed (1) hide show
  1. timestamp.py +18 -0
timestamp.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def format_timestamp(seconds: float, always_include_hours: bool = False, decimal_marker: str = "."):
2
+ if seconds is not None:
3
+ milliseconds = round(seconds * 1000.0)
4
+
5
+ hours = milliseconds // 3_600_000
6
+ milliseconds -= hours * 3_600_000
7
+
8
+ minutes = milliseconds // 60_000
9
+ milliseconds -= minutes * 60_000
10
+
11
+ seconds = milliseconds // 1_000
12
+ milliseconds -= seconds * 1_000
13
+
14
+ hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else ""
15
+ return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
16
+ else:
17
+ # we have a malformed timestamp so just return it as is
18
+ return seconds