I Fixed My Sleep Schedule With Code

Recently I had to shift my sleep schedule to wake up earlier. In the past, I did this abruptly (and suffered the consequences).

Sleep science recommends slowly shifting one’s sleep schedule. I had a deadline to adopt the new schedule, so I created nemui, an app to plan a progressive transition.

The interface and user experience

I schedule my sleep through the iOS Health app. It allows me to set a sleep goal (hours of sleep I need), bedtime and wake up hours.

I wanted to replicate Apple’s UI: it’s a good design and user experience, I’m used to it, and I liked the challenge. This took 60% of the time spent on the project.

Here’s the result. Have fun dragging the handles around:

hr
min
BEDTIME
WAKE UP

It’s not pixel perfect, but it’s almost identical to Apple’s. In fact, it’s better! You can click/tap on the bedtime and wake up time to enter the number directly, something you can’t do on iOS.

Once you’ve set the current and desired schedules, you need to choose the target date. It suggests a day based on sleep research data (15 min shift per day), but you can change it. You’ll see a warning if the change is too aggressive:

Warning when shifting too many minutes per day Warning when shifting too many minutes per day

Finally, you get the daily plan:

Daily sleep schedule plan Daily sleep schedule plan

Clicking on “Add to calendar” downloads a Calendar (.ics) file with one event per day. These events show when to sleep and wake up each day, and include a reminder 30 minutes before bedtime to start winding down.

If you revisit the site, it’ll show you the plan for the upcoming nights (all data is stored locally).

I named it nemui: nemu (sleep) and i (transition), reading as 眠いnemui (sleepy) in Japanese.

I was about to consider the project done when I remembered about Daylight saving time (DST).

Daylight saving time

More than a third of the world’s countries use DST. I had to support it, said my perfectionism.

And I listened.

nemui supports DST for two use cases. First, if you’re adjusting your schedule through a DST change, it warns you and adjusts the times:

Warning crossing DST Warning crossing DST

Second—and this is my favourite—, a few days before DST starts, you can set identical current and desired schedules to slowly adjust the timing (you’ll go to bed and wake up a bit earlier every day) and avoid abruptly losing an hour of sleep.

Supporting DST was not easy.

First, vanilla JavaScript is not great with dates. It doesn’t support timezones and “calculations across DST are notoriously difficult to work with” (Temporal can’t come soon enough!).

Second, DST is weird.

Countries start/end DST on the first, second, or last Sunday of a given month, or the last Thursday/Friday. Or the Friday before the last Sunday.

Naturally, the hour of transition varies too: it can be at 00:00, 01, 02, 03… Even at 24:00! (thanks, Chile!)

And there’s one more thing! You’d think all regions shift by an hour, right? Wrong.

There’s a group of islands between Australia and New Zealand, called Lord Howe Island, with 30-minute DST transitions. That’s right. On the first Sunday of April at 02:00, clocks will be turned backwards to 01:30. And the change will be reversed on the 5th of October.

Even though fewer than 400 people live there, I made sure my app is compatible with this shift. I had to.

The verdict

I’m happy to report that my schedule was smoothly and almost painlessly shifted. My body successfully adapted to the new schedule, unlike the times I did this abruptly. I plan on using nemui again when DST start approaches.


Extra: mini-project

Before creating nemui, I made a little web-app to check when the next DST change is (inspired by a Claude artifact created by Simon Willison), based on your device timezone. Here’s a screenshot:

Next DST date screenshot Next DST date screenshot

Visit dst.osc.garden to see when the next DST change is in your timezone.