: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix: Make "/events" API endpoint return timestamps in UTC time instead of project timezone (#67205)
souheyl gouadriafabfb332af43
diff --git a/posthog/api/event.py b/posthog/api/event.pyindex 0dd970641a11..5657fcaf2997 100644--- a/posthog/api/event.py+++ b/posthog/api/event.py@@ -5,7 +5,7 @@ import randomimport urllibimport builtinsimport dataclasses-from datetime import datetime+from datetime import UTC, datetimefrom typing import Any, Iterator, List, Optional, Union, cast # noqa: UP035from django.conf import settings@@ -181,7 +181,7 @@ class EventViewSet() -> str:params = request.GET.dict()reverse = "-timestamp" in order_by- timestamp = last_event_timestamp.astimezone().isoformat()+ timestamp = last_event_timestamp.replace(tzinfo=UTC).isoformat()if reverse:params["before"] = timestampelse:diff --git a/posthog/api/test/test_event.py b/posthog/api/test/test_event.pyindex cee303f7b488..c1cdf0146b11 100644--- a/posthog/api/test/test_event.py+++ b/posthog/api/test/test_event.py@@ -870,6 +870,37 @@ class TestEvents(ClickhouseTestMixin, APIBaseTest):assert parser.parse(response["results"][0]["timestamp"]) > parser.parse(response["results"][-1]["timestamp"])assert "before=" in response["next"]+ def test_retrieve_event_returns_utc_timestamp_when_project_timezone_is_not_utc(self):+ self.team.timezone = "Africa/Algiers"+ self.team.save()+ event_uuid = _create_event(+ team=self.team,+ event="watched movie",+ distinct_id="1",+ timestamp="2026-06-30T20:44:19.407000Z",+ )+ flush_persons_and_events()++ response = self.client.get(f"/api/projects/{self.team.id}/events/{event_uuid}/")+ assert response.status_code == status.HTTP_200_OK+ assert response.json()["timestamp"] == "2026-06-30T20:44:19.407000+00:00"++ def test_list_events_returns_utc_timestamp_when_project_timezone_is_not_utc(self):+ self.team.timezone = "Africa/Algiers"+ self.team.save()+ with freeze_time("2026-06-30T20:45:00Z"):+ _create_event(+ team=self.team,+ event="watched movie",+ distinct_id="1",+ timestamp="2026-06-30T20:44:19.407000Z",+ )+ flush_persons_and_events()+ response = self.client.get(f"/api/projects/{self.team.id}/events/?distinct_id=1").json()++ assert len(response["results"]) == 1+ assert response["results"][0]["timestamp"] == "2026-06-30T20:44:19.407000+00:00"+def test_action_no_steps(self):action = Action.objects.create(team=self.team)diff --git a/posthog/models/event/legacy_events_query.py b/posthog/models/event/legacy_events_query.pyindex 57f0a32a3169..6f7e258d7d80 100644--- a/posthog/models/event/legacy_events_query.py+++ b/posthog/models/event/legacy_events_query.py@@ -92,7 +92,9 @@ def get_one_event(team: Team, pk: str) -> Optional[dict]:right=ast.Constant(value=uuid.UUID(pk)),),)- result = execute_hogql_query(query, team=team, query_type="event_detail")+ modifiers = create_default_modifiers_for_team(team)+ modifiers.convertToProjectTimezone = False+ result = execute_hogql_query(query, team=team, query_type="event_detail", modifiers=modifiers)if not result.results:return Nonereturn dict(zip(EVENT_LIST_SELECT_COLUMNS, result.results[0]))@@ -112,6 +114,8 @@ class LegacyEventsListQuery:self.team = teamself.user = userself.modifiers: HogQLQueryModifiers = create_default_modifiers_for_team(team)+ # Return raw UTC timestamps instead of shifting DateTime columns into the project timezone.+ self.modifiers.convertToProjectTimezone = Falseself.database = Database.create_for(team=team, user=user, modifiers=self.modifiers)def run(diff --git a/posthog/models/event/util.py b/posthog/models/event/util.pyindex aa9f8a50e30e..ffc235a371a0 100644--- a/posthog/models/event/util.py+++ b/posthog/models/event/util.py@@ -402,7 +402,7 @@ class ClickhouseEventSerializer(serializers.Serializer):@extend_schema_field(serializers.DateTimeField())def get_timestamp(self, event):dt = event["timestamp"].replace(tzinfo=UTC)- return dt.astimezone().isoformat()+ return dt.isoformat()@extend_schema_field(serializers.DictField(allow_null=True))def get_person(self, event):