Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(hogql): fix timezone_info override on compare/previous period ranges (#58328)

Frank Hamandb4183a3477d3
diff --git a/posthog/hogql_queries/utils/query_compare_to_date_range.py b/posthog/hogql_queries/utils/query_compare_to_date_range.py
index 35ca0780987f..ce70c90a315b 100644
--- a/posthog/hogql_queries/utils/query_compare_to_date_range.py
+++ b/posthog/hogql_queries/utils/query_compare_to_date_range.py
@@ -35,7 +35,7 @@ class QueryCompareToDateRange(QueryDateRange):
start_date = relative_date_parse(
self.compare_to,
- self._team.timezone_info,
+ self._timezone_info,
now=current_period_date_from,
human_friendly_comparison_periods=bool(self._team.human_friendly_comparison_periods),
)
diff --git a/posthog/hogql_queries/utils/query_previous_period_date_range.py b/posthog/hogql_queries/utils/query_previous_period_date_range.py
index cf3f8e7c89b5..d4ec5a2f3018 100644
--- a/posthog/hogql_queries/utils/query_previous_period_date_range.py
+++ b/posthog/hogql_queries/utils/query_previous_period_date_range.py
@@ -35,7 +35,7 @@ class QueryPreviousPeriodDateRange(QueryDateRange):
delta_mapping = relative_date_parse_with_delta_mapping(
date_from,
- self._team.timezone_info,
+ self._timezone_info,
now=self.now_with_timezone,
)[1]
return delta_mapping
@@ -44,7 +44,7 @@ class QueryPreviousPeriodDateRange(QueryDateRange):
if self._date_range and self._date_range.date_to:
delta_mapping = relative_date_parse_with_delta_mapping(
self._date_range.date_to,
- self._team.timezone_info,
+ self._timezone_info,
always_truncate=True,
now=self.now_with_timezone,
)[1]
diff --git a/posthog/hogql_queries/utils/test/test_query_compare_to_date_range.py b/posthog/hogql_queries/utils/test/test_query_compare_to_date_range.py
index d019a79e475f..5268cad5bb65 100644
--- a/posthog/hogql_queries/utils/test/test_query_compare_to_date_range.py
+++ b/posthog/hogql_queries/utils/test/test_query_compare_to_date_range.py
@@ -1,3 +1,5 @@
+from zoneinfo import ZoneInfo
+
from posthog.test.base import APIBaseTest
from dateutil import parser
@@ -63,6 +65,52 @@ class TestQueryCompareToDateRange(APIBaseTest):
# Human friendly comparison periods guarantee that the end of the week is same day
self.assertEqual(query_date_range.date_to().isoweekday(), now.isoweekday())
+ def test_explicit_timezone_info_overrides_team_timezone(self):
+ # The compare-period parsing used to read directly from `self._team.timezone_info`,
+ # so a `timezone_info=UTC` override on the constructor was silently ignored.
+ #
+ # The bug surfaces in `date_from_str` / `date_to_str`, not in the datetime
+ # objects themselves: both point to the same UTC instant but display in
+ # different timezones. `format_date` strips the tz suffix via
+ # `strftime("%Y-%m-%d %H:%M:%S")`, so the formatted string carries the
+ # team-tz wall clock under the bug and the UTC wall clock with the fix.
+ # That string is what flows into ClickHouse, so we assert against it.
+ self.team.timezone = "US/Pacific"
+ self.team.save()
+
+ now = parser.isoparse("2021-08-25T00:00:00.000Z")
+ date_range = DateRange(date_from="-48h")
+ with_override = QueryCompareToDateRange(
+ team=self.team,
+ date_range=date_range,
+ interval=IntervalType.DAY,
+ now=now,
+ compare_to="-1d",
+ timezone_info=ZoneInfo("UTC"),
+ )
+ without_override = QueryCompareToDateRange(
+ team=self.team,
+ date_range=date_range,
+ interval=IntervalType.DAY,
+ now=now,
+ compare_to="-1d",
+ )
+ # The override must change the formatted output — otherwise the test wouldn't
+ # catch a regression of the fix.
+ self.assertNotEqual(with_override.date_from_str, without_override.date_from_str)
+ # And the override produces the same result as a UTC-team baseline would.
+ self.team.timezone = "UTC"
+ self.team.save()
+ utc_baseline = QueryCompareToDateRange(
+ team=self.team,
+ date_range=date_range,
+ interval=IntervalType.DAY,
+ now=now,
+ compare_to="-1d",
+ )
+ self.assertEqual(with_override.date_from_str, utc_baseline.date_from_str)
+ self.assertEqual(with_override.date_to_str, utc_baseline.date_to_str)
+
def test_minus_one_year_human_friendly(self):
self.team.human_friendly_comparison_periods = True
diff --git a/posthog/hogql_queries/utils/test/test_query_previous_period_date_range.py b/posthog/hogql_queries/utils/test/test_query_previous_period_date_range.py
new file mode 100644
index 000000000000..eac0d1425269
--- /dev/null
+++ b/posthog/hogql_queries/utils/test/test_query_previous_period_date_range.py
@@ -0,0 +1,71 @@
+from zoneinfo import ZoneInfo
+
+from posthog.test.base import APIBaseTest
+
+from dateutil import parser
+
+from posthog.schema import DateRange, IntervalType
+
+from posthog.hogql_queries.utils.query_previous_period_date_range import QueryPreviousPeriodDateRange
+
+
+class TestQueryPreviousPeriodDateRange(APIBaseTest):
+ def test_previous_period(self):
+ now = parser.isoparse("2021-08-25T00:00:00.000Z")
+ date_range = DateRange(date_from="-48h")
+ query_date_range = QueryPreviousPeriodDateRange(
+ team=self.team, date_range=date_range, interval=IntervalType.DAY, now=now
+ )
+ # Current period [2021-08-23T00:00, 2021-08-25T23:59:59] is ~3 days inclusive.
+ # The previous period shifts back by that full span.
+ self.assertEqual(query_date_range.date_from(), parser.isoparse("2021-08-20T00:00:00Z"))
+ self.assertEqual(query_date_range.date_to(), parser.isoparse("2021-08-22T23:59:59.999999Z"))
+
+ def test_explicit_timezone_info_overrides_team_timezone(self):
+ # The previous-period delta parsing used to read directly from
+ # `self._team.timezone_info`, so a `timezone_info=UTC` override on the constructor
+ # was silently ignored.
+ #
+ # The bug surfaces in `date_from_str` / `date_to_str`, not in the datetime
+ # objects themselves: both point to the same UTC instant but display in
+ # different timezones. `format_date` strips the tz suffix via
+ # `strftime("%Y-%m-%d %H:%M:%S")`, so the formatted string carries the
+ # team-tz wall clock under the bug and the UTC wall clock with the fix.
+ # That string is what flows into ClickHouse, so we assert against it.
+ #
+ # `date_from="-2d"` is day-based, so the midnight-of-day anchor differs between
+ # US/Pacific (08-24 00:00 PDT = 08-24 07:00 UTC) and UTC (08-24 00:00 UTC).
+ self.team.timezone = "US/Pacific"
+ self.team.save()
+
+ now = parser.isoparse("2021-08-25T00:00:00.000Z")
+ date_range = DateRange(date_from="-2d")
+
+ with_override = QueryPreviousPeriodDateRange(
+ team=self.team,
+ date_range=date_range,
+ interval=IntervalType.DAY,
+ now=now,
+ timezone_info=ZoneInfo("UTC"),
+ )
+ without_override = QueryPreviousPeriodDateRange(
+ team=self.team,
+ date_range=date_range,
+ interval=IntervalType.DAY,
+ now=now,
+ )
+ # The override must change the formatted output — otherwise the test wouldn't
+ # catch a regression of the fix.
+ self.assertNotEqual(with_override.date_from_str, without_override.date_from_str)
+
+ # Same setup with team on UTC and no override — should match the override result.
+ self.team.timezone = "UTC"
+ self.team.save()
+ utc_baseline = QueryPreviousPeriodDateRange(
+ team=self.team,
+ date_range=date_range,
+ interval=IntervalType.DAY,
+ now=now,
+ )
+ self.assertEqual(with_override.date_from_str, utc_baseline.date_from_str)
+ self.assertEqual(with_override.date_to_str, utc_baseline.date_to_str)