• iOS
  • Swift
  • WidgetKit

Scheduling a lunar calendar widget around midnight

Luminder prepares three timeline entries in advance and uses civil dates to schedule day changes in the device time zone.

The Luminder widget shows today’s lunar date. Someone can leave it on their Home Screen for several days without opening the app. If they add it on Friday and do not open Luminder all weekend, it still has to show the right date on Saturday and Sunday morning. The widget needs tomorrow’s content ready in advance, and it needs to follow the app’s choice of lunar calendar when the user changes settings.

A widget on iOS is not a small app that keeps running. WidgetKit, Apple’s framework for widgets, asks the widget’s code for content now and then, stores what it gets, and shows it on schedule. So the question is not “how does the widget notice midnight?” but “what should the widget hand WidgetKit so the right content is already there at midnight?”

Prepare three days in one timeline

A timeline is a list of entries. Each entry carries the content to show and the time from which it should be shown. If WidgetKit receives a new timeline on Friday, the widget already has entries for Saturday and Sunday, so both day changes can use data that was prepared in advance.

TodayLunarProvider gets today’s civil date in the device calendar and creates three entries. A civil date is a plain calendar date (year, month, day) with no time of day and no time zone attached. Each entry starts at local midnight on the corresponding day. The schedule handed to WidgetKit looks like this:

Local time Content
00:00 today Today’s lunar date
00:00 tomorrow Tomorrow’s lunar date
00:00 the day after That day’s lunar date
00:00 the following day Request a new timeline

All three entries are calculated when the timeline is built. Data for the first two day changes is already available, and the reload policy .after(refresh) tells WidgetKit when to request the next timeline. This is the earliest requested refresh; WidgetKit may update the view later than an entry’s date. Apple’s timeline documentation

Here is a simplified sketch of that provider. It is not Luminder’s source: CivilDate, TodayLunarProvider, TodayLunarEntry and TodayLunarEntryBuilder are the names used in this article, while entry(for:startingAt:lunarCalendar:) and selectedLunarCalendar() stand in for app code not shown here. The required placeholder(in:) and getSnapshot(in:completion:) methods are left out.

import WidgetKit

struct TodayLunarProvider: TimelineProvider {
    func getTimeline(in context: Context,
                     completion: @escaping (Timeline<TodayLunarEntry>) -> Void) {
        let calendar = Calendar.current                 // device calendar and time zone
        let today = CivilDate(Date(), in: calendar)
        let lunarCalendar = selectedLunarCalendar()     // read from the App Group

        // One entry per day, each starting at that day's local midnight.
        let entries = (0..<3).map { offset -> TodayLunarEntry in
            let day = today.adding(days: offset, in: calendar)
            return TodayLunarEntryBuilder.entry(for: day,
                                                startingAt: day.startOfDay(in: calendar),
                                                lunarCalendar: lunarCalendar)
        }

        // Ask for a new timeline at the start of the fourth day.
        let refresh = today.adding(days: 3, in: calendar).startOfDay(in: calendar)
        completion(Timeline(entries: entries, policy: .after(refresh)))
    }
}

getTimeline(in:completion:), Timeline(entries:policy:) and .after(_:) are WidgetKit’s API. TimelineProvider, TimelineReloadPolicy

Calculate midnight from the calendar date

To calculate those boundaries, the builder advances by a day on CivilDate, then converts each date to a timestamp in the device calendar. A timestamp is one exact instant, independent of time zone. The tempting shortcut is to take today’s midnight timestamp and add 86,400 seconds, the length of a normal day. That shortcut breaks on days that are not 24 hours long.

Daylight saving time (DST) makes such days. In the Australia/Sydney time zone, clocks jumped from 02:00 to 03:00 on Sunday, 4 October 2026, so that day lasted only 23 hours. Starting from Saturday’s midnight:

Day Civil-date midnight Midnight + n × 86,400 s
Sat 3 Oct 00:00 (UTC+10) 00:00 (UTC+10)
Sun 4 Oct 00:00 (UTC+10) 00:00 (UTC+10)
Mon 5 Oct 00:00 (UTC+11) 01:00 (UTC+11)
Tue 6 Oct 00:00 (UTC+11) 01:00 (UTC+11)

With the seconds shortcut, every entry after the DST change starts at 1 a.m., so the widget would show yesterday’s lunar date for an hour each night. Asking the calendar for the start of each civil date keeps every entry at midnight. I compiled and ran the date part of the sketch above, a minimal CivilDate built on Calendar.date(byAdding:value:to:) and Calendar.date(from:), with Swift 6.4.2 on macOS to produce this table; the WidgetKit part was not run.

The civil date also keeps two decisions apart. The device time zone determines when the widget rolls over. The chosen lunar calendar determines what it shows for that day. For example, if the iPhone is in a time zone other than Vietnam’s, the widget still changes entries at the iPhone’s midnight and can keep showing the lunar date from the Vietnamese calendar.

Share settings through the App Group

The widget runs as an extension, a separate program that ships inside the app but runs in its own process. An App Group is shared storage that the app and its extensions can all read. For example, if you choose the Vietnamese calendar in the app, the widget can read that choice the next time it builds a timeline.

The app writes the selected lunar calendar to its App Group so the extension can read it. Before the app writes a preference, the widget uses the same rule for choosing a regional default. When the shared value changes, the app calls WidgetCenter.shared.reloadAllTimelines() to request an early refresh. It also stores the language preference in the App Group and requests a reload when that setting changes.

Test entries in the app test target

TodayLunarEntryBuilder calculates display data from a civil date and the selected calendar. The entry type conforms to TimelineEntry in the extension, so the app test target can compile the builder without importing WidgetKit. The tests check local midnight in a fixed time zone, Lunar New Year, and a day when the Vietnamese and Chinese calendars disagree. Why those calendars can differ, and how Luminder checks its engine, is covered in why Luminder ships its own Vietnamese lunar calendar.

That keeps the date calculations easy to test. WidgetKit gets the remaining decisions about when each entry takes effect and when to ask for a fresh timeline. Luminder’s reminders face a similar limit from the other side: iOS keeps only a bounded queue of pending notifications, so the app plans its reminders by day and refills the queue.

Common mistakes with this kind of widget:

  • Adding 86,400 seconds to find the next midnight, which drifts by an hour across a DST change.
  • Expecting the widget to change at the exact second of an entry’s date; as noted above, WidgetKit may update the view later.
  • Forgetting to call reloadAllTimelines() after a shared setting changes, so the widget keeps showing the old calendar until its next scheduled refresh.

Conclusion

Prepare entries for predictable day changes, and let WidgetKit request the next timeline when needed. A timeline describes when content should change; it does not guarantee that the system will refresh the widget at the exact second. Keep the calculation tied to civil dates and test it across time-zone, daylight-saving and calendar boundaries.

Written by Duy Pham Independent developer. I build the apps on this site and write about how they work.

More about me