.pragma library // Copyright (C) 2026 Danilo M. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as // published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // Pure date logic for the calendar panel. Days are "YYYY-MM-DD" strings and // times "HH:MM", the shapes calctl speaks; day arithmetic goes through UTC so // a DST change never shifts a day. Tested by test_cal.js under node. function pad(n) { return (n < 10 ? "0" : "") + n; } function fromUtc(t) { return t.getUTCFullYear() + "-" + pad(t.getUTCMonth() + 1) + "-" + pad(t.getUTCDate()); } function utc(day) { return Date.UTC(+day.slice(0, 4), +day.slice(5, 7) - 1, +day.slice(8, 10)); } function addDays(day, n) { return fromUtc(new Date(utc(day) + n * 86400000)); } function daysBetween(a, b) { return Math.round((utc(b) - utc(a)) / 86400000); } // The local calendar day of a JS Date. function today(now) { return now.getFullYear() + "-" + pad(now.getMonth() + 1) + "-" + pad(now.getDate()); } // 42 cells, six weeks from Monday (khal's firstweekday = 0). function monthCells(y, m0) { const first = new Date(Date.UTC(y, m0, 1)); const month = fromUtc(first).slice(0, 7); const lead = (first.getUTCDay() + 6) % 7; const start = addDays(fromUtc(first), -lead); const out = []; for (let i = 0; i < 42; i++) { const iso = addDays(start, i); out.push({ iso: iso, day: +iso.slice(8, 10), inMonth: iso.slice(0, 7) === month }); } return out; } function shiftMonth(y, m0, n) { const t = new Date(Date.UTC(y, m0 + n, 1)); return { y: t.getUTCFullYear(), m0: t.getUTCMonth() }; } // The last day an event covers. calctl gives all-day ends inclusive; a // timed event ending exactly at midnight does not reach the day it ends on. function lastDay(ev) { const endDay = ev.end.slice(0, 10); if (!ev.allDay && ev.end.slice(11, 16) === "00:00" && ev.end > ev.start) return addDays(endDay, -1); return endDay; } function onDay(ev, day) { return ev.start.slice(0, 10) <= day && lastDay(ev) >= day; } // The day's events, all-day first, then by start. function dayEvents(events, day) { return events.filter(e => onDay(e, day)).sort((a, b) => (b.allDay - a.allDay) || (a.start < b.start ? -1 : a.start > b.start ? 1 : 0)); } // The calendars with something on the day, in calendar order. function dots(events, day, calendars) { return calendars.filter(c => events.some(e => e.calendar === c.name && onDay(e, day))); } // The island's clock settings (Prefs.island): h12 for a 12-hour clock, and // dateFormat, the clock pill's date as one of DATE_FORMATS (Qt format // strings). The calendar's longer dates follow its order, day or month // first. clockTime formats calctl's "HH:MM", compact ("9:30p") where width // is short. var DATE_GROUPS = [ { title: "Day first", formats: ["ddd dd/MM", "dd/MM", "dd/MM/yy", "dd/MM/yyyy", "ddd dd/MM/yyyy", "dd.MM.yyyy", "dd-MM-yyyy", "d MMM", "ddd d MMM", "d MMM yyyy"] }, { title: "Month first", formats: ["ddd MM/dd", "MM/dd", "MM/dd/yy", "MM/dd/yyyy", "ddd MM/dd/yyyy", "MMM d", "ddd MMM d", "MMM d, yyyy"] }, { title: "Year first", formats: ["yyyy-MM-dd", "ddd yyyy-MM-dd", "yyyy/MM/dd", "ddd MM-dd"] }]; var DATE_FORMATS = DATE_GROUPS.reduce((a, g) => a.concat(g.formats), []); function timeFmt(h12) { return h12 ? "h:mm AP" : "HH:mm"; } function shortDateFmt(fmt) { return DATE_FORMATS.includes(fmt) ? fmt : DATE_FORMATS[0]; } // Day first unless the month comes before the day, the weekday name aside. function dayFirst(fmt) { const f = shortDateFmt(fmt).replace(/ddd+/, ""); return f.indexOf("d") < f.indexOf("M"); } function dayFmt(fmt, long) { if (dayFirst(fmt)) return long ? "dddd d MMMM" : "ddd d MMM"; return long ? "dddd, MMMM d" : "ddd MMM d"; } function clockTime(hhmm, h12) { if (!h12 || !validTime(hhmm)) return hhmm; const h = +hhmm.slice(0, 2); return (h % 12 || 12) + hhmm.slice(2) + (h < 12 ? "a" : "p"); } function hourLabel(h, h12) { return h12 ? (h % 12 || 12) + (h < 12 ? "a" : "p") : pad(h); } function timeLabel(ev, day, h12) { if (ev.allDay) return "all day"; const s = ev.start.slice(0, 10) === day ? clockTime(ev.start.slice(11, 16), h12) : "…"; const e = ev.end.slice(0, 10) === day ? clockTime(ev.end.slice(11, 16), h12) : "…"; return s + "–" + e; } function validTime(s) { return /^([01]\d|2[0-3]):[0-5]\d$/.test(s); } // Loose typing as HH:MM: "9.30", "9:30", "930", "9", and with am or pm // ("9.30pm", "12am") for the 12-hour clock, mean what they look like. // Anything else comes back unchanged, for validTime to reject. function normTime(s) { const m = /^\s*(\d{1,2})(?:[:.,h ]?(\d{2}))?\s*(?:([ap])\.?m?\.?)?\s*$/i.exec(s); if (!m) return s; let h = +m[1]; if (m[3]) { if (h < 1 || h > 12) return s; h = h % 12 + (m[3].toLowerCase() === "p" ? 12 : 0); } const t = pad(h) + ":" + (m[2] || "00"); return validTime(t) ? t : s; } // Step a time by minutes, wrapping within the day; an invalid time is left // for the user to fix rather than guessed at. function stepTime(s, minutes) { s = normTime(s); if (!validTime(s)) return s; const m = ((+s.slice(0, 2) * 60 + +s.slice(3, 5) + minutes) % 1440 + 1440) % 1440; return pad(Math.floor(m / 60)) + ":" + pad(m % 60); } // A new event on `day`: the next whole hour, `minutes` long (the island's // default event length), alarm 1h (the most common alarm in the live // calendars). Capped at 22:00 so a short event stays on the same day. function newDraft(day, now, calendar, minutes) { const h = Math.min(22, now.getHours() + 1); return withStart({ uid: "", source: "", calendar: calendar, title: "", allDay: false, startDay: day, endDay: day, startTime: pad(h) + ":00", endTime: pad(h) + ":00", repeat: "none", until: "", alarm: "1h", location: "", notes: "", override: false }, pad(h) + ":00", minutes); } // The draft with a new start time, as typed, the end following it `minutes` later, // into the next day if need be. A multi-day event keeps its end: snapping it // to the start's day would throw the other days away. function withStart(draft, time, minutes) { const d = Object.assign({}, draft, { startTime: time }); const n = normTime(time); if (!validTime(n) || d.startDay !== d.endDay) return d; const t = +n.slice(0, 2) * 60 + +n.slice(3, 5) + (minutes > 0 ? minutes : 60); d.endDay = addDays(d.startDay, Math.floor(t / 1440)); d.endTime = pad(Math.floor(t % 1440 / 60)) + ":" + pad(t % 60); return d; } // calctl's event JSON as an editor draft. `source` is the calendar the event // lives in now; `calendar` is where the form wants it. function fromEvent(ev) { return { uid: ev.uid, source: ev.calendar, calendar: ev.calendar, title: ev.title, allDay: ev.allDay, startDay: ev.start.slice(0, 10), endDay: ev.end.slice(0, 10), startTime: ev.allDay ? "09:00" : ev.start.slice(11, 16), endTime: ev.allDay ? "10:00" : ev.end.slice(11, 16), repeat: ev.repeat, until: ev.until, alarm: ev.alarm, location: ev.location, notes: ev.notes, override: ev.override }; } // A click on the grid while editing. Plain: move the event, keeping its // length in days. Shift: set the end, or the start when before it. function pick(draft, day, shift) { const d = Object.assign({}, draft); if (!shift) { const len = daysBetween(d.startDay, d.endDay); d.startDay = day; d.endDay = addDays(day, len); } else if (day >= d.startDay) { d.endDay = day; } else { d.startDay = day; } return d; } // The draft as calctl's event JSON, or the first thing wrong with it. function toPayload(d) { d = Object.assign({}, d, { startTime: normTime(d.startTime), endTime: normTime(d.endTime) }); if (!d.title.trim()) return { error: "Title is empty" }; if (!d.allDay && (!validTime(d.startTime) || !validTime(d.endTime))) return { error: "Times are HH:MM" }; const start = d.allDay ? d.startDay : d.startDay + "T" + d.startTime; const end = d.allDay ? d.endDay : d.endDay + "T" + d.endTime; if (end < start) return { error: "End is before start" }; const repeats = d.repeat !== "none" && d.repeat !== "custom"; if (repeats && d.until && d.until < d.startDay) return { error: "Until is before the start" }; return { payload: { calendar: d.calendar, title: d.title.trim(), allDay: d.allDay, start: start, end: end, repeat: d.repeat, until: repeats ? d.until : "", alarm: d.alarm, location: d.location, notes: d.notes } }; }