diff --git a/app/apps/calendar/calendar.ts b/app/apps/calendar/calendar.ts index 5d112ab4..c2a5e976 100644 --- a/app/apps/calendar/calendar.ts +++ b/app/apps/calendar/calendar.ts @@ -9,6 +9,7 @@ import { hasCalendarPermission } from "../../native/calendar-permissions"; import { type Layer, type LayerContext } from "../../ui/layers"; import { lineStep } from "../../ui/metrics"; import type { Viewport } from "../../ui/viewport"; +import { listingDayMs, spanLabel, spansDays } from "./event-days"; /** How far a row's text sits inside its selection box; the title lines up with it. */ const ROW_TEXT_INSET = 10; @@ -143,14 +144,20 @@ export class CalendarLayer implements Layer { function buildEventRows(font: UiFont, events: CalendarEvent[]): EventRow[] { const rows: EventRow[] = []; let previousDayKey = ""; + const nowMs = Date.now(); for (const event of events) { - const dayKey = dayKeyOf(event.startMs); - const dayHeader = dayKey === previousDayKey ? null : dayHeaderLabel(event.startMs); + // An event still going since an earlier day is listed under today, not its first day. + const listedMs = listingDayMs(event, nowMs); + const dayKey = dayKeyOf(listedMs); + const dayHeader = dayKey === previousDayKey ? null : dayHeaderLabel(listedMs); previousDayKey = dayKey; const lines: string[] = []; const timeLabel = event.allDay ? "All day" : formatEventTime(event.startMs); lines.push(`${timeLabel} ${event.title || "(untitled)"}`); + if (spansDays(event)) { + lines.push(spanLabel(event)); + } if (event.location) { lines.push(event.location); } diff --git a/app/apps/calendar/event-days.ts b/app/apps/calendar/event-days.ts new file mode 100644 index 00000000..68c8ef66 --- /dev/null +++ b/app/apps/calendar/event-days.ts @@ -0,0 +1,43 @@ +/** + * Which day an event is listed under, and how a multi-day span reads. Pure (no + * native or UI imports) so it can be unit-tested. + */ + +const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; +const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; + +export type DayEvent = { startMs: number; endMs: number; allDay: boolean }; + +function midnight(timestampMs: number): number { + const date = new Date(timestampMs); + return new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime(); +} + +/** The last moment the event covers: all-day ends are exclusive (the midnight after its last day). */ +function lastMomentMs(event: DayEvent): number { + return event.allDay ? Math.max(event.startMs, event.endMs - 1) : Math.max(event.startMs, event.endMs); +} + +/** + * The day to list an event under: its start, or today for one that began on an + * earlier day and is still going. Without this, the third day of a three-day + * event shows under its first day and never under today. + */ +export function listingDayMs(event: DayEvent, nowMs: number): number { + return Math.max(midnight(event.startMs), Math.min(midnight(nowMs), midnight(lastMomentMs(event)))); +} + +/** Whether the event covers more than one calendar day. */ +export function spansDays(event: DayEvent): boolean { + return midnight(lastMomentMs(event)) > midnight(event.startMs); +} + +/** "Mon 16 – Wed 18 Nov", or with both months when they differ ("Mon 30 Nov – Tue 1 Dec"). */ +export function spanLabel(event: DayEvent): string { + const start = new Date(event.startMs); + const last = new Date(lastMomentMs(event)); + const day = (date: Date) => `${WEEKDAYS[date.getDay()]} ${date.getDate()}`; + return start.getMonth() === last.getMonth() && start.getFullYear() === last.getFullYear() + ? `${day(start)} – ${day(last)} ${MONTHS[last.getMonth()]}` + : `${day(start)} ${MONTHS[start.getMonth()]} – ${day(last)} ${MONTHS[last.getMonth()]}`; +} diff --git a/tests/calendar-event-days.test.cjs b/tests/calendar-event-days.test.cjs new file mode 100644 index 00000000..e0d216b2 --- /dev/null +++ b/tests/calendar-event-days.test.cjs @@ -0,0 +1,47 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); + +// Local-time day arithmetic: pin the zone so the expectations hold on any machine. +process.env.TZ = 'Europe/Brussels'; + +function load(file) { + const sandbox = { exports: {}, require: id => { throw new Error(`Unstubbed import: ${id}`); } }; + vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, + }).outputText, sandbox); + return sandbox.exports; +} + +const { listingDayMs, spansDays, spanLabel } = load('app/apps/calendar/event-days.ts'); +const local = (y, m, d, h = 0, min = 0) => new Date(y, m - 1, d, h, min).getTime(); +// Mon Nov 16 – Wed Nov 18, all day, as the provider stores it (end = midnight after the last day). +const threeDays = { startMs: local(2026, 11, 16), endMs: local(2026, 11, 19), allDay: true }; + +test('an all-day event still going is listed under today, not its first day', () => { + assert.equal(listingDayMs(threeDays, local(2026, 11, 18, 0, 24)), local(2026, 11, 18)); + assert.equal(listingDayMs(threeDays, local(2026, 11, 17, 9)), local(2026, 11, 17)); +}); + +test('events that have not started are listed under their start day', () => { + assert.equal(listingDayMs(threeDays, local(2026, 11, 14, 12)), local(2026, 11, 16)); + const meeting = { startMs: local(2026, 11, 19, 9), endMs: local(2026, 11, 19, 12), allDay: false }; + assert.equal(listingDayMs(meeting, local(2026, 11, 18, 0, 24)), local(2026, 11, 19)); +}); + +test('a timed event running past midnight is listed under today the next morning', () => { + const lateShift = { startMs: local(2026, 11, 17, 22), endMs: local(2026, 11, 18, 2), allDay: false }; + assert.equal(listingDayMs(lateShift, local(2026, 11, 18, 0, 30)), local(2026, 11, 18)); + assert.equal(spansDays(lateShift), true); +}); + +test('only events covering more than one day get a span, and it reads naturally', () => { + assert.equal(spansDays(threeDays), true); + assert.equal(spanLabel(threeDays), 'Mon 16 – Wed 18 Nov'); + assert.equal(spansDays({ startMs: local(2026, 11, 16), endMs: local(2026, 11, 17), allDay: true }), false); + assert.equal(spansDays({ startMs: local(2026, 11, 19, 9), endMs: local(2026, 11, 19, 12), allDay: false }), false); + assert.equal(spanLabel({ startMs: local(2026, 11, 30), endMs: local(2026, 12, 2), allDay: true }), 'Mon 30 Nov – Tue 1 Dec'); +}); diff --git a/tests/ios-calendar.test.cjs b/tests/ios-calendar.test.cjs index 484eb53f..73b4b1fd 100644 --- a/tests/ios-calendar.test.cjs +++ b/tests/ios-calendar.test.cjs @@ -163,7 +163,7 @@ function screenFixture() { 'util/numeric-util': { clamp: (n, min, max) => Math.max(min, Math.min(max, n)) }, }; const modules = depth => Object.fromEntries(Object.entries(common).map(([key, value]) => [depth + key, value])); - const calendar = load('app/apps/calendar/calendar.ts', modules('../../')); + const calendar = load('app/apps/calendar/calendar.ts', { ...modules('../../'), './event-days': load('app/apps/calendar/event-days.ts') }); const widget = load('app/apps/glanceboard/widgets/calendar-widget.ts', { ...modules('../../../'), '../../calendar/calendar': calendar, '../glance-font': { glanceFont: { small: () => font, medium: () => font } },