Daily totals in UTC
Task
Events arrive with timestamps in different time zones, such as "2026-03-02T01:00:00+05:30". Write daily_totals_utc(events), where each event is {"ts": ..., "amount": ...}, that returns {"YYYY-MM-DD": total} grouped by the UTC date.
Python 3.14 running in your browser (Pyodide). Standard library only. print() output is shown below the results.
Tests
| Call | Expected |
|---|---|
daily_totals_utc([{'ts': '2026-03-02T01:00:00+05:30', 'amount': 10}, {'ts': '2026-03-01T20:00:00+00:00', 'amount': 5}, {'ts': '2026-03-02T12:00:00+05:30', 'amount': 7}]) | {'2026-03-01': 15, '2026-03-02': 7} |
daily_totals_utc([{'ts': '2026-03-01T22:30:00-05:00', 'amount': 3}]) | {'2026-03-02': 3} |
daily_totals_utc([]) | {} |
- The first 10 characters are the local date, not the UTC date.
datetime.fromisoformat(ts)keeps the offset;.astimezone(timezone.utc)converts it.
Show solution
from datetime import datetime, timezone
def daily_totals_utc(events):
totals = {}
for e in events:
day = datetime.fromisoformat(e['ts']).astimezone(timezone.utc).date().isoformat()
totals[day] = totals.get(day, 0) + e['amount']
return totals01:00 in India (+05:30) is 19:30 the previous day in UTC, so slicing the string files the event under the wrong day. Convert to UTC before deriving the date, and store timestamps in UTC throughout a pipeline.
Why this works
01:00 in India (+05:30) is 19:30 the previous day in UTC, so slicing the string files the event under the wrong day. Convert to UTC before deriving the date, and store timestamps in UTC throughout a pipeline.
Revise in the Python for Data Engineers guide, or test yourself with the Python practice sets.