Lukáš Kucharczyk 34ce1e9b05
All checks were successful
continuous-integration/drone/push Build is passing
Set up tests, add tests for common.util.time, add %d
2023-01-05 15:18:57 +01:00

40 lines
1.1 KiB
Python

from datetime import datetime, timedelta
from django.conf import settings
from zoneinfo import ZoneInfo
import re
def now() -> datetime:
return datetime.now(ZoneInfo(settings.TIME_ZONE))
def format_duration(
duration: timedelta, format_string: str = "%H hours %m minutes"
) -> str:
"""
Format timedelta into the specified format_string.
Valid format variables:
- %H hours
- %m minutes
- %s seconds
- %r total seconds
"""
minute_seconds = 60
hour_seconds = 60 * minute_seconds
day_seconds = 24 * hour_seconds
seconds_total = int(duration.total_seconds())
days, remainder = divmod(seconds_total, day_seconds)
hours, remainder = divmod(remainder, hour_seconds)
minutes, seconds = divmod(remainder, minute_seconds)
literals = {
"%d": str(days),
"%H": str(hours),
"%m": str(minutes),
"%s": str(seconds),
"%r": str(seconds_total),
}
formatted_string = format_string
for pattern, replacement in literals.items():
formatted_string = re.sub(pattern, replacement, formatted_string)
return formatted_string