Tech challenge · Python

Daily sales totals are wrong near midnight

MediumPython

Orders carry UTC timestamps like 2026-03-01T19:45:00Z. The finance team in India says some late-evening orders appear on the wrong day in the daily totals. The code does ts[:10] to get the date. What is wrong, and how do you compute the business date?

Think it through or sketch an answer first, then open the hint or the solution.

Show a hint

Which day is 19:45 UTC in India (UTC+5:30)?

Show the solution

Slicing the string gives the UTC date, but the business day is in IST. 19:45 UTC is 01:15 the next day in India, so that order belongs to 2 March. Parse the timestamp as an aware datetime, convert it with zoneinfo to the business time zone, then take the date.

Example (Python)

from datetime import datetime
from zoneinfo import ZoneInfo

IST = ZoneInfo('Asia/Kolkata')

def business_date(ts: str):
    utc = datetime.fromisoformat(ts.replace('Z', '+00:00'))
    return utc.astimezone(IST).date()

print(business_date('2026-03-01T19:45:00Z'))   # 2026-03-02

Common mistakes

  • Adding a fixed 5.5 hours by hand: it works for India but breaks for any zone with daylight saving.
  • Parsing without the zone so the datetime is naive, then converting: Python assumes the machine's local time.

Learn more

More Python challenges

Today's challenge and the full archive