import { getPreferenceValues } from "@raycast/api"; export const DAY = 86_400_000; export const startOfDay = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate()); export const sameDay = (a: Date, b: Date) => a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate(); export const addDays = (d: Date, n: number) => new Date(d.getFullYear(), d.getMonth(), d.getDate() + n); export const weekStart = () => Number(getPreferenceValues<{ weekStart?: string }>().weekStart ?? "1"); /** The weeks shown for a month: from the week holding the 1st to the week holding the last day. */ export function monthGrid(month: Date) { const first = new Date(month.getFullYear(), month.getMonth(), 1); const last = new Date(month.getFullYear(), month.getMonth() + 1, 0); const start = addDays(first, -((first.getDay() - weekStart() + 7) % 7)); const days: Date[] = []; for (let d = start; d <= last || days.length % 7 !== 0; d = addDays(d, 1)) days.push(d); return days; } export const time = (d: Date) => d.toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit" }); export function when(e: { start: Date; end: Date; allDay: boolean }) { return e.allDay ? "All day" : time(e.start) + " – " + time(e.end); }