Architecture
Just say when
In this post
Every date field in Milieu takes words. A to-do’s When and Deadline open a date picker with a field to type in, so a deadline can be “next fri” or “the last working day of november”. Its Repeat opens a repeat picker, which takes “every other thursday until christmas”. In the Calendar, the New event card takes a whole sentence: “Lunch with Ann tomorrow 1–2pm at Dishoom every week”.
All of that is read by one library, which we’ve written in Rust and released as open source. It’s called SayWhen. This post covers what we wanted from our date fields, why it’s one library for the web and the apps, how it reads a phrase and how you can use it.
What we wanted from a date field
Things and Fantastical showed how good it feels to type a date instead of picking one. We wanted that in every date field, for more of the ways people say dates, so nobody has to find a square on a calendar or work out which field takes the time. That meant five things.
The date picker should be useful before you’ve finished typing. On a Monday, “t” already offers Today, Tomorrow and Thursday. (Tuesday is Tomorrow, so it isn’t offered twice.) “3” could be the 3rd, or three days, weeks, months or years from now. A half-typed word still counts, so “next f” is a Friday before you reach the “r”.
- nto next fri nothing yet
One date picker should do for every kind of date. A form usually picks its control by the kind of date it wants: a calendar for a meeting, a few presets like Tomorrow for a task, and separate day, month and year boxes for a birthday, which no calendar is any help with. Milieu has one date picker for all of them. For a to-do’s When, it offers Today, This Evening and the next four weeks. For a deadline, it offers the weeks. For a birthday, which is long past and may have no year, it offers only its field, and reads “3 dec”, “3/12/90” or “3 december 1990” as a birthday rather than a day to come.
When people disagree about a phrase, the date picker shouldn’t pick for them. Said on a Tuesday, “next friday” could mean the Friday of next week or the one in three days, and people disagree about which. The date picker offers both, the Friday of next week first. Said on a Friday, Saturday or Sunday, only one reading makes sense, so only one is offered.
An event should fit in a sentence, typed in whatever order it comes to mind. “Lunch with Ann tomorrow 1–2pm at Dishoom every week” has a title, a day, a time, a place and a repeat. A bare number should mean what a person would mean by it: “breakfast 6” is at 6am and “dinner 6” at 6pm, and with no end given, breakfast lasts an hour and dinner two.
Lunch,titlewith,titleAnn,titletomorrow,title1-2pm,titleat,titleDishoom,titleevery,titleweek,title
- event
- nothing read yet
A reminder should keep what you said, not the date it worked out to. A reminder set for “the day before at 9am” on a Wednesday event should move to Wednesday at 9am when the event moves to Thursday.
One library for the web and the apps
Milieu first read these phrases with TypeScript, inside the web app. When we began the Mac and iPhone apps, we wanted the same reading to run natively there, and we wanted the apps and the web app to share one implementation rather than keep two in step. So we wrote it again in Rust, as SayWhen: a fresh design, not a line-by-line port. The apps link SayWhen natively, and the web app runs the same code through WebAssembly.
How SayWhen reads a phrase
Each method answers one question
Take “next week”. In a Deadline field, it’s a day: Monday 23 November, said on Monday 16 November. In a calendar view, it’s a span from Monday to Sunday, and the view wants to know the span is a week so that it can show a week. In a Repeat field, “next week” means nothing at all.
A parser that takes a string and returns a date has to guess which of those the person typing meant. You ask SayWhen through a Reader, which has a method for each question instead:
let reader = Reader::new(Options::new(Locale::Uk));
let today = Day::new(2026, 11, 16).unwrap();
// Mon 23 Nov
reader.days("next week", today);
// Mon 23 Nov to Sun 29 Nov, a week
reader.span("next week", today);
// Every other Thursday, to 25 Dec
let starts = today;
reader.repeats("every other thursday until christmas", starts, today);
// -P1D@09:00, from any day
reader.offset("the day before at 9am"); Others read a time of day, a whole event, a date picker’s search as it’s typed, and when something is, like “tomorrow 2–4pm”, for finding a free slot. The demo below asks five of these questions of whatever you type: a day, a span, a repeat, a time and an event. Try “half three”, which is only a time; “every other thursday until christmas”, which is only a repeat; and “next week”, which is both a day and a span.
nothing
nothing
nothing
nothing
nothing
The caller supplies the day, the locale and the calendar
SayWhen has no clock, so every call that needs to know the day is told it. That’s how the two Fridays come out right on any day of the week.
The caller also gives a locale, which decides the order of a date in figures, which day starts a week and how a day is written back. “3/4” is 3 April in the UK and 4 March in the US, and Monday 30 November is “Mon 30 Nov” in one and “Mon, Nov 30” in the other. For now, SayWhen reads only English, in six locales: the UK, the US, Ireland, Australia, New Zealand and Canada. We aim to support more.
Last, the caller supplies a calendar of its public holidays, because they can’t be worked out by rule. The early May bank holiday is the first Monday in May, except in 2020, when the UK moved it to Friday 8 May for the 75th anniversary of VE Day. In 2022 the spring bank holiday moved to Thursday 2 June for the Platinum Jubilee, and an extra one was added on Friday 3 June. Another was added on 19 September 2022 for the Queen’s funeral. A library with its own rules would get both years wrong. So SayWhen counts working days around the holidays it’s given, and with none, a working day is Monday to Friday. In the UK, Milieu gives it gov.uk’s list of bank holidays.
The calendar can also name days that aren’t holidays, each on the dates it falls that year. That’s how “mother’s day” and “a week after ramadan” read, and a named day can last longer than a day: “ramadan” as a span is the whole month, and “a week after ramadan” counts from its last day. Only a few names are built in, for days whose date never changes, such as Christmas, Halloween and Guy Fawkes Night. Easter, Thanksgiving and Diwali move from year to year, so they come from the caller.
A phrase becomes a tree before it becomes a day
Inside, the reader builds a small tree from four kinds of piece:
- a day: “today”, “fri”, “9 dec”, “christmas”;
- a span: “next week”, “november”, “q1 2027”;
- a day picked out of a span: “the last working day of november”, “friday next week”; and
- a day moved by some amount: “2 weeks after”, “the day before”, “3 working days before”.
Any piece can sit inside another:
// Sun 29 Nov, read as "the day before Mon 30 Nov,
// the last working day of November 2026"
reader.days("the day before the last working day of november", today); The tree doesn’t depend on the date: it’s worked out against today only once it’s built. That’s how the two-Fridays demo can read one phrase on seven different days. A phrase can also make more than one tree: in “next friday”, “next” can belong to the week or to the Friday. The reader keeps every tree that uses all the words, so “next friday” gives two days. Each reading also says how it was read, as in the comment on that example, so the date picker can show its working under the day.
An event’s words are read away from its title
Most of an event’s sentence isn’t a date. So Reader::event starts by treating every word as part of the title, then makes a series of passes, each taking away the words it can read as something else: a time zone, a range of days, a time, a day, a place (“at Dishoom”) and a repeat. Whatever is left is the title. Every word comes back with what it was read as, which is how the event demo above colours them.
The order of the passes settles ambiguity. Times go before days, so “friday 3” is at three o’clock, not on the 3rd. Time zones go before places, so in “9am New York time”, New York isn’t where the meeting is. Ranges go before single days, so “6–8 nov” stays one trip.
A bare hour takes am or pm from the title. Words like “breakfast”, “dinner” and “drinks” each have the hours their events usually happen in, and a bare hour takes whichever half of the day falls inside them. So “breakfast 6” is in the morning.
Repeats and reminders are written in iCalendar’s terms
Repeats come out as iCalendar rules (RRULEs), so a repeat can go straight to a phone’s calendar app over CalDAV. “Every other thursday until christmas” is FREQ=WEEKLY;INTERVAL=2;BYDAY=TH;UNTIL=20261225.
Some things people say have no iCalendar equivalent, and for those the rule gets an X- part, the form iCalendar gives extensions. “The last working day of the month” adds X-SKIP-DAYS-OFF=TRUE, and a calendar app that doesn’t know it shows the last weekday instead. “The weekend before each Mother’s Day” can’t be a plain yearly rule, because Mother’s Day falls on a different date each year, so the rule names the day and the offset from it:
FREQ=YEARLY;X-ON=Mother’s Day;X-OFFSET=-WEEKEND A plain offset is an iCalendar duration. “3 days before” is -P3D, which a calendar alarm can use as it is. iCalendar has no way to say “the day before at 9am”, so SayWhen adds the time after the duration: -P1D@09:00. A reminder stores the offset, not a date, so it follows its event when the event moves.
Milieu’s own date fields
The demos above call SayWhen directly. Here are four of Milieu’s own date fields, as the app has them: a to-do’s When, Deadline and Repeat, and a contact’s Birthday, which uses the same date picker. The New event card isn’t here, as it needs a calendar to sit over.
Using SayWhen
SayWhen is MIT licensed. The Rust crate is say-when on crates.io:
cargo add say-when The same library compiled to WebAssembly, with a typed TypeScript API and no dependencies, is @milieu/say-when on npm. It runs in browsers, Node, Deno, Bun and workers. It’s about 157 KB with Brotli compression and 193 KB with gzip, almost all of it the WebAssembly.
npm install @milieu/say-when import { Reader } from "@milieu/say-when";
const reader = new Reader({ locale: "uk" });
// Fri 27 Nov, then Fri 20 Nov
reader.days("next fri", "2026-11-16"); The code is on GitHub.
If you find a phrase SayWhen doesn’t read, or reads wrongly, please open an issue with the phrase, the day you typed it and what you meant. Each one becomes a test case, so it stays fixed.