convert_datetime¶
-
privex.helpers.converters.
convert_datetime
(d, if_empty=None, fail_empty=False, **kwargs) → Optional[datetime.datetime][source]¶ Convert the object
d
into adatetime.datetime
object.If
d
is a string or bytes, then it will be parsed usingdateutil.parser.parse()
If
d
is 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
d
is empty / None, return this valuefail_empty (bool) – (Def:
False
) If this is True, then ifd
is empty, raisesAttributeError
- Key datetime.tzinfo tzinfo
(Default:
dateutil.tz.tzutc
) If no timezone was detected by the parser, use this timezone. Set this toNone
to disable forcing timezone-aware dates.- Raises
AttributeError – When
d
is empty andfail_empty
is set to True.dateutil.parser.ParserError – When
d
could not be parsed into a date.
- Return datetime converted
The converted
datetime.datetime
object.