DEV Community

BeforeGo
BeforeGo

Posted on Fully Autonomous

Flying home for Christmas: why your countdown can be a day off after a time-zone change

Every December, a few people open a countdown app after a long flight and find that Christmas has moved. It is December 20 in Tokyo and the countdown says 5 days. They land in Los Angeles, where it is also December 20, and it says 4. The date under the number now reads December 24.

Nothing is wrong with the clock, and the user didn't change anything. The bug is in how the app stored "December 25" in the first place. We had this bug in our own countdown app, and the tests we wrote to pin it down are a good map of what can go wrong.

The bug: storing a day as a moment

Most date pickers hand you a Date, and a Date is an instant: a single point on the timeline, the same everywhere on Earth. If you save that instant, you have saved "December 25, 14:00 in Tokyo". That is 21:00 on December 24 in Los Angeles.

Then two things leak into every count you show later:

  • The time zone the event was created in. Read back in Los Angeles, the date label is December 24.
  • The hour the user happened to tap Save. An event saved at 00:10 and one saved at 23:50 are almost a day apart, even though the user picked the same day in both.

Before we fixed it, we measured an all-day December 25 created in Tokyo and read in Los Angeles. The date label said 12-24. Of 72 sampled moments, 31 showed a different number of days left in Los Angeles than in Tokyo, and which ones differed depended on the hour the event had been created.

Christmas is a date on the calendar, not a moment in time. It is December 25 wherever you are standing.

Rule 1: an all-day event is a floating date

We stopped storing all-day events as instants. We store them as "floating" dates: only the year, month and day survive, and the time of day is thrown away when you save.

// At the moment a picked date becomes an event:
let parts = calendar.dateComponents([.year, .month, .day], from: picked)
let stored = utcCalendar.date(from: DateComponents(
    year: parts.year, month: parts.month, day: parts.day))   // 00:00 UTC
Enter fullscreen mode Exit fullscreen mode

It is still a single Date column, so there's no migration and the sync schema doesn't change. The difference is what the value means. For an all-day event, its UTC year, month and day are the date the user meant, and the hour carries no meaning at all. When we read it back, we rebuild that year, month and day in whatever calendar the phone is using right now.

Two tests keep it honest:

  • The same pick made at 00:00, 08:00, 14:00 and 23:00 in Tokyo must collapse to one stored value.
  • Picked in Tokyo, the date must still read 2026-12-25 in seven zones, from Kiritimati (UTC+14) to Midway (UTC−11).

Rule 2: count days between midnights, never divide by 86,400

"Days left" is the number of midnights between the start of today and the start of the target day, in the phone's current calendar.

let today = calendar.startOfDay(for: now)
let target = calendar.startOfDay(for: event.date(in: calendar))
let days = calendar.dateComponents([.day], from: today, to: target).day ?? 0
Enter fullscreen mode Exit fullscreen mode

Dividing the seconds left by 86,400 gives you a number that drifts with the time of day. It also jumps by one on the night clocks change for daylight saving, because that day is 23 or 25 hours long.

Rule 3: the promise is per local date, not per instant

When we wrote the test for this, the first version failed for a good reason, and that taught us what "correct" means here.

At the same instant, two zones can be on different calendar days. At 03:00 UTC on December 20 it is already the 20th in Tokyo, but still the 19th in Los Angeles. So "5 days" in Tokyo and "6 days" in Los Angeles are both right.

What must hold is the sentence a person would actually say: if it is December 20 where you are, Christmas is 5 days away, and it doesn't matter where you are or what hour of the day you ask. The test checks that for every zone at 00:30, 06:30, 12:30 and 23:30 local time. With the old instant-based storage, Los Angeles read 4.

Rule 4: a timed event really is an instant

Not everything is a floating date. A rocket launch at 05:30 UTC on December 25 happens at one moment for everyone. In Los Angeles it really is on the evening of the 24th, and the countdown should say so.

So the app keeps two kinds of event and never mixes them up. All-day events float. Timed events stay as instants and are never rebuilt in the local calendar. If you add a time to "Christmas dinner", the countdown now refers to a moment, and after you fly it will show local time, which is what you'd expect for a dinner reservation.

The widget part: when does the number change?

A Home Screen or Lock Screen widget doesn't run code all the time. The app gives iOS a list of entries ahead of time, and iOS shows each one when its time comes.

Our countdown widget gives iOS one entry for now and one for each of the next seven local midnights, and asks for a new list after the last one. The day number can only change at midnight, so refreshing more often uses up the refresh budget and gains nothing. Hours and minutes, for countdowns that show them, are drawn by the system's own timer text, which ticks without needing new entries.

The catch for travellers is that those midnights are fixed instants, worked out in the time zone the phone was in when the list was made. Changing time zones doesn't move instants that have already been computed. iOS may ask the widget for a fresh list sooner, but nothing guarantees it will. So for a short while after a long flight, a widget can tick over at the old midnight instead of the new one.

Floating storage keeps the day count right whenever the list is rebuilt. Until then, the widget may simply be early or late. In our app, any edit to an event rebuilds every widget's list straight away.

If you're the one flying home

  • Use an all-day event for Christmas. Don't give it a time unless you mean a specific moment, like a flight or a dinner.
  • If the widget looks a day off right after you land, open the app and save the event again. The widget will be rebuilt for the time zone you're in now.
  • Count down to the flight separately, as a timed event. That one should follow the clock.

The app these examples come from is our iPhone countdown widget, Countdown Widget: Any Event. All of the above applies to any countdown app you might be using, though: if yours moves Christmas when you cross the Pacific, it is almost certainly storing December 25 as an instant.


This post was written by an AI agent (Claude) at my request, from the app's source code and its tests (FloatingDate, FloatingDateTests, the widget's timeline provider). If anything looks wrong, please say so in the comments.

Top comments (0)