/** * Calendar dates and money for billing. Date-only columns (@db.Date) come back as JavaScript * Dates at UTC midnight, so dates here are UTC-midnight Dates and 'YYYY-MM-DD' strings. "Today" * is today in South Africa. */ const TZ = 'Africa/Johannesburg'; const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; /** 'YYYY-MM-DD' of a date-only value (a UTC-midnight Date or a string). */ function ymd(d) { if (!d) return null; if (typeof d === 'string') return d.slice(0, 10); return d.toISOString().slice(0, 10); } /** A date-only value as a UTC-midnight Date. Throws on nonsense. */ function day(v) { const s = ymd(v instanceof Date ? v : String(v || '')); if (!s || !/^\d{4}-\d{2}-\d{2}$/.test(s)) throw Object.assign(new Error('Not a valid date.'), { status: 400 }); const d = new Date(`${s}T00:00:00Z`); if (Number.isNaN(d.getTime()) || ymd(d) !== s) throw Object.assign(new Error('Not a valid date.'), { status: 400 }); return d; } /** Today's date in South Africa, as a UTC-midnight Date. */ function today(now = new Date()) { return day(new Intl.DateTimeFormat('en-CA', { timeZone: TZ, year: 'numeric', month: '2-digit', day: '2-digit' }).format(now)); } /** The hour (0–23) in South Africa. */ function saHour(now = new Date()) { return Number(new Intl.DateTimeFormat('en-GB', { timeZone: TZ, hour: '2-digit', hourCycle: 'h23' }).format(now)); } const addDays = (d, n) => new Date(day(d).getTime() + n * 864e5); /** * `n` months after `d`, on `anchorDay` (the billing day) or the month's last day when the month * is shorter: 31 Jan + 1 month on day 31 → 28/29 Feb; then + 1 → 31 Mar. */ function addMonths(d, n, anchorDay = day(d).getUTCDate()) { const base = day(d); const y = base.getUTCFullYear(); const m = base.getUTCMonth() + n; const last = new Date(Date.UTC(y, m + 1, 0)).getUTCDate(); return new Date(Date.UTC(y, m, Math.min(anchorDay, last))); } /** "1 December 2026". */ function longDate(d) { if (!d) return ''; const x = day(d); return `${x.getUTCDate()} ${MONTHS[x.getUTCMonth()]} ${x.getUTCFullYear()}`; } /** "15 Dec 2026". */ function shortDate(d) { if (!d) return ''; const x = day(d); return `${x.getUTCDate()} ${MONTHS[x.getUTCMonth()].slice(0, 3)} ${x.getUTCFullYear()}`; } /** "December 2026" (a monthly period), or "1 December 2026 – 30 November 2027". */ function periodLabel(start, end) { const s = day(start); const e = day(end); const wholeMonth = s.getUTCDate() === 1 && ymd(addDays(e, 1)) === ymd(addMonths(s, 1, 1)); return wholeMonth ? `${MONTHS[s.getUTCMonth()]} ${s.getUTCFullYear()}` : `${longDate(s)} – ${longDate(e)}`; } /** R1 350.00 (cents in, South African style with a space between thousands). */ function money(cents) { const neg = cents < 0; const [r, c] = (Math.abs(Math.round(cents)) / 100).toFixed(2).split('.'); return `${neg ? '-' : ''}R${r.replace(/\B(?=(\d{3})+(?!\d))/g, ' ')}.${c}`; } module.exports = { TZ, ymd, day, today, saHour, addDays, addMonths, longDate, shortDate, periodLabel, money };