timeutils.py 2.9 KB

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