timeutils.py 8.4 KB

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