ngsildclient.utils.iso8601#

This module contains helper functions to generate iso8601-formatted strings from native Python date types.

Notes

NGSI-LD dates are expressed in UTC, using the ISO8601 format.

References

1

ETSI, 2021. “Supported data types for Values” in Context Information Management (CIM); NGSI-LD API ETSI GS CIM 009 V1.4.2, pp. 41-42, 2021-04.

Functions

extract(value)

Extract an ISO8601 datetime string from the input string.

from_date(value)

Convert from date to ISO8601-formatted string.

from_datetime(value)

Convert from datetime to ISO8601-formatted string.

from_time(value)

Convert from time to ISO8601-formatted string.

parse(value)

Guess the temporal date type from a given argument carrying a temporal information.

utcnow()

Converts the current UTC datetime to ISO8601-formatted string.

ngsildclient.utils.iso8601.extract(value)[source]#

Extract an ISO8601 datetime string from the input string.

Parameters

value (str) – the input string

Returns

the extracted datetime if found, else None

Return type

datetime

Example

>>> from ngsildclient.utils import iso8601
>>> iso8601.extract("urn:ngsi-ld:WeatherObserved:Spain-WeatherObserved-Valladolid-2016-11-30T07:00:00Z")
datetime.datetime(2016, 11, 30, 7, 0)
ngsildclient.utils.iso8601.from_date(value)[source]#

Convert from date to ISO8601-formatted string.

Parameters

value (date) – The date to be converted.

Returns

ISO8601-formatted string

Return type

str

Example

>>> from datetime import date
>>> from ngsildclient import iso8601
>>> d = date(2021, 10, 13)
>>> print(iso8601.from_date(d))
2021-10-13
ngsildclient.utils.iso8601.from_datetime(value)[source]#

Convert from datetime to ISO8601-formatted string.

Parameters

value (datetime) – The datetime to be converted. Expected in UTC.

Returns

ISO8601-formatted string

Return type

str

Example

>>> from datetime import datetime
>>> from ngsildclient import iso8601
>>> d = datetime(2021, 10, 13, 9, 29)
>>> print(iso8601.from_datetime(d))
2021-10-13T09:29:00Z
ngsildclient.utils.iso8601.from_time(value)[source]#

Convert from time to ISO8601-formatted string.

Parameters

value (time) – The time to be converted. Expected in UTC.

Returns

ISO8601-formatted string

Return type

str

Example

>>> from datetime import time
>>> from ngsildclient import iso8601
>>> d = time(9, 29)
>>> print(iso8601.from_time(d))
09:29:00Z
ngsildclient.utils.iso8601.parse(value)[source]#

Guess the temporal date type from a given argument carrying a temporal information.

This function is typically used to build a NGSI-LD Temporal Property or temporal metadata such as observedAt.

Parameters

value (Union[datetime, date, time, str]) – A value carrying temporal information, could be either either a datetime, date or time, or a string representation of these former types.

Returns

A tuple composed of a ISO8601 string representation of the date and the TemporalType that has been identified

Return type

tuple[str, TemporalType, datetime]

Raises

ValueError – The date format does not match datetime, date or time or a valid string representation of a date

See also

ngsildclient.attribute

Example

>>> from ngsildclient import iso8601
>>> print(iso8601.parse(date(2021,9,13)))
('2021-10-13', <TemporalType.DATE: 'Date'>)
ngsildclient.utils.iso8601.utcnow()[source]#

Converts the current UTC datetime to ISO8601-formatted string.

Returns

The current UTC datetime, ISO8601-formatted

Return type

str