Browse Source
media time pattern reformat
media time pattern reformat
new utils file for handling formatting methods update podcast and video time formats with new helper methodsmaster
3 changed files with 40 additions and 52 deletions
@ -0,0 +1,23 @@ |
|||
# backend/utils/formatters.py |
|||
|
|||
def format_media_time(time_obj): |
|||
"""Formats a datetime.time object to MM:SS or HH:MM:SS""" |
|||
if not time_obj: |
|||
return None |
|||
if time_obj.hour == 0: |
|||
return time_obj.strftime('%M:%S') |
|||
return time_obj.strftime('%H:%M:%S') |
|||
|
|||
def format_duration(timedelta_obj): |
|||
"""Formats a datetime.timedelta object to MM:SS or HH:MM:SS""" |
|||
if not timedelta_obj: |
|||
return "00:00" |
|||
|
|||
total_seconds = int(timedelta_obj.total_seconds()) |
|||
hours = total_seconds // 3600 |
|||
minutes = (total_seconds % 3600) // 60 |
|||
seconds = total_seconds % 60 |
|||
|
|||
if hours > 0: |
|||
return f"{hours:02d}:{minutes:02d}:{seconds:02d}" |
|||
return f"{minutes:02d}:{seconds:02d}" |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue