Add All existing
This commit is contained in:
23
time_add.py
Normal file
23
time_add.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import re
|
||||
from datetime import timedelta
|
||||
|
||||
def parse_timecode(timecode):
|
||||
parts = list(map(int, timecode.split(':')))
|
||||
if len(parts) == 2: # mm:ss format
|
||||
return timedelta(minutes=parts[0], seconds=parts[1])
|
||||
elif len(parts) == 3: # hh:mm:ss format
|
||||
return timedelta(hours=parts[0], minutes=parts[1], seconds=parts[2])
|
||||
else:
|
||||
raise ValueError(f"Invalid timecode format: {timecode}")
|
||||
|
||||
def add_timecodes(*timecodes):
|
||||
total_time = sum((parse_timecode(tc) for tc in timecodes), timedelta())
|
||||
hours, remainder = divmod(total_time.seconds, 3600)
|
||||
minutes, seconds = divmod(remainder, 60)
|
||||
hours += total_time.days * 24 # Account for days if needed
|
||||
return f"{hours:02}:{minutes:02}:{seconds:02}"
|
||||
|
||||
if __name__ == "__main__":
|
||||
timecodes = input("Enter timecodes separated by spaces: ").split()
|
||||
total = add_timecodes(*timecodes)
|
||||
print("Total time:", total)
|
||||
Reference in New Issue
Block a user