tron.utils.timeutils module

Functions for working with dates and timestamps.

class tron.utils.timeutils.DateArithmetic

Bases: object

Parses a string which contains a date arithmetic pattern and returns a date with the delta added or subtracted.

DATE_FORMATS = {'day': '%d', 'hour': '%H', 'month': '%m', 'shortdate': '%Y-%m-%d', 'year': '%Y', 'ym': '%Y-%m', 'ymd': '%Y-%m-%d', 'ymdh': '%Y-%m-%dT%H', 'ymdhm': '%Y-%m-%dT%H:%M'}
DATE_TYPE_PATTERN = re.compile('(\\w+)([+-]\\d+)?')
classmethod parse(date_str, dt=None)

Parse a date arithmetic pattern (Ex: ‘shortdate-1’). Supports date strings: shortdate, year, month, day, unixtime, daynumber. Supports subtraction and addition operations of integers. Time unit is based on date format (Ex: seconds for unixtime, days for day).

tron.utils.timeutils.current_time(tz=None)

Return the current datetime.

tron.utils.timeutils.current_timestamp()

Return the current time as a timestamp.

tron.utils.timeutils.delta_total_seconds(td)

Equivalent to timedelta.total_seconds() available in Python 2.7.

tron.utils.timeutils.duration(start_time, end_time=None)

Get a timedelta between end_time and start_time, where end_time defaults to now().

WARNING: mixing tz-aware and naive datetimes in start_time and end_time will cause an error.

tron.utils.timeutils.macro_timedelta(start_date, years=0, months=0, days=0, hours=0, minutes=0)

Since datetime doesn’t provide timedeltas at the year or month level, this function generates timedeltas of the appropriate sizes.