Skip to content

Commit

Permalink
Fix date parser for missing ms
Browse files Browse the repository at this point in the history
  • Loading branch information
fishi0x01 committed Jan 21, 2025
1 parent a23341b commit 4416d15
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
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"}]}

0 comments on commit 4416d15

Please sign in to comment.