Is your app’s “today” secretly UTC? What an audit of my streak logic found
I have a streak counter and a daily limit in an AI app. When I had Claude Code audit them, the first finding was that every “today” in the app was a UTC day. For someone in Pakistan the day resets at 5 a.m. For someone in California it resets in the late afternoon.
Two more things turned up. There were two different streak numbers: the dashboard computed one from the user’s saved results, while the results page showed a stored value that only one route ever wrote, so it was effectively frozen. And the free dashboard only loaded the latest 12 saved results for display, then computed the streak from those rows, so the free streak was capped at about 12 by accident.
The plan: save each user’s time zone once, compute one streak everywhere from the dates of all their results, and count the daily limit per local day. The users table already had a nullable timezone column that nothing used.
The part I’d have gotten wrong alone: the logged-in role cannot write that column (the database only lets it update a few fields on its own row), so the browser can’t save it directly. A small server route does it. It requires a signed-in user, takes the row id from the session and never from the request body, skips the write if the value hasn’t changed, and reads the row back to check that exactly one row changed. The silent failures I’ve had before taught me not to trust a write I haven’t verified.
Validation also needed more than a try/catch. Intl.DateTimeFormat will accept raw offsets like +05:00, so I also require the name to start with a letter and be at most 64 characters.
The honest weak spots: users who never open the dashboard keep no saved zone, so they fall back to UTC. If someone uses two devices in different zones, the last one wins. And since the zone comes from the client, a user could post a different zone to shift when their day resets and get one extra daily result.
How do you handle “today” in apps with daily limits: store a zone per user, send it with every request, or something else? And do you guard against people changing it to game the limit?
Replies
We made opposite choices for our two counters, on purpose. The daily song limit stays a UTC day, so it resets at one fixed moment for everyone instead of following whatever the device claims its zone is. The daily bonus streak uses the user's own dates, with the offset sent on each claim, and that is where your last weak spot comes in. The backend holds the offset: a different one is accepted only when the last claim is a day old, and two claims are always at least 12 hours apart, so changing the phone's zone or clock cannot buy a second day.
One more that rhymes with your 12 rows cap: the limit counter lives on the user's own document instead of being counted from saved songs, because users can delete songs, and a count of surviving rows would hand back a slot with every deletion.