oficina / mapa Public

⌘K typed dates: today, weekdays, next <day>, 12 nov and ISO, from the viewer's today

Level-up §4.6. Resolved on the server from today in the viewer's zone;
English only; unit-tested with a fixed today.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 5083e5fd8edbfaeca76588aeacaf3688db4b32db parent 9414f16
2 files changed, +176 −0
  • internal/mapa/typeddate.go +121 −0
  • internal/mapa/typeddate_test.go +55 −0
diff --git a/internal/mapa/typeddate.go b/internal/mapa/typeddate.go
new file mode 100644
index 0000000..612cff0
--- /dev/null
+++ b/internal/mapa/typeddate.go
@@ -0,0 +1,121 @@
+package mapa
+
+import (
+ "strconv"
+ "strings"
+ "time"
+
+ "amadan.net/oficina/mapa/internal/mapa/markup"
+)
+
+// Typed dates (level-up §4.6): ⌘K reads "today", "yesterday",
+// "tomorrow", a weekday ("friday" is the most recent Friday, today
+// included; "next friday" the coming one), "12 nov" or "12 november"
+// (this year), and ISO dates as a day. They are resolved on the server,
+// counted from today in the viewer's own zone. English only.
+
+// DateHit is ⌘K's date row: the viewer's own note for a typed day.
+type DateHit struct {
+ Date string `json:"date"`
+ Label string `json:"label"`
+ Href string `json:"href"`
+}
+
+var weekdays = map[string]time.Weekday{
+ "sunday": time.Sunday, "monday": time.Monday, "tuesday": time.Tuesday,
+ "wednesday": time.Wednesday, "thursday": time.Thursday,
+ "friday": time.Friday, "saturday": time.Saturday,
+ "sun": time.Sunday, "mon": time.Monday, "tue": time.Tuesday,
+ "tues": time.Tuesday, "wed": time.Wednesday, "thu": time.Thursday,
+ "thur": time.Thursday, "thurs": time.Thursday, "fri": time.Friday,
+ "sat": time.Saturday,
+}
+
+var months = map[string]time.Month{
+ "jan": time.January, "feb": time.February, "mar": time.March,
+ "apr": time.April, "may": time.May, "jun": time.June,
+ "jul": time.July, "aug": time.August, "sep": time.September,
+ "sept": time.September, "oct": time.October, "nov": time.November,
+ "dec": time.December, "january": time.January,
+ "february": time.February, "march": time.March, "april": time.April,
+ "june": time.June, "july": time.July, "august": time.August,
+ "september": time.September, "october": time.October,
+ "november": time.November, "december": time.December,
+}
+
+// typedDate is the day query names, as an ISO date, counted from today
+// (an ISO date in the viewer's zone); "" when it names none.
+func typedDate(query, today string) string {
+ base, err := time.Parse(time.DateOnly, today)
+ if err != nil {
+ return ""
+ }
+ words := strings.Fields(strings.ToLower(strings.TrimSpace(query)))
+ switch len(words) {
+ case 1:
+ return oneWordDate(words[0], base)
+ case 2:
+ return twoWordDate(words[0], words[1], base)
+ }
+ return ""
+}
+
+func oneWordDate(text string, base time.Time) string {
+ switch text {
+ case "today":
+ return iso(base)
+ case "yesterday":
+ return iso(base.AddDate(0, 0, -1))
+ case "tomorrow":
+ return iso(base.AddDate(0, 0, 1))
+ }
+ if day, ok := weekdays[text]; ok {
+ back := (int(base.Weekday()) - int(day) + 7) % 7
+ return iso(base.AddDate(0, 0, -back))
+ }
+ if markup.IsDate(text) {
+ return text
+ }
+ return ""
+}
+
+// twoWordDate is "next friday", "12 nov" or "nov 12".
+func twoWordDate(first, second string, base time.Time) string {
+ if day, ok := weekdays[second]; ok && first == "next" {
+ ahead := (int(day) - int(base.Weekday()) + 7) % 7
+ if ahead == 0 {
+ ahead = 7
+ }
+ return iso(base.AddDate(0, 0, ahead))
+ }
+ if _, ok := months[first]; ok {
+ first, second = second, first
+ }
+ month, ok := months[second]
+ number, err := strconv.Atoi(first)
+ if !ok || err != nil || number < 1 || number > 31 {
+ return ""
+ }
+ day := time.Date(base.Year(), month, number, 0, 0, 0, 0, time.UTC)
+ if day.Month() != month {
+ return ""
+ }
+ return iso(day)
+}
+
+func iso(day time.Time) string { return day.Format(time.DateOnly) }
+
+// dateRow is the row for date, labelled "Friday 13 November", with the
+// year only when it is not this one.
+func dateRow(date, today string) *DateHit {
+ day, err := time.Parse(time.DateOnly, date)
+ if err != nil {
+ return nil
+ }
+ layout := "Monday 2 January"
+ if date[:4] != today[:4] {
+ layout += " 2006"
+ }
+ return &DateHit{Date: date, Label: day.Format(layout),
+ Href: "/d/" + date}
+}
diff --git a/internal/mapa/typeddate_test.go b/internal/mapa/typeddate_test.go
new file mode 100644
index 0000000..af4f6e2
--- /dev/null
+++ b/internal/mapa/typeddate_test.go
@@ -0,0 +1,55 @@
+package mapa
+
+import "testing"
+
+// The parser counts from a fixed today: Saturday 14 November 2026.
+const fixedToday = "2026-11-14"
+
+func TestTypedDatesResolveFromToday(t *testing.T) {
+ for query, want := range map[string]string{
+ "today": "2026-11-14", " Today ": "2026-11-14",
+ "yesterday": "2026-11-13", "tomorrow": "2026-11-15",
+ // The most recent Friday; today included.
+ "friday": "2026-11-13", "saturday": "2026-11-14",
+ "sunday": "2026-11-08", "fri": "2026-11-13",
+ // The coming one: never today.
+ "next friday": "2026-11-20", "next saturday": "2026-11-21",
+ "next sunday": "2026-11-15", "next Mon": "2026-11-16",
+ // This year, whether past or still to come.
+ "12 nov": "2026-11-12", "12 november": "2026-11-12",
+ "3 March": "2026-03-03", "dec 25": "2026-12-25",
+ "29 feb": "", "31 apr": "", "0 nov": "", "12 novem": "",
+ "2025-02-28": "2025-02-28", "2026-02-30": "", "2026-11": "",
+ "next": "", "next week": "", "friday plans": "", "acme": "",
+ "": "", "12 nov 2026": "", "last friday": "",
+ } {
+ if got := typedDate(query, fixedToday); got != want {
+ t.Errorf("%q is %q, want %q", query, got, want)
+ }
+ }
+}
+
+func TestTheDateRowNamesTheDay(t *testing.T) {
+ row := dateRow("2026-11-13", fixedToday)
+ if row.Label != "Friday 13 November" || row.Href != "/d/2026-11-13" {
+ t.Fatalf("this year: %+v", row)
+ }
+ if got := dateRow("2025-02-28", fixedToday).Label; got !=
+ "Friday 28 February 2025" {
+ t.Fatalf("another year: %q", got)
+ }
+ if dateRow("", fixedToday) != nil {
+ t.Fatal("no date, but a row")
+ }
+}
+
+// A leap year's 29 February is a day; the parser reads the year from
+// today, not from the clock.
+func TestTypedDatesUseTodaysYear(t *testing.T) {
+ if got := typedDate("29 feb", "2028-01-05"); got != "2028-02-29" {
+ t.Fatalf("29 feb in 2028 is %q", got)
+ }
+ if got := typedDate("friday", "not a date"); got != "" {
+ t.Fatalf("no today, but %q", got)
+ }
+}