NDOJ/judge/utils/timedelta.py

122 lines
3.9 KiB
Python
Raw Permalink Normal View History

2020-01-21 06:35:58 +00:00
import datetime
from django.utils.translation import npgettext, pgettext, ungettext
2022-05-14 17:57:27 +00:00
def nice_repr(timedelta, display="long", sep=", "):
2020-01-21 06:35:58 +00:00
"""
Turns a datetime.timedelta object into a nice string repr.
display can be 'minimal', 'short' or 'long' [default].
>>> from datetime import timedelta as td
>>> nice_repr(td(days=1, hours=2, minutes=3, seconds=4))
'1 day, 2 hours, 3 minutes, 4 seconds'
>>> nice_repr(td(days=1, seconds=1), 'minimal')
'1d, 1s'
"""
2022-05-14 17:57:27 +00:00
assert isinstance(
timedelta, datetime.timedelta
), "First argument must be a timedelta."
2020-01-21 06:35:58 +00:00
result = []
weeks = timedelta.days // 7
days = timedelta.days % 7
hours = timedelta.seconds // 3600
minutes = (timedelta.seconds % 3600) // 60
seconds = timedelta.seconds % 60
2022-05-14 17:57:27 +00:00
if display == "simple-no-seconds":
2020-01-21 06:35:58 +00:00
days += weeks * 7
if days:
if hours or minutes:
2022-05-14 17:57:27 +00:00
return "%d day%s %d:%02d" % (days, "s"[days == 1 :], hours, minutes)
return "%d day%s" % (days, "s"[days == 1 :])
2020-01-21 06:35:58 +00:00
else:
2022-05-14 17:57:27 +00:00
return "%d:%02d" % (hours, minutes)
elif display == "sql":
2020-01-21 06:35:58 +00:00
days += weeks * 7
2022-05-14 17:57:27 +00:00
return "%d %02d:%02d:%02d" % (days, hours, minutes, seconds)
elif display == "simple":
2020-01-21 06:35:58 +00:00
days += weeks * 7
if days:
2022-05-14 17:57:27 +00:00
return "%d day%s %02d:%02d:%02d" % (
days,
"s"[days == 1 :],
hours,
minutes,
seconds,
)
2020-01-21 06:35:58 +00:00
else:
2022-05-14 17:57:27 +00:00
return "%02d:%02d:%02d" % (hours, minutes, seconds)
elif display == "localized":
2020-01-21 06:35:58 +00:00
days += weeks * 7
if days:
2022-05-14 17:57:27 +00:00
return (
npgettext(
"time format with day", "%d day %h:%m:%s", "%d days %h:%m:%s", days
)
.replace("%d", str(days))
.replace("%h", "%02d" % hours)
.replace("%m", "%02d" % minutes)
.replace("%s", "%02d" % seconds)
)
2020-01-21 06:35:58 +00:00
else:
2022-05-14 17:57:27 +00:00
return (
pgettext("time format without day", "%h:%m:%s")
.replace("%h", "%02d" % hours)
.replace("%m", "%02d" % minutes)
.replace("%s", "%02d" % seconds)
)
elif display == "localized-no-seconds":
2020-01-21 06:35:58 +00:00
days += weeks * 7
if days:
if hours or minutes:
2022-05-14 17:57:27 +00:00
return (
npgettext(
"time format no seconds with day",
"%d day %h:%m",
"%d days %h:%m",
days,
)
.replace("%d", str(days))
.replace("%h", "%02d" % hours)
.replace("%m", "%02d" % minutes)
)
return ungettext("%d day", "%d days", days) % days
2020-01-21 06:35:58 +00:00
else:
2022-05-14 17:57:27 +00:00
return (
pgettext("hours and minutes", "%h:%m")
.replace("%h", "%02d" % hours)
.replace("%m", "%02d" % minutes)
)
elif display == "concise":
2020-01-21 06:35:58 +00:00
days += weeks * 7
if days:
2022-05-14 17:57:27 +00:00
return "%dd %02d:%02d:%02d" % (days, hours, minutes, seconds)
2020-01-21 06:35:58 +00:00
else:
2022-05-14 17:57:27 +00:00
return "%02d:%02d:%02d" % (hours, minutes, seconds)
elif display == "noday":
2020-01-21 06:35:58 +00:00
days += weeks * 7
hours += days * 24
2022-05-14 17:57:27 +00:00
return "%02d:%02d:%02d" % (hours, minutes, seconds)
elif display == "minimal":
words = ["w", "d", "h", "m", "s"]
elif display == "short":
words = [" wks", " days", " hrs", " min", " sec"]
2020-01-21 06:35:58 +00:00
else:
2022-05-14 17:57:27 +00:00
words = [" weeks", " days", " hours", " minutes", " seconds"]
2020-01-21 06:35:58 +00:00
values = [weeks, days, hours, minutes, seconds]
for i in range(len(values)):
if values[i]:
if values[i] == 1 and len(words[i]) > 1:
2022-05-14 17:57:27 +00:00
result.append("%i%s" % (values[i], words[i].rstrip("s")))
2020-01-21 06:35:58 +00:00
else:
2022-05-14 17:57:27 +00:00
result.append("%i%s" % (values[i], words[i]))
2020-01-21 06:35:58 +00:00
return sep.join(result)