timeutils.py 10 KB

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