utils
This module offers general convenience and utility functions for dealing with datetimes.
Added in version 2.7.0.
- dateutil.utils.default_tzinfo(dt, tzinfo)[source]
Sets the
tzinfo
parameter on naive datetimes onlyThis 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 todt
if (and only if) it is naive.
- Returns:
Returns an aware
datetime.datetime
.