Skip to content

Lesson 6 of 9 · 25m

Calendars, Recurrence, and the Difference Between Meaning and Math

Model recurring human schedules with rules, timezones, exceptions, and materialized occurrences instead of brittle timestamp arithmetic.

Recurring time is where senior engineers become humble. "Every weekday at 9 AM" sounds trivial until the organizer travels, a DST rule changes, a holiday exception appears, a user edits one occurrence, and the mobile client materializes the series in a different zone. The correct object is not a timestamp plus addition. It is a recurrence rule with context.

Preserve intent, not only the next occurrence

A meeting or billing schedule usually means something civil: "09:00 in America/New_York every weekday." Store that intent:

rule:        FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR
local time:  09:00
zone:        America/New_York
start date:  2026-03-02
exceptions:  2026-05-25 holiday

If you store only the next UTC instant, then future DST transitions cannot reconstruct whether the user intended fixed elapsed time or fixed wall-clock time. iCalendar's RRULE model is imperfect, but it encodes the right idea: recurrence is declarative, exceptions are explicit, and occurrences are materialized from rules.

Months and years are not fixed durations

A month can mean calendar month or a billing interval. January 31 plus one month has no universally obvious answer. Product policy must choose clamping or skipping:

Jan 31 + 1 month -> Feb 28 or Mar 3?
Feb 29 exists only in leap years.

Subscription systems usually clamp to the end of the shorter month and then document whether later cycles return to the original anchor day. Picking one behavior without recording the anchor creates drifting renewal dates and angry customers.

Business days are cultural data

"Close of business" and "three business days" depend on jurisdiction, holidays, weekends, and sometimes industry-specific calendars. A US federal holiday calendar is not valid for a Japanese customer or a global exchange. Model holiday calendars as data and version them. Never hard-code weekday-only arithmetic and call it business-time logic.

Materialize carefully

Materialized occurrences make querying and notification delivery practical. They also create update hazards:

CREATE TABLE schedule_occurrences (
    schedule_id bigint NOT NULL,
    occurrence_start timestamptz NOT NULL,
    local_start date NOT NULL,
    local_start_time time NOT NULL,
    zone text NOT NULL,
    status text NOT NULL,
    PRIMARY KEY (schedule_id, occurrence_start)
);

When a rule changes, decide whether future occurrences are regenerated, historical occurrences frozen, and user-edited exceptions preserved. When tzdata changes, previously correct future instants may need recomputation even though the recurrence rule did not change.

Display ranges need end-time semantics

An all-day event on 2026-11-30 is not midnight-to-midnight UTC. It is a date range in a calendar system. Many calendaring systems use a half-open range: start inclusive, end exclusive. Keep that convention consistent in APIs and SQL queries, or events will overlap or vanish at boundaries.

Field notes

  • Always store the timezone with a recurrence rule; a floating recurrence follows the viewer, while a zoned recurrence belongs to a place.
  • RRULE strings are easy to copy and hard to validate. Treat untrusted recurrence input as hostile and bound expansion counts.
  • Leap-year bugs often appear only on February 29 or in renewal jobs that run monthly.
  • Keep original user intent in audit logs, not merely the computed first occurrence.

Was this lesson helpful?

Sign in to rate