Utils
Convert timestamp to milliseconds since epoch
- cognite.client.utils.timestamp_to_ms(timestamp: int | float | str | datetime.datetime) int
Returns the ms representation of some timestamp given by milliseconds, time-shift format or datetime object
- Parameters
timestamp (int | float | str | datetime) – Convert this timestamp to ms.
- Returns
Milliseconds since epoch representation of timestamp
- Return type
int
Examples
Gets the millisecond representation of a timestamp:
>>> from cognite.client.utils import timestamp_to_ms >>> from datetime import datetime >>> timestamp_to_ms(datetime(2021, 1, 7, 12, 0, 0)) >>> timestamp_to_ms("now") >>> timestamp_to_ms("2w-ago") # 2 weeks ago >>> timestamp_to_ms("3d-ahead") # 3 days ahead from now
Convert milliseconds since epoch to datetime
- cognite.client.utils.ms_to_datetime(ms: int | float) datetime
Converts valid Cognite timestamps, i.e. milliseconds since epoch, to datetime object.
- Parameters
ms (int | float) – Milliseconds since epoch.
- Raises
ValueError – On invalid Cognite timestamps.
- Returns
Aware datetime object in UTC.
- Return type
datetime
Convert datetime to milliseconds since epoch
- cognite.client.utils.datetime_to_ms(dt: datetime) int
Converts a datetime object to milliseconds since epoch.
- Parameters
dt (datetime) – Naive or aware datetime object. Naive datetimes are interpreted as local time.
- Returns
Milliseconds since epoch (negative for time prior to 1970-01-01)
- Return type
int
Convert datetime to ISO timestamp
- cognite.client.utils.datetime_to_ms_iso_timestamp(dt: datetime) str
Converts a datetime object to a string representing a timestamp in the ISO-format expected by the Cognite GraphQL API.
- Parameters
dt (datetime) – Naive or aware datetime object. Naive datetimes are interpreted as local time.
- Raises
TypeError – If dt is not a datetime object
- Returns
Timestamp string in ISO 8601 format with milliseconds
- Return type
str