You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
726 B
23 lines
726 B
# 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}"
|