timeutils.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.utils.timeutils
  4. ~~~~~~~~~~~~~~~~~~~~~~
  5. This module contains various utilities related to dates and times.
  6. """
  7. from __future__ import absolute_import
  8. import os
  9. import time as _time
  10. from kombu.utils import cached_property
  11. from datetime import datetime, timedelta, tzinfo
  12. from dateutil import tz
  13. from dateutil.parser import parse as parse_iso8601
  14. from celery.exceptions import ImproperlyConfigured
  15. from .text import pluralize
  16. try:
  17. import pytz
  18. from pytz import AmbiguousTimeError
  19. except ImportError: # pragma: no cover
  20. pytz = None # noqa
  21. class AmbiguousTimeError(Exception): # noqa
  22. pass
  23. C_REMDEBUG = os.environ.get('C_REMDEBUG', False)
  24. DAYNAMES = 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
  25. WEEKDAYS = dict((name, dow) for name, dow in zip(DAYNAMES, range(7)))
  26. RATE_MODIFIER_MAP = {'s': lambda n: n,
  27. 'm': lambda n: n / 60.0,
  28. 'h': lambda n: n / 60.0 / 60.0}
  29. HAVE_TIMEDELTA_TOTAL_SECONDS = hasattr(timedelta, 'total_seconds')
  30. TIME_UNITS = (('day', 60 * 60 * 24.0, lambda n: '%.2f' % n),
  31. ('hour', 60 * 60.0, lambda n: '%.2f' % n),
  32. ('minute', 60.0, lambda n: '%.2f' % n),
  33. ('second', 1.0, lambda n: '%.2f' % n))
  34. ZERO = timedelta(0)
  35. _local_timezone = None
  36. class LocalTimezone(tzinfo):
  37. """
  38. Local time implementation taken from Python's docs.
  39. Used only when pytz isn't available, and most likely inaccurate. If you're
  40. having trouble with this class, don't waste your time, just install pytz.
  41. """
  42. def __init__(self):
  43. # This code is moved in __init__ to execute it as late as possible
  44. # See get_default_timezone().
  45. self.STDOFFSET = timedelta(seconds=-_time.timezone)
  46. if _time.daylight:
  47. self.DSTOFFSET = timedelta(seconds=-_time.altzone)
  48. else:
  49. self.DSTOFFSET = self.STDOFFSET
  50. self.DSTDIFF = self.DSTOFFSET - self.STDOFFSET
  51. tzinfo.__init__(self)
  52. def __repr__(self):
  53. return "<LocalTimezone>"
  54. def utcoffset(self, dt):
  55. if self._isdst(dt):
  56. return self.DSTOFFSET
  57. else:
  58. return self.STDOFFSET
  59. def dst(self, dt):
  60. if self._isdst(dt):
  61. return self.DSTDIFF
  62. else:
  63. return ZERO
  64. def tzname(self, dt):
  65. return _time.tzname[self._isdst(dt)]
  66. def _isdst(self, dt):
  67. tt = (dt.year, dt.month, dt.day,
  68. dt.hour, dt.minute, dt.second,
  69. dt.weekday(), 0, 0)
  70. stamp = _time.mktime(tt)
  71. tt = _time.localtime(stamp)
  72. return tt.tm_isdst > 0
  73. class _Zone(object):
  74. def tz_or_local(self, tzinfo=None):
  75. if tzinfo is None:
  76. return self.local
  77. return self.get_timezone(tzinfo)
  78. def to_local(self, dt, local=None, orig=None):
  79. if is_naive(dt):
  80. dt = make_aware(dt, orig or self.utc)
  81. return localize(dt, self.tz_or_local(local))
  82. def to_system(self, dt):
  83. return localize(dt, self.local)
  84. def to_local_fallback(self, dt, *args, **kwargs):
  85. if is_naive(dt):
  86. return make_aware(dt, self.local)
  87. return localize(dt, self.local)
  88. def get_timezone(self, zone):
  89. if isinstance(zone, basestring):
  90. if pytz is None:
  91. if zone == 'UTC':
  92. return tz.gettz('UTC')
  93. raise ImproperlyConfigured(
  94. 'Timezones requires the pytz library')
  95. return pytz.timezone(zone)
  96. return zone
  97. @cached_property
  98. def local(self):
  99. return LocalTimezone()
  100. @cached_property
  101. def utc(self):
  102. return self.get_timezone('UTC')
  103. timezone = _Zone()
  104. def maybe_timedelta(delta):
  105. """Coerces integer to timedelta if `delta` is an integer."""
  106. if isinstance(delta, (int, float)):
  107. return timedelta(seconds=delta)
  108. return delta
  109. if HAVE_TIMEDELTA_TOTAL_SECONDS: # pragma: no cover
  110. def timedelta_seconds(delta):
  111. """Convert :class:`datetime.timedelta` to seconds.
  112. Doesn't account for negative values.
  113. """
  114. return max(delta.total_seconds(), 0)
  115. else: # pragma: no cover
  116. def timedelta_seconds(delta): # noqa
  117. """Convert :class:`datetime.timedelta` to seconds.
  118. Doesn't account for negative values.
  119. """
  120. if delta.days < 0:
  121. return 0
  122. return delta.days * 86400 + delta.seconds + (delta.microseconds / 10e5)
  123. def delta_resolution(dt, delta):
  124. """Round a datetime to the resolution of a timedelta.
  125. If the timedelta is in days, the datetime will be rounded
  126. to the nearest days, if the timedelta is in hours the datetime
  127. will be rounded to the nearest hour, and so on until seconds
  128. which will just return the original datetime.
  129. """
  130. delta = timedelta_seconds(delta)
  131. resolutions = ((3, lambda x: x / 86400),
  132. (4, lambda x: x / 3600),
  133. (5, lambda x: x / 60))
  134. args = dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second
  135. for res, predicate in resolutions:
  136. if predicate(delta) >= 1.0:
  137. return datetime(*args[:res])
  138. return dt
  139. def remaining(start, ends_in, now=None, relative=False, debug=False):
  140. """Calculate the remaining time for a start date and a timedelta.
  141. e.g. "how many seconds left for 30 seconds after start?"
  142. :param start: Start :class:`~datetime.datetime`.
  143. :param ends_in: The end delta as a :class:`~datetime.timedelta`.
  144. :keyword relative: If enabled the end time will be
  145. calculated using :func:`delta_resolution` (i.e. rounded to the
  146. resolution of `ends_in`).
  147. :keyword now: Function returning the current time and date,
  148. defaults to :func:`datetime.utcnow`.
  149. """
  150. now = now or datetime.utcnow()
  151. end_date = start + ends_in
  152. if relative:
  153. end_date = delta_resolution(end_date, ends_in)
  154. ret = end_date - now
  155. if C_REMDEBUG:
  156. print('rem: NOW:%s START:%s END_DATE:%s REM:%s' % (
  157. now, start, end_date, ret))
  158. return ret
  159. def rate(rate):
  160. """Parses rate strings, such as `"100/m"`, `"2/h"` or `"0.5/s"`
  161. and converts them to seconds."""
  162. if rate:
  163. if isinstance(rate, basestring):
  164. ops, _, modifier = rate.partition('/')
  165. return RATE_MODIFIER_MAP[modifier or 's'](float(ops)) or 0
  166. return rate or 0
  167. return 0
  168. def weekday(name):
  169. """Return the position of a weekday (0 - 7, where 0 is Sunday).
  170. Example::
  171. >>> weekday('sunday'), weekday('sun'), weekday('mon')
  172. (0, 0, 1)
  173. """
  174. abbreviation = name[0:3].lower()
  175. try:
  176. return WEEKDAYS[abbreviation]
  177. except KeyError:
  178. # Show original day name in exception, instead of abbr.
  179. raise KeyError(name)
  180. def humanize_seconds(secs, prefix='', sep=''):
  181. """Show seconds in human form, e.g. 60 is "1 minute", 7200 is "2
  182. hours".
  183. :keyword prefix: Can be used to add a preposition to the output,
  184. e.g. 'in' will give 'in 1 second', but add nothing to 'now'.
  185. """
  186. secs = float(secs)
  187. for unit, divider, formatter in TIME_UNITS:
  188. if secs >= divider:
  189. w = secs / divider
  190. return '%s%s%s %s' % (prefix, sep, formatter(w),
  191. pluralize(w, unit))
  192. return 'now'
  193. def maybe_iso8601(dt):
  194. """`Either datetime | str -> datetime or None -> None`"""
  195. if not dt:
  196. return
  197. if isinstance(dt, datetime):
  198. return dt
  199. return parse_iso8601(dt)
  200. def is_naive(dt):
  201. """Returns :const:`True` if the datetime is naive
  202. (does not have timezone information)."""
  203. return dt.tzinfo is None or dt.tzinfo.utcoffset(dt) is None
  204. def make_aware(dt, tz):
  205. """Sets the timezone for a datetime object."""
  206. try:
  207. _localize = tz.localize
  208. except AttributeError:
  209. return dt.replace(tzinfo=tz)
  210. else:
  211. # works on pytz timezones
  212. try:
  213. return _localize(dt, is_dst=None)
  214. except AmbiguousTimeError:
  215. return min(_localize(dt, is_dst=True),
  216. _localize(dt, is_dst=False))
  217. def localize(dt, tz):
  218. """Convert aware datetime to another timezone."""
  219. dt = dt.astimezone(tz)
  220. try:
  221. _normalize = tz.normalize
  222. except AttributeError: # non-pytz tz
  223. return dt
  224. else:
  225. try:
  226. return _normalize(dt, is_dst=None)
  227. except TypeError:
  228. return _normalize(dt)
  229. except AmbiguousTimeError:
  230. return min(_normalize(dt, is_dst=True),
  231. _normalize(dt, is_dst=False))
  232. def to_utc(dt):
  233. """Converts naive datetime to UTC"""
  234. return make_aware(dt, timezone.utc)
  235. def maybe_make_aware(dt, tz=None):
  236. if is_naive(dt):
  237. dt = to_utc(dt)
  238. return localize(dt,
  239. timezone.utc if tz is None else timezone.tz_or_local(tz))