From c2d00cd67e71aed6ca5e0b9465f676ae789f3b3c Mon Sep 17 00:00:00 2001 From: Burak Yigit Kaya Date: Sat, 21 Sep 2024 17:09:02 +0300 Subject: [PATCH] simplify code and fix edge-case --- sentry_sdk/utils.py | 12 +++++++----- tests/test_basics.py | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index fdd21e6aeb..e6724c68ed 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -239,6 +239,9 @@ def format_timestamp(value): return utctime.strftime("%Y-%m-%dT%H:%M:%S.%fZ") +ISO_TZ_SEPARATORS = frozenset(("+", "-")) + + def datetime_from_isoformat(value): # type: (str) -> datetime try: @@ -251,11 +254,10 @@ def datetime_from_isoformat(value): if value.endswith("Z"): value = value[:-1] else: - plus_idx = value.rfind("+") - if plus_idx != -1: - tz_colon_index = value.rfind(":", plus_idx) - if tz_colon_index != -1: - value = value[:tz_colon_index] + value[tz_colon_index + 1 :] + if value[-6] in ISO_TZ_SEPARATORS: + timestamp_format += "%z" + value = value[:-3] + value[-2:] + elif value[-5] in ISO_TZ_SEPARATORS: timestamp_format += "%z" return datetime.strptime(value, timestamp_format).astimezone(timezone.utc) diff --git a/tests/test_basics.py b/tests/test_basics.py index 747ed87730..74dfe1955a 100644 --- a/tests/test_basics.py +++ b/tests/test_basics.py @@ -444,7 +444,7 @@ def test_breadcrumb_ordering_different_types(sentry_init, capture_events): datetime.datetime.strftime(timestamps[2], "%Y-%m-%dT%H:%M:%S") + "Z", datetime.datetime.strftime(timestamps[3], "%Y-%m-%dT%H:%M:%S.%f") + "+00:00", datetime.datetime.strftime(timestamps[4], "%Y-%m-%dT%H:%M:%S.%f") + "+0000", - datetime.datetime.strftime(timestamps[5], "%Y-%m-%dT%H:%M:%S.%f") + "+0000", + datetime.datetime.strftime(timestamps[5], "%Y-%m-%dT%H:%M:%S.%f") + "-0000", ] for i, timestamp in enumerate(timestamps):