timeutils.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import math
  2. from datetime import datetime, timedelta
  3. from dateutil.parser import parse as parse_iso8601
  4. DAYNAMES = "sun", "mon", "tue", "wed", "thu", "fri", "sat"
  5. WEEKDAYS = dict((name, dow) for name, dow in zip(DAYNAMES, range(7)))
  6. RATE_MODIFIER_MAP = {"s": lambda n: n,
  7. "m": lambda n: n / 60.0,
  8. "h": lambda n: n / 60.0 / 60.0}
  9. HAVE_TIMEDELTA_TOTAL_SECONDS = hasattr(timedelta, "total_seconds")
  10. TIME_UNITS = (("day", 60 * 60 * 24, lambda n: int(math.ceil(n))),
  11. ("hour", 60 * 60, lambda n: int(math.ceil(n))),
  12. ("minute", 60, lambda n: int(math.ceil(n))),
  13. ("second", 1, lambda n: "%.2f" % n))
  14. def maybe_timedelta(delta):
  15. """Coerces integer to timedelta if `delta` is an integer."""
  16. if isinstance(delta, (int, float)):
  17. return timedelta(seconds=delta)
  18. return delta
  19. def timedelta_seconds(delta): # pragma: no cover
  20. """Convert :class:`datetime.timedelta` to seconds.
  21. Doesn't account for negative values.
  22. """
  23. if HAVE_TIMEDELTA_TOTAL_SECONDS:
  24. # Should return 0 for negative seconds
  25. return max(delta.total_seconds(), 0)
  26. if delta.days < 0:
  27. return 0
  28. return delta.days * 86400 + delta.seconds + (delta.microseconds / 10e5)
  29. def delta_resolution(dt, delta):
  30. """Round a datetime to the resolution of a timedelta.
  31. If the timedelta is in days, the datetime will be rounded
  32. to the nearest days, if the timedelta is in hours the datetime
  33. will be rounded to the nearest hour, and so on until seconds
  34. which will just return the original datetime.
  35. """
  36. delta = timedelta_seconds(delta)
  37. resolutions = ((3, lambda x: x / 86400),
  38. (4, lambda x: x / 3600),
  39. (5, lambda x: x / 60))
  40. args = dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second
  41. for res, predicate in resolutions:
  42. if predicate(delta) >= 1.0:
  43. return datetime(*args[:res])
  44. return dt
  45. def remaining(start, ends_in, now=None, relative=True):
  46. """Calculate the remaining time for a start date and a timedelta.
  47. e.g. "how many seconds left for 30 seconds after start?"
  48. :param start: Start :class:`~datetime.datetime`.
  49. :param ends_in: The end delta as a :class:`~datetime.timedelta`.
  50. :keyword relative: If set to :const:`False`, the end time will be
  51. calculated using :func:`delta_resolution` (i.e. rounded to the
  52. resolution of `ends_in`).
  53. :keyword now: Function returning the current time and date,
  54. defaults to :func:`datetime.now`.
  55. """
  56. now = now or datetime.now()
  57. end_date = start + ends_in
  58. if not relative:
  59. end_date = delta_resolution(end_date, ends_in)
  60. return end_date - now
  61. def rate(rate):
  62. """Parses rate strings, such as `"100/m"` or `"2/h"`
  63. and converts them to seconds."""
  64. if rate:
  65. if isinstance(rate, basestring):
  66. ops, _, modifier = rate.partition("/")
  67. return RATE_MODIFIER_MAP[modifier or "s"](int(ops)) or 0
  68. return rate or 0
  69. return 0
  70. def weekday(name):
  71. """Return the position of a weekday (0 - 7, where 0 is Sunday).
  72. Example::
  73. >>> weekday("sunday"), weekday("sun"), weekday("mon")
  74. (0, 0, 1)
  75. """
  76. abbreviation = name[0:3].lower()
  77. try:
  78. return WEEKDAYS[abbreviation]
  79. except KeyError:
  80. # Show original day name in exception, instead of abbr.
  81. raise KeyError(name)
  82. def humanize_seconds(secs, prefix=""):
  83. """Show seconds in human form, e.g. 60 is "1 minute", 7200 is "2
  84. hours"."""
  85. for unit, divider, formatter in TIME_UNITS:
  86. if secs >= divider:
  87. w = secs / divider
  88. punit = w > 1 and (unit + "s") or unit
  89. return "%s%s %s" % (prefix, formatter(w), punit)
  90. return "now"
  91. def maybe_iso8601(dt):
  92. """`Either datetime | str -> datetime or None -> None`"""
  93. if not dt:
  94. return
  95. if isinstance(dt, datetime):
  96. return dt
  97. return parse_iso8601(dt)