Skip to content

Commit 262d1f6

Browse files
authored
Merge pull request #110 from fishi0x01/fix-date-parsing
Fix date parser for missing ms
2 parents 7c2b752 + 4416d15 commit 262d1f6

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

dynatrace/utils.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424

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

2728

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

5455
def iso8601_to_datetime(timestamp: Optional[str]) -> Optional[datetime]:
5556
if isinstance(timestamp, str):
56-
return datetime.strptime(timestamp, ISO_8601)
57+
try:
58+
return datetime.strptime(timestamp, ISO_8601)
59+
except ValueError:
60+
# DT API currently omitts milliseconds in response if they are 000
61+
return datetime.strptime(timestamp, ISO_8601_NO_MS)
5762
return timestamp
5863

5964

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from dynatrace import Dynatrace
2+
3+
4+
def test_parse_multiple_date_formats(dt: Dynatrace):
5+
"""
6+
The API can currently return two different date formats.
7+
If a token is created exactly at 0, then the millisecond portion is not returned by the API.
8+
E.g., 2025-01-19T21:36:02.000Z will be returned as 2025-01-19T21:36:02Z by API.
9+
This test ensures that both are parsed correctly.
10+
"""
11+
tokens = dt.tokens.list()
12+
assert len(tokens) == 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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 commit comments

Comments
 (0)