convert_datetime¶
-
privex.helpers.converters.convert_datetime(d, if_empty=None, fail_empty=False, **kwargs) → Optional[datetime.datetime][source]¶ Convert the object
dinto adatetime.datetimeobject.If
dis a string or bytes, then it will be parsed usingdateutil.parser.parse()If
dis an int/float/Decimal, then it will be assumed to be a unix epoch timestamp.Examples:
>>> convert_datetime("2019-01-01T00:00:00Z") # ISO date/time datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc()) >>> convert_datetime("01/JAN/2019 00:00:00.0000") # Human date/time with month name datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc()) >>> convert_datetime(1546300800) # Unix timestamp as integer datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc()) >>> convert_datetime(1546300800000) # Unix timestamp (milliseconds) as integer datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())
- Parameters
d – Object to convert into a datetime
if_empty – If
dis empty / None, return this valuefail_empty (bool) – (Def:
False) If this is True, then ifdis empty, raisesAttributeError
- Key datetime.tzinfo tzinfo
(Default:
dateutil.tz.tzutc) If no timezone was detected by the parser, use this timezone. Set this toNoneto disable forcing timezone-aware dates.- Raises
AttributeError – When
dis empty andfail_emptyis set to True.dateutil.parser.ParserError – When
dcould not be parsed into a date.
- Return datetime converted
The converted
datetime.datetimeobject.