The situation
The app stores every event with a UTC timestamp. The India business team gets a daily report: events and active users per day.
An AI assistant wrote the job in the answer cell. The weekly totals match the raw event count exactly, so it was signed off. Then the support team in Pune noticed that activity from just after midnight keeps showing up under the previous day.
Find the bug and fix it.
Input tables
app_events
One row per event.
| Column | Type | Description |
|---|---|---|
event_id |
BIGINT | Unique per event. |
user_id |
BIGINT | The user who triggered the event. |
event_ts |
TIMESTAMP_NTZ | When the event happened, in UTC. |
Requirement
Return events and active users per day, where a day is a calendar day in India.
- India Standard Time (IST) is UTC+05:30, all year. There is no daylight saving.
event_dateis the date of the event in IST. An event at 18:30:00 UTC is at 00:00:00 IST on the next day.eventsis the number of events on that date.usersis the number of different users with at least one event on that date.
Expected output
One row per India calendar day that has events.
| Column | Type | Description |
|---|---|---|
event_date |
DATE | Date in IST. |
events |
BIGINT | |
users |
BIGINT | Distinct users. |
The AI-written code
from pyspark.sql import functions as F def solve(spark): """Daily events and active users for the India report.""" events = spark.table("app_events") # Derive the calendar date of each event events = events.withColumn("event_date", F.to_date("event_ts")) # Count events and distinct users per day result = events.groupBy("event_date").agg( F.count("*").alias("events"), F.countDistinct("user_id").alias("users"), ) return result
Explanation
Show explanationGives the answer away. Try the problem first.
The bug
events = events.withColumn("event_date", F.to_date("event_ts"))
event_ts is in UTC. to_date cuts the time off and keeps the date, so event_date
is the UTC date. The report is supposed to be per India calendar day.
India is 5 hours 30 minutes ahead of UTC. From midnight to 05:29 in India, the UTC clock still shows the previous day. An event at 00:10 IST on 4 March is stored as 18:40 UTC on 3 March, and the code books it on 3 March.
In this data, 101 of the 302 events fall in that window and land on the wrong day.
Why it passed review:
- The weekly total is exactly right. Every event is counted once; it is only in the wrong bucket.
- The result has the same seven dates either way.
- Each daily number is plausible. On 3 March the event count is even identical by coincidence (51 both ways), although the events behind it are not the same ones.
The fix
Shift the timestamp to India time first, then take the date:
india_time = F.col("event_ts") + F.expr("INTERVAL 330 MINUTES")
events = events.withColumn("event_date", F.to_date(india_time))
The order matters. The date has to be derived from the shifted timestamp. Adding 5.5 hours to something that is already a date does nothing useful.
Spark also has a function for this:
F.to_date(F.from_utc_timestamp("event_ts", "Asia/Kolkata"))
For India both give the same result, because IST has no daylight saving and the
offset never changes. For a zone that does change its clocks, such as Europe/London
or America/New_York, only the named time zone is correct: the offset is different in
summer and winter, and a fixed interval would be wrong for half the year.
Use the region name (Asia/Kolkata), not the abbreviation IST. Abbreviations are
ambiguous: IST is also Irish Standard Time and Israel Standard Time.
Common wrong answers
Adding 5 hours, or 6. India's offset has a half hour in it. Both versions give seven rows with wrong numbers, because events in the remaining half hour are still on the wrong side of midnight.
Subtracting instead of adding. Returns 8 rows: the dates move the wrong way and a 28 February appears. To go from UTC to a zone that is ahead of UTC, add.
Changing the session time zone and hoping. event_ts is a TIMESTAMP_NTZ, a
timestamp without a time zone. Spark does not convert it when
spark.sql.session.timeZone changes; it is just a date and a time. The conversion has
to be written explicitly.
The broader lesson
Store timestamps in UTC, and convert at the last moment, for the audience of the
report. And say in the column name or its description which zone a date belongs to.
"event_date" in a table used by teams in three countries will be read three ways.
Edge cases in the data
| Case | Where | What it catches |
|---|---|---|
| Events between 18:30 and 23:59 UTC | 101 events | The bug itself. |
| An event at exactly 18:30:00 UTC | 3 March | It is midnight IST: it belongs to 4 March. |
| An event at 18:29:59 UTC | 3 March | One second earlier: still 3 March in India. |
| Same seven dates in UTC and IST | whole data set | Shows that a row count check proves nothing here. |
| Events between 18:00 and 18:30 UTC | several | Adding 6 hours instead of 5:30. |