utils

This module offers general convenience and utility functions for dealing with datetimes.

New in version 2.7.0.

dateutil.utils.default_tzinfo(dt, tzinfo)[source]

Sets the tzinfo parameter on naive datetimes only

This is useful for example when you are provided a datetime that may have either an implicit or explicit time zone, such as when parsing a time zone string.

>>> from dateutil.tz import tzoffset
>>> from dateutil.parser import parse
>>> from dateutil.utils import default_tzinfo
>>> dflt_tz = tzoffset("EST", -18000)
>>> print(default_tzinfo(parse('2014-01-01 12:30 UTC'), dflt_tz))
2014-01-01 12:30:00+00:00
>>> print(default_tzinfo(parse('2014-01-01 12:30'), dflt_tz))
2014-01-01 12:30:00-05:00
Parameters:
  • dt – The datetime on which to replace the time zone

  • tzinfo – The datetime.tzinfo subclass instance to assign to dt if (and only if) it is naive.

Returns:

Returns an aware datetime.datetime.

dateutil.utils.today(tzinfo=None)[source]

Returns a datetime representing the current day at midnight

Parameters:

tzinfo – The time zone to attach (also used to determine the current day).

Returns:

A datetime.datetime object representing the current day at midnight.

dateutil.utils.within_delta(dt1, dt2, delta)[source]

Useful for comparing two datetimes that may have a negligible difference to be considered equal.