Skip to content

Commit

Permalink
simplify code and fix edge-case
Browse files Browse the repository at this point in the history
  • Loading branch information
BYK committed Sep 21, 2024
1 parent 54b407a commit c2d00cd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit c2d00cd

Please sign in to comment.