Skip to content

Commit

Permalink
Bug datetime parsing (#1906)
Browse files Browse the repository at this point in the history
  • Loading branch information
doctrino authored Sep 4, 2024
1 parent 929adcb commit 51910e0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [7.58.4] - 2024-09-03
### Fixed
- The deserialization `datetime` properties in `TypedNode`/`TypedEdge` now correctly handles truncated milliseconds.

## [7.58.3] - 2024-09-03
### Fixed
- The parameter `query` is now optional in `client.data_modeling.instances.search(...)`.
Expand Down
2 changes: 1 addition & 1 deletion cognite/client/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import annotations

__version__ = "7.58.3"
__version__ = "7.58.4"
__api_subversion__ = "20230101"
5 changes: 3 additions & 2 deletions cognite/client/data_classes/data_modeling/typed_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import inspect
from abc import ABC
from collections.abc import Iterable
from datetime import date, datetime
from datetime import date
from typing import TYPE_CHECKING, Any, cast

from typing_extensions import Self
Expand All @@ -20,6 +20,7 @@
_serialize_property_value,
)
from cognite.client.utils._text import to_camel_case
from cognite.client.utils._time import convert_data_modelling_timestamp

if TYPE_CHECKING:
from cognite.client import CogniteClient
Expand Down Expand Up @@ -304,7 +305,7 @@ def _deserialize_value(value: Any, parameter: inspect.Parameter) -> Any:
return value
annotation = str(parameter.annotation)
if "datetime" in annotation and isinstance(value, str):
return datetime.fromisoformat(value)
return convert_data_modelling_timestamp(value)
elif "date" in annotation and isinstance(value, str):
return date.fromisoformat(value)
elif DirectRelationReference.__name__ in annotation and isinstance(value, dict):
Expand Down
17 changes: 17 additions & 0 deletions cognite/client/utils/_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,23 @@ def datetime_to_ms_iso_timestamp(dt: datetime) -> str:
raise TypeError(f"Expected datetime object, got {type(dt)}")


def convert_data_modelling_timestamp(timestamp: str) -> datetime:
"""Converts a timestamp string to a datetime object.
Args:
timestamp (str): A timestamp string.
Returns:
datetime: A datetime object.
"""
try:
return datetime.fromisoformat(timestamp)
except ValueError:
# Typically hits if the timestamp has truncated milliseconds,
# For example, "2021-01-01T00:00:00.17+00:00".
return datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f%z")


def split_granularity_into_quantity_and_normalized_unit(granularity: str) -> tuple[int, str]:
"""A normalized unit is any unit accepted by the API"""
if match := re.match(r"(\d+)(.*)", granularity):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "cognite-sdk"

version = "7.58.3"
version = "7.58.4"
description = "Cognite Python SDK"
readme = "README.md"
documentation = "https://cognite-sdk-python.readthedocs-hosted.com"
Expand Down
22 changes: 22 additions & 0 deletions tests/tests_unit/test_utils/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
align_large_granularity,
align_start_and_end_for_granularity,
convert_and_isoformat_time_attrs,
convert_data_modelling_timestamp,
datetime_to_ms,
datetime_to_ms_iso_timestamp,
granularity_to_ms,
Expand Down Expand Up @@ -230,6 +231,27 @@ def test_negative(self, t):
timestamp_to_ms(t)


class TestConvertDataModelingTimestamp:
@pytest.mark.parametrize(
"timestamp_str, expected",
[
("2021-01-01T00:00:00.000+00:00", datetime(2021, 1, 1, 0, 0, 0, 0, tzinfo=timezone.utc)),
("2021-01-01T00:00:00.000+01:00", datetime(2021, 1, 1, 0, 0, 0, 0, tzinfo=timezone(timedelta(hours=1)))),
(
"2021-01-01T00:00:00.000+01:15",
datetime(2021, 1, 1, 0, 0, 0, 0, tzinfo=timezone(timedelta(hours=1, minutes=15))),
),
(
"2021-01-01T00:00:00.000-01:15",
datetime(2021, 1, 1, 0, 0, 0, 0, tzinfo=timezone(timedelta(hours=-1, minutes=-15))),
),
("2024-09-03T09:36:01.17+00:00", datetime(2024, 9, 3, 9, 36, 1, 170000, tzinfo=timezone.utc)),
],
)
def test_valid_timestamp_str(self, timestamp_str: str, expected: datetime) -> None:
assert expected == convert_data_modelling_timestamp(timestamp_str)


class TestGranularityToMs:
@pytest.mark.parametrize(
"granularity, expected_ms",
Expand Down

0 comments on commit 51910e0

Please sign in to comment.