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.

106 lines
2.4 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=3):
  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
  62. def choose_next_report_date(now=None):
  63. """ Calculate the target date for the next monthly report.
  64. Monthly reports should get sent on 1st of each month, at a random
  65. time after 12PM UTC (so it's over the month boundary even in UTC-12).
  66. """
  67. if now is None:
  68. now = timezone.now()
  69. h, m, s = randint(12, 23), randint(0, 59), randint(0, 59)
  70. if now.month == 12:
  71. return now.replace(now.year + 1, 1, 1, h, m, s)
  72. else:
  73. return now.replace(now.year, now.month + 1, 1, h, m, s)