Skip to content

fix: Fix non-UTC timestamps #3461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import threading
import time
from collections import namedtuple
from datetime import datetime
from datetime import datetime, timezone
from decimal import Decimal
from functools import partial, partialmethod, wraps
from numbers import Real
Expand Down Expand Up @@ -228,7 +228,15 @@ def to_timestamp(value):

def format_timestamp(value):
# type: (datetime) -> str
return value.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
"""Formats a timestamp in RFC 3339 format.

Any datetime objects with a non-UTC timezone are converted to UTC, so that all timestamps are formatted in UTC.
"""
utctime = value.astimezone(timezone.utc)

# We use this custom formatting rather than isoformat for backwards compatibility (we have used this format for
# several years now), and isoformat is slightly different.
return utctime.strftime("%Y-%m-%dT%H:%M:%S.%fZ")


def event_hint_with_exc_info(exc_info=None):
Expand Down
39 changes: 38 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import threading
import re
import sys
from datetime import timedelta
from datetime import timedelta, datetime, timezone
from unittest import mock

import pytest
Expand All @@ -13,6 +13,7 @@
Components,
Dsn,
env_to_bool,
format_timestamp,
get_current_thread_meta,
get_default_release,
get_error_message,
Expand Down Expand Up @@ -950,3 +951,39 @@ def target():
thread.start()
thread.join()
assert (main_thread.ident, main_thread.name) == results.get(timeout=1)


@pytest.mark.parametrize(
("datetime_object", "expected_output"),
(
(
datetime(2021, 1, 1, tzinfo=timezone.utc),
"2021-01-01T00:00:00.000000Z",
), # UTC time
(
datetime(2021, 1, 1, tzinfo=timezone(timedelta(hours=2))),
"2020-12-31T22:00:00.000000Z",
), # UTC+2 time
(
datetime(2021, 1, 1, tzinfo=timezone(timedelta(hours=-7))),
"2021-01-01T07:00:00.000000Z",
), # UTC-7 time
(
datetime(2021, 2, 3, 4, 56, 7, 890123, tzinfo=timezone.utc),
"2021-02-03T04:56:07.890123Z",
), # UTC time all non-zero fields
),
)
def test_format_timestamp(datetime_object, expected_output):
formatted = format_timestamp(datetime_object)

assert formatted == expected_output


def test_format_timestamp_naive():
datetime_object = datetime(2021, 1, 1)
timestamp_regex = r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{6}Z"

# Ensure that some timestamp is returned, without error. We currently treat these as local time, but this is an
# implementation detail which we should not assert here.
assert re.fullmatch(timestamp_regex, format_timestamp(datetime_object))
Loading