Skip to content

Commit

Permalink
mionr fixes and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BYK committed Sep 21, 2024
1 parent c2d00cd commit 6064185
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
19 changes: 10 additions & 9 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,22 +245,23 @@ def format_timestamp(value):
def datetime_from_isoformat(value):
# type: (str) -> datetime
try:
return datetime.fromisoformat(value)
result = datetime.fromisoformat(value)
except (AttributeError, ValueError):
# py 3.6
timestamp_format = (
"%Y-%m-%dT%H:%M:%S.%f" if "." in value else "%Y-%m-%dT%H:%M:%S"
)
if value.endswith("Z"):
value = value[:-1]
else:
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"
value = value[:-1] + "+0000"

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)
result = datetime.strptime(value, timestamp_format)
return result.astimezone(timezone.utc)


def event_hint_with_exc_info(exc_info=None):
Expand Down
50 changes: 50 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from sentry_sdk.utils import (
Components,
Dsn,
datetime_from_isoformat,
env_to_bool,
format_timestamp,
get_current_thread_meta,
Expand Down Expand Up @@ -61,6 +62,55 @@ def _normalize_distribution_name(name):
return re.sub(r"[-_.]+", "-", name).lower()


@pytest.mark.parametrize(
("input_str", "expected_output"),
(
(
"2021-01-01T00:00:00.000000Z",
datetime(2021, 1, 1, tzinfo=timezone.utc),
), # UTC time
(
"2021-01-01T00:00:00.000000",
datetime(2021, 1, 1, tzinfo=datetime.now().astimezone().tzinfo),
), # No TZ -- assume UTC
(
"2021-01-01T00:00:00Z",
datetime(2021, 1, 1, tzinfo=timezone.utc),
), # UTC - No milliseconds
(
"2021-01-01T00:00:00.000000+00:00",
datetime(2021, 1, 1, tzinfo=timezone.utc),
),
(
"2021-01-01T00:00:00.000000-00:00",
datetime(2021, 1, 1, tzinfo=timezone.utc),
),
(
"2021-01-01T00:00:00.000000+0000",
datetime(2021, 1, 1, tzinfo=timezone.utc),
),
(
"2021-01-01T00:00:00.000000-0000",
datetime(2021, 1, 1, tzinfo=timezone.utc),
),
(
"2020-12-31T00:00:00.000000+02:00",
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=2))),
), # UTC+2 time
(
"2020-12-31T00:00:00.000000-0200",
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=-2))),
), # UTC-2 time
(
"2020-12-31T00:00:00-0200",
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=-2))),
), # UTC-2 time - no milliseconds
),
)
def test_datetime_from_isoformat(input_str, expected_output):
assert datetime_from_isoformat(input_str) == expected_output, input_str


@pytest.mark.parametrize(
"env_var_value,strict,expected",
[
Expand Down

0 comments on commit 6064185

Please sign in to comment.