Skip to content

Fix date parser for missing ms #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion dynatrace/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@


ISO_8601 = "%Y-%m-%dT%H:%M:%S.%fZ"
ISO_8601_NO_MS = "%Y-%m-%dT%H:%M:%SZ"


def slugify(value):
Expand Down Expand Up @@ -53,7 +54,11 @@ def timestamp_to_string(timestamp: Optional[Union[datetime, str]]) -> Optional[s

def iso8601_to_datetime(timestamp: Optional[str]) -> Optional[datetime]:
if isinstance(timestamp, str):
return datetime.strptime(timestamp, ISO_8601)
try:
return datetime.strptime(timestamp, ISO_8601)
except ValueError:
# DT API currently omitts milliseconds in response if they are 000
return datetime.strptime(timestamp, ISO_8601_NO_MS)
return timestamp


Expand Down
12 changes: 12 additions & 0 deletions test/environment_v2/test_tokens_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from dynatrace import Dynatrace


def test_parse_multiple_date_formats(dt: Dynatrace):
"""
The API can currently return two different date formats.
If a token is created exactly at 0, then the millisecond portion is not returned by the API.
E.g., 2025-01-19T21:36:02.000Z will be returned as 2025-01-19T21:36:02Z by API.
This test ensures that both are parsed correctly.
"""
tokens = dt.tokens.list()
assert len(tokens) == 2
1 change: 1 addition & 0 deletions test/mock_data/GET_api_v2_apiTokens_ca4f773d77d3bb7.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"totalCount": 2, "pageSize": 200, "apiTokens": [{"id": "dt0c01.34ILWD2EUD4ZYZG3LH7D2358", "name": "Without ms in timestamp", "enabled": true, "owner": "TestOwner", "creationDate": "2025-01-20T21:37:02Z"}, {"id": "dt0c01.SZQBU44KW2NIOICNL6YZ4354", "name": "With ms in timestamp", "enabled": true, "owner": "TestOwner", "creationDate": "2025-01-19T21:36:02.999Z"}]}