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.

87 lines
1.9 KiB

  1. from datetime import datetime as dt
  2. from random import randint
  3. from django.utils import timezone
  4. class Unit(object):
  5. def __init__(self, name, nsecs):
  6. self.name = name
  7. self.plural = name + "s"
  8. self.nsecs = nsecs
  9. SECOND = Unit("second", 1)
  10. MINUTE = Unit("minute", 60)
  11. HOUR = Unit("hour", MINUTE.nsecs * 60)
  12. DAY = Unit("day", HOUR.nsecs * 24)
  13. WEEK = Unit("week", DAY.nsecs * 7)
  14. def format_duration(td):
  15. remaining_seconds = int(td.total_seconds())
  16. result = []
  17. for unit in (WEEK, DAY, HOUR, MINUTE):
  18. if unit == WEEK and remaining_seconds % unit.nsecs != 0:
  19. # Say "8 days" instead of "1 week 1 day"
  20. continue
  21. v, remaining_seconds = divmod(remaining_seconds, unit.nsecs)
  22. if v == 1:
  23. result.append("1 %s" % unit.name)
  24. elif v > 1:
  25. result.append("%d %s" % (v, unit.plural))
  26. return " ".join(result)
  27. def format_hms(td):
  28. total_seconds = td.total_seconds()
  29. if 0.01 <= total_seconds < 1:
  30. return "%.2f sec" % total_seconds
  31. total_seconds = int(total_seconds)
  32. result = []
  33. mins, secs = divmod(total_seconds, 60)
  34. h, mins = divmod(mins, 60)
  35. if h:
  36. result.append("%d h" % h)
  37. if h or mins:
  38. result.append("%d min" % mins)
  39. result.append("%s sec" % secs)
  40. return " ".join(result)
  41. def format_approx_duration(td):
  42. v = td.total_seconds()
  43. for unit in (DAY, HOUR, MINUTE, SECOND):
  44. if v >= unit.nsecs:
  45. vv = v // unit.nsecs
  46. if vv == 1:
  47. return "1 %s" % unit.name
  48. else:
  49. return "%d %s" % (vv, unit.plural)
  50. return ""
  51. def month_boundaries(months=2):
  52. result = []
  53. now = timezone.now()
  54. y, m = now.year, now.month
  55. for x in range(0, months):
  56. result.insert(0, dt(y, m, 1, tzinfo=timezone.utc))
  57. m -= 1
  58. if m == 0:
  59. m = 12
  60. y = y - 1
  61. return result