Skip to content

Bug: date.replace() return value discarded in parse_iso_timestamp() #110503

@karesansui-u

Description

@karesansui-u

Description

In src/sentry/search/utils.py, line 220, date.replace(tzinfo=timezone.utc) discards its return value:

def parse_iso_timestamp(value: str) -> datetime:
    date = datetime.fromisoformat(value.replace("Z", "+00:00"))

    # Values with no timezone info will default to UTC
    if not date.tzinfo:
        date.replace(tzinfo=timezone.utc)  # ← return value discarded

    # Convert to UTC
    return datetime.fromtimestamp(date.timestamp(), tz=timezone.utc)

datetime is immutable — replace() returns a new object and does not mutate the original. The fix is:

        date = date.replace(tzinfo=timezone.utc)

Note: line 264 in the same file uses the correct pattern:

result = datetime.strptime(value, DATE_FORMAT).replace(tzinfo=timezone.utc)

Impact

When parse_iso_timestamp() receives a timezone-naive ISO 8601 string (e.g., "2026-03-12T10:30:00"), the date object remains naive. date.timestamp() on a naive datetime uses the server's local timezone, not UTC.

  • Servers running in UTC: no visible impact (coincidentally correct).
  • Non-UTC servers (local dev, on-premise): search queries with timezone-naive timestamps will be offset by the server's UTC difference (e.g., ±9 hours for JST).

This function is called via parse_datetime_string() from:

  • API start/end parameter parsing (api/utils.py)
  • Event search date filters (api/event_search.py)
  • Metrics API transformations
  • Trend analysis, root cause analysis, statistical detectors

Fix

One-line change:

-        date.replace(tzinfo=timezone.utc)
+        date = date.replace(tzinfo=timezone.utc)

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Waiting for: Support

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions