timeutils.py 3.7 KB

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