Daylight saving time bug: why a 120-day streak hit 1

A daylight saving time bug reset login streaks when UK clocks went back. Why today's offset misreads past days, and the fix: store each login's local day.

By

A daylight saving time bug reset login streaks on a fitness app I work on the weekend UK clocks went back. People who opened the app every day were dropped to 1, and one user went from 120 to 1 without missing a day. The mistake only shows when a user's UTC offset changes between two logins, so it can sit unnoticed for most of a year. This post explains why you can't derive a past local day from today's offset, what the same bug does in spring, and the fix I shipped: store the local day when the event happens.

What the daylight saving time bug looked like

A login streak needs one number: how many calendar days separate this login from the last one that counted. A gap of 1 adds to the streak. A gap of 0 means the same day, so nothing changes. Anything larger resets it to 1.

The last login that counted was stored as a UTC time. To get the gap, the code turned that UTC time back into a local day, using the offset the user has today.

UK clocks went back on 25 October 2026, moving the offset from UTC+1 to UTC+0. Take a login at 00:24 on 24 October, local time. In UTC that is 23:24 on 23 October. After the clocks changed, the code added today's offset of zero and got 23:24 on 23 October. The stored login had moved to the previous day.

So a user who opened the app on the 23rd, the 24th and the 25th looked like they had skipped a day. The gap came out as 2, and the streak went to 1.

Why do streaks break when the clocks change?

Streaks break because the code applies today's UTC offset to a time recorded under a different offset. A past local day depends on the offset in force at that moment, and daylight saving changes it twice a year. Any login close enough to midnight then lands on the wrong calendar day, so the gap between days comes out wrong.

The MDN page for getTimezoneOffset shows the same idea from the other side: in a region that shifts in and out of daylight saving, the offset depends on the date you ask about. An offset describes a moment. It is easy to store it as if it described a person.

This also explains why the bug hides so well. On almost every day of the year, today's offset and the offset at the last login are the same, and the arithmetic gives the right answer. With a one-hour shift, only logins inside one hour of midnight can cross the line. Test data on a fixed offset will never reach that branch.

Clocks going forward break it the other way

In spring the offset grows, so an old login reads as later than it was. That produces two different wrong answers, and neither of them is a reset.

A day can be skipped. Take a login at 23:40 on the Saturday night before the change. Read back with the new offset, it becomes 00:40 on Sunday. When that user opens the app on Sunday the gap is 0, so the code treats it as the same day and the streak does not grow. They earned a day and it never counted.

A missed day can be forgiven. Take a login at 23:40 on Friday, nothing on Saturday, then a login on Sunday. Friday's login reads back as 00:40 on Saturday, the gap comes out as 1, and the streak carries on over a day the user really missed.

So one piece of date maths was wrong three ways across the year. Zones without daylight saving never saw any of it.

The fix: store the local day with each login

I stopped rebuilding the past. The stats row now has a column holding the local day of each streak update, written at the moment it happens, when the correct offset is the one in hand. The gap is today's local date minus that stored date.

const DAY_MS = 86_400_000;

// offsetMinutes is minutes east of UTC, in force at the moment of the event.
// getTimezoneOffset() returns the opposite sign, so flip it if it comes from there.
function localDay(at: Date, offsetMinutes: number): string {
  return new Date(at.getTime() + offsetMinutes * 60_000).toISOString().slice(0, 10);
}

// Date-only ISO strings parse as UTC, so this subtraction never sees a clock change.
function nextStreak(streak: number, lastDay: string, today: string): number {
  const gap = (Date.parse(today) - Date.parse(lastDay)) / DAY_MS;
  if (gap <= 0) return streak;
  return gap === 1 ? streak + 1 : 1;
}

This is a sketch of the logic with made-up names. The point is the type of the stored value. A local day is a calendar date with no time and no zone, which is what Temporal.PlainDate models in JavaScript and a plain date column models in a database. Once you hold that, two dates subtract cleanly and the offset drops out of the comparison.

My default for gap <= 0 is to leave the streak alone. Someone who flies west can legitimately see today's date go backwards against their last login, and that should cost them nothing.

Zones without daylight saving behave exactly as before, because their offset never moved and the old arithmetic was already right for them.

Backfilling without an app release

A new column starts empty, so existing users needed a value before their next login. I backfilled it from the stored offset, so the first comparison after the change already had a local day to work with.

A backfill like this repairs the comparison from then on. It does not hand back a streak that was already reset. Restoring those is a separate product decision, and I would treat it as one, since you need to be sure which resets were wrong.

The part I cared about most was keeping the API still. Request and response are unchanged, so every released app build is unaffected. The whole fix lives on the server, with nothing for users to update. That mattered on the calendar too, because US clocks were due to go back on 1 November, a week after the UK.

Old app builds stay in the field for a long time, and I try to treat their contract as fixed. I wrote about the same caution in removing unused API endpoints safely.

How I'd test date logic like this

These are the cases I now want in any test for day-based logic:

  • Both transitions. A pair of logins spanning the autumn change and a pair spanning the spring one.
  • The hour either side of midnight. Logins at 00:24 and 23:40 are the ones that move, so midday fixtures prove nothing.
  • A zone without daylight saving. Its results should not change at all. Running old and new code over real rows and comparing them is a quick way to prove it, which is the method in verifying a refactor by diffing old and new output.
  • A real miss. Make sure a skipped day still resets, so the fix has not simply made streaks harder to lose.

The same family of bug shows up in the browser, where a date formatted in the visitor's locale can disagree with the server. I covered that in fixing React hydration errors from dates. And if a streak update can arrive twice at once, the day column gives you something to enforce in the database, along the lines of fixing race condition duplicates with a unique index.

The short version

Any time you ask "what local day was this?" about the past, today's offset is the wrong input. Daylight saving moves it, and logins near midnight land on the wrong date: a false reset in autumn, a skipped or forgiven day in spring. Store the local day as a date when the event happens, subtract dates, and keep the API unchanged so released apps need nothing. If you do one thing today, search your code for places that convert a stored UTC time using the user's current offset.

More of my backend and mobile work is on the projects page.

Want this kind of work on your product?

Get in touch