Datetime Helper Functions

To set a standard where all datetime values in responses, Insanic provides helper functions that will format to the appropriate datetime format.

Functions

get_utc_timestamp()

Returns the current utc timestamp with decimals.

>>> get_utc_datetime()
1531279648.991617
Return type

float

get_utc_datetime()

Returns the current utc datetime object.

Return type

datetime

timestamp_to_datetime(timestamp=None, units=None)

Converts a timestamp to datetime. Assumes timestamp is in utc timezone. If not passed gets the current timestamp and converts that.

If passing in timestamp, must supply units(either ms or s) to depending on the units of the timestamp provided.

Parameters
  • timestamp (Optional[int]) – Defaults to utc timestamp

  • units (Optional[str]) – either “ms” or “s” if timestamp is passed

Return type

datetime

timestamp_seconds_to_datetime(timestamp)

Wrapper for timestamp_to_datetime

Parameters

timestamp (Union[int, float]) – A timestamp in seconds

Return type

datetime

timestamp_milliseconds_to_datetime(timestamp)

Wrapper for timestamp_to_datetime

Parameters

timestamp (Union[int, float]) – A timestamp in milliseconds

Return type

datetime

timestamp_to_iso(timestamp=None, units=None)

Takes a timestamp and converts it to a iso formatted string

Parameters
  • timestamp (Union[int, float, None]) –

  • units (Optional[str]) – either “ms” or “s”

Return type

str

iso_to_datetime(datetime_string)

Takes an iso formatted datetime string and tries to convert it to a datetime object

Parameters

datetime_string (str) – An iso formatted datetime string

Return type

datetime

iso_to_timestamp(iso)

Takes an iso formatted string and tries to convert it to a timestamp

Parameters

iso (str) – An iso formatted datetime string

Return type

float