utils.time

Utilities related to dates, times, intervals, and timezones.

Module Contents

Classes

LocalTimezone(self) Local time implementation.
_Zone()
ffwd(self,year=None,month=None,weeks=0,weekday=None,day=None,hour=None,minute=None,second=None,microsecond=None,**kwargs) Version of dateutil.relativedelta that only supports addition.

Functions

maybe_timedelta(delta) Convert integer to timedelta, if argument is an integer.
delta_resolution(dt,delta) Round a datetime to the resolution of timedelta.
remaining(start,ends_in,now=None,relative=False) Calculate the remaining time for a start date and a timedelta.
rate(r) Convert rate string (“100/m”, “2/h” or “0.5/s”) to seconds.
weekday(name) Return the position of a weekday: 0 - 7, where 0 is Sunday.
humanize_seconds(secs,prefix=”“,sep=”“,now=”now”,microseconds=False) Show seconds in human form.
maybe_iso8601(dt) Either datetime | str -> datetime or None -> None.
is_naive(dt) Return True if datetime is naive.
make_aware(dt,tz) Set timezone for a datetime object.
localize(dt,tz) Convert aware datetime to another timezone.
to_utc(dt) Convert naive datetime to UTC.
maybe_make_aware(dt,tz=None) Convert dt to aware datetime, do nothing if dt is already aware.
utcoffset(time=_time,localtime=None) Return the current offset to UTC in hours.
adjust_timestamp(ts,offset,here=utcoffset) Adjust timestamp based on provided utcoffset.
get_exponential_backoff_interval(factor,retries,maximum,full_jitter=False) Calculate the exponential backoff wait time.
class LocalTimezone

Local time implementation.

Note:
Used only when the :setting:`enable_utc` setting is disabled.
__init__()
__repr__()
utcoffset(dt)
dst(dt)
tzname(dt)
fromutc(dt)
_isdst(dt)
class _Zone
tz_or_local(tzinfo=None)
to_local(dt, local=None, orig=None)
to_system(dt)
to_system(dt)
to_local_fallback(dt)
get_timezone(zone)
local()
utc()
maybe_timedelta(delta)

Convert integer to timedelta, if argument is an integer.

delta_resolution(dt, delta)

Round a datetime to the resolution of 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.

remaining(start, ends_in, now=None, relative=False)

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

For example, “how many seconds left for 30 seconds after start?”

Arguments:

start (~datetime.datetime): Starting date. ends_in (~datetime.timedelta): The end delta. relative (bool): If enabled the end time will be calculated

using delta_resolution() (i.e., rounded to the resolution of ends_in).
now (Callable): Function returning the current time and date.
Defaults to datetime.utcnow().
Returns:
~datetime.timedelta: Remaining time.
rate(r)

Convert rate string (“100/m”, “2/h” or “0.5/s”) to seconds.

weekday(name)

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

Example:
>>> weekday('sunday'), weekday('sun'), weekday('mon')
(0, 0, 1)
humanize_seconds(secs, prefix="", sep="", now="now", microseconds=False)

Show seconds in human form.

For example, 60 becomes “1 minute”, and 7200 becomes “2 hours”.

Arguments:
prefix (str): can be used to add a preposition to the output
(e.g., ‘in’ will give ‘in 1 second’, but add nothing to ‘now’).

now (str): Literal ‘now’. microseconds (bool): Include microseconds.

maybe_iso8601(dt)

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

is_naive(dt)

Return True if datetime is naive.

make_aware(dt, tz)

Set timezone for a datetime object.

localize(dt, tz)

Convert aware datetime to another timezone.

to_utc(dt)

Convert naive datetime to UTC.

maybe_make_aware(dt, tz=None)

Convert dt to aware datetime, do nothing if dt is already aware.

class ffwd(year=None, month=None, weeks=0, weekday=None, day=None, hour=None, minute=None, second=None, microsecond=None, **kwargs)

Version of dateutil.relativedelta that only supports addition.

__init__(year=None, month=None, weeks=0, weekday=None, day=None, hour=None, minute=None, second=None, microsecond=None, **kwargs)
__repr__()
__radd__(other)
_fields(**extra)
utcoffset(time=_time, localtime=None)

Return the current offset to UTC in hours.

adjust_timestamp(ts, offset, here=utcoffset)

Adjust timestamp based on provided utcoffset.

get_exponential_backoff_interval(factor, retries, maximum, full_jitter=False)

Calculate the exponential backoff wait time.