Skip to content
Open
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
2 changes: 1 addition & 1 deletion snuba/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def get_re(format: Sequence[PartSegment]) -> Pattern[Any]:
if date_str and retention_days:
return Part(
part_str,
datetime.strptime(date_str, "%Y-%m-%d").astimezone(UTC),
datetime.strptime(date_str, "%Y-%m-%d").replace(tzinfo=UTC),
int(retention_days),
)

Expand Down
25 changes: 25 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import time
from collections.abc import Generator
from datetime import UTC, datetime

import pytest

from snuba.clickhouse.escaping import escape_alias, escape_identifier
from snuba.util import PartSegment, decode_part_str


def test_escape_identifier() -> None:
Expand All @@ -21,3 +28,21 @@ def test_escape_alias() -> None:
assert escape_alias("foo") == "foo"
assert escape_alias("foo.bar") == "`foo.bar`"
assert escape_alias("foo:bar") == "`foo:bar`"


@pytest.fixture
def local_timezone(monkeypatch: pytest.MonkeyPatch) -> Generator[None]:
# A zone east of UTC, where local midnight falls on the previous UTC day.
monkeypatch.setenv("TZ", "Asia/Tokyo")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use a context manager here - something like

@pytest.fixture
  def local_timezone(monkeypatch: pytest.MonkeyPatch) -> Generator[None]:
      with monkeypatch.context() as m:
          m.setenv("TZ", "Asia/Tokyo")
          time.tzset()
          yield
      time.tzset()

avoids calling monkeypatch.undo()

time.tzset()
yield
monkeypatch.undo()
time.tzset()


@pytest.mark.usefixtures("local_timezone")
def test_decode_part_str_ignores_local_timezone() -> None:
part = decode_part_str("(90,'2026-10-12')", [PartSegment.RETENTION_DAYS, PartSegment.DATE])
assert part.date == datetime(2026, 10, 12, tzinfo=UTC)
assert part.date.weekday() == 0
assert part.retention_days == 90
Loading