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.

103 lines
2.3 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 = int(td.total_seconds())
  29. result = []
  30. mins, secs = divmod(total_seconds, 60)
  31. h, mins = divmod(mins, 60)
  32. if h:
  33. result.append("%d h" % h)
  34. if h or mins:
  35. result.append("%d min" % mins)
  36. result.append("%s sec" % secs)
  37. return " ".join(result)
  38. def format_approx_duration(td):
  39. v = td.total_seconds()
  40. for unit in (DAY, HOUR, MINUTE, SECOND):
  41. if v >= unit.nsecs:
  42. vv = v // unit.nsecs
  43. if vv == 1:
  44. return "1 %s" % unit.name
  45. else:
  46. return "%d %s" % (vv, unit.plural)
  47. return ""
  48. def month_boundaries(months=3):
  49. result = []
  50. now = timezone.now()
  51. y, m = now.year, now.month
  52. for x in range(0, months):
  53. result.insert(0, dt(y, m, 1, tzinfo=timezone.utc))
  54. m -= 1
  55. if m == 0:
  56. m = 12
  57. y = y - 1
  58. return result
  59. def choose_next_report_date(now=None):
  60. """ Calculate the target date for the next monthly report.
  61. Monthly reports should get sent on 1st of each month, at a random
  62. time after 12PM UTC (so it's over the month boundary even in UTC-12).
  63. """
  64. if now is None:
  65. now = timezone.now()
  66. h, m, s = randint(12, 23), randint(0, 59), randint(0, 59)
  67. if now.month == 12:
  68. return now.replace(now.year + 1, 1, 1, h, m, s)
  69. else:
  70. return now.replace(now.year, now.month + 1, 1, h, m, s)