This document describes the current stable version of Celery (3.1). For development docs, go here.

celery.utils.timeutils

celery.utils.timeutils

This module contains various utilities related to dates and times.

class celery.utils.timeutils.LocalTimezone[source]

Local time implementation taken from Python’s docs.

Used only when UTC is not enabled.

dst(dt)[source]
tzname(dt)[source]
utcoffset(dt)[source]
celery.utils.timeutils.maybe_timedelta(delta)[source]

Coerces integer to timedelta if delta is an integer.

celery.utils.timeutils.timedelta_seconds(delta)[source]

Convert datetime.timedelta to seconds.

Doesn’t account for negative values.

celery.utils.timeutils.delta_resolution(dt, delta)[source]

Round a datetime to the resolution of a timedelta.

If the timedelta is in days, the datetime will be rounded to the nearest days, if the timedelta is in hours the datetime will be rounded to the nearest hour, and so on until seconds which will just return the original datetime.

celery.utils.timeutils.remaining(start, ends_in, now=None, relative=False)[source]

Calculate the remaining time for a start date and a timedelta.

e.g. “how many seconds left for 30 seconds after start?”

Parameters:
  • start – Start datetime.
  • ends_in – The end delta as a timedelta.
  • relative – If enabled the end time will be calculated using delta_resolution() (i.e. rounded to the resolution of ends_in).
  • now – Function returning the current time and date, defaults to datetime.utcnow().
celery.utils.timeutils.rate(rate)[source]

Parse rate strings, such as “100/m”, “2/h” or “0.5/s” and convert them to seconds.

celery.utils.timeutils.weekday(name)[source]

Return the position of a weekday (0 - 7, where 0 is Sunday).

Example:

>>> weekday('sunday'), weekday('sun'), weekday('mon')
(0, 0, 1)
celery.utils.timeutils.humanize_seconds(secs, prefix='', sep='', now='now')[source]

Show seconds in human form, e.g. 60 is “1 minute”, 7200 is “2 hours”.

Parameters:prefix – Can be used to add a preposition to the output, e.g. ‘in’ will give ‘in 1 second’, but add nothing to ‘now’.
celery.utils.timeutils.maybe_iso8601(dt)[source]

Either datetime | str -> datetime or None -> None

celery.utils.timeutils.is_naive(dt)[source]

Return True if the datetime is naive (does not have timezone information).

celery.utils.timeutils.make_aware(dt, tz)[source]

Sets the timezone for a datetime object.

celery.utils.timeutils.localize(dt, tz)[source]

Convert aware datetime to another timezone.

celery.utils.timeutils.to_utc(dt)[source]

Converts naive datetime to UTC

celery.utils.timeutils.maybe_make_aware(dt, tz=None)[source]
class celery.utils.timeutils.ffwd(year=None, month=None, weeks=0, weekday=None, day=None, hour=None, minute=None, second=None, microsecond=None, **kwargs)[source]

Version of relativedelta that only supports addition.

celery.utils.timeutils.utcoffset(time=<module 'time' (built-in)>, localtime=<built-in function localtime>)[source]
celery.utils.timeutils.adjust_timestamp(ts, offset, here=<function utcoffset>)[source]
celery.utils.timeutils.maybe_s_to_ms(v)[source]