convert_datetime

privex.helpers.converters.convert_datetime(d, if_empty=None, fail_empty=False, **kwargs) → Optional[datetime.datetime][source]

Convert the object d into a datetime.datetime object.

If d is a string or bytes, then it will be parsed using dateutil.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 value

  • fail_empty (bool) – (Def: False) If this is True, then if d is empty, raises AttributeError

Key datetime.tzinfo tzinfo

(Default: dateutil.tz.tzutc) If no timezone was detected by the parser, use this timezone. Set this to None to disable forcing timezone-aware dates.

Raises
Return datetime converted

The converted datetime.datetime object.