2017-08-13 15:59:14 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-05-26 04:03:12 +00:00
|
|
|
import random
|
|
|
|
|
2021-04-11 20:00:17 +00:00
|
|
|
from django.utils import timezone
|
2019-11-09 21:36:29 +00:00
|
|
|
from django.utils.translation import ngettext
|
2017-08-18 05:53:48 +00:00
|
|
|
|
2022-05-26 04:03:12 +00:00
|
|
|
random.seed()
|
|
|
|
|
|
|
|
COLORS = [
|
|
|
|
"#ff0000",
|
|
|
|
"#00ff00",
|
|
|
|
"#0000ff",
|
|
|
|
"#ff00ff",
|
|
|
|
"#ffff00",
|
|
|
|
"#00ffff",
|
|
|
|
"#ff7f7f",
|
|
|
|
"#7fff7f",
|
|
|
|
"#7f7fff",
|
|
|
|
"#ff7fff",
|
|
|
|
"#ffff7f",
|
|
|
|
"#7fffff",
|
|
|
|
"#7f0000",
|
|
|
|
"#007f00",
|
|
|
|
"#00007f",
|
|
|
|
"#7f007f",
|
|
|
|
"#7f7f00",
|
|
|
|
"#007f7f",
|
|
|
|
]
|
|
|
|
|
2017-08-13 15:59:14 +00:00
|
|
|
|
2022-02-10 00:00:30 +00:00
|
|
|
def duration_string(duration, precision="s"):
|
2017-09-13 18:07:51 +00:00
|
|
|
"""Format hours, minutes and seconds as a human-friendly string (e.g. "2
|
2017-10-30 18:26:49 +00:00
|
|
|
hours, 25 minutes, 31 seconds") with precision to h = hours, m = minutes or
|
|
|
|
s = seconds.
|
|
|
|
"""
|
2017-08-25 20:33:14 +00:00
|
|
|
h, m, s = duration_parts(duration)
|
|
|
|
|
2022-02-10 00:00:30 +00:00
|
|
|
duration = ""
|
2017-08-25 20:33:14 +00:00
|
|
|
if h > 0:
|
2022-02-10 00:00:30 +00:00
|
|
|
duration = ngettext("%(hours)s hour", "%(hours)s hours", h) % {"hours": h}
|
2024-02-07 08:34:00 +00:00
|
|
|
if m >= 0 and precision != "h":
|
2022-02-10 00:00:30 +00:00
|
|
|
if duration != "":
|
|
|
|
duration += ", "
|
|
|
|
duration += ngettext("%(minutes)s minute", "%(minutes)s minutes", m) % {
|
|
|
|
"minutes": m
|
|
|
|
}
|
|
|
|
if s > 0 and precision != "h" and precision != "m":
|
|
|
|
if duration != "":
|
|
|
|
duration += ", "
|
|
|
|
duration += ngettext("%(seconds)s second", "%(seconds)s seconds", s) % {
|
|
|
|
"seconds": s
|
2019-11-09 21:36:29 +00:00
|
|
|
}
|
2017-08-25 20:33:14 +00:00
|
|
|
|
|
|
|
return duration
|
|
|
|
|
|
|
|
|
|
|
|
def duration_parts(duration):
|
2022-02-10 00:00:30 +00:00
|
|
|
"""Get hours, minutes and seconds from a timedelta."""
|
2017-08-25 20:33:14 +00:00
|
|
|
if not isinstance(duration, timezone.timedelta):
|
2022-02-10 00:00:30 +00:00
|
|
|
raise TypeError("Duration provided must be a timedetla")
|
2017-08-25 20:33:14 +00:00
|
|
|
h, remainder = divmod(duration.seconds, 3600)
|
|
|
|
h += duration.days * 24
|
|
|
|
m, s = divmod(remainder, 60)
|
2017-09-25 15:39:08 +00:00
|
|
|
return h, m, s
|
2022-05-26 04:03:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def random_color():
|
|
|
|
return COLORS[random.randrange(0, len(COLORS))]
|