//ensures the week starts from sunday export function weekStartSunday(date_local: Date): Date { const start = new Date(Date.UTC(date_local.getFullYear(), date_local.getMonth(), date_local.getDate())); const dow = start.getDay(); // 0 = dimanche start.setDate(start.getDate() - dow); start.setHours(0, 0, 0, 0); return start; } //converts string to HHmm format export const toStringFromHHmm = (date: Date): string => { const hh = date.getUTCHours().toString().padStart(2, '0'); const mm = date.getUTCMinutes().toString().padStart(2, '0'); return `${hh}:${mm}`; } //converts string to Date format export const toStringFromDate = (date: Date) => date.toISOString().slice(0,10); //converts HHmm format to string export const toHHmmFromString = (hhmm: string): Date => { const [hh, mm] = hhmm.split(':').map(Number); const date = new Date('1970-01-01T00:00:00.000Z'); date.setUTCHours(hh, mm, 0, 0); return new Date(date); } //converts string to HHmm format export const toHHmmFromDate = (input: Date | string): string => { const date = new Date(input); const hh = String(date.getUTCHours()).padStart(2, '0'); const mm = String(date.getUTCMinutes()).padStart(2, '0'); return `${hh}:${mm}`; } //converts Date format to string export const toDateFromString = (ymd: string | Date): Date => { return new Date(`${ymd}T00:00:00:000Z`); } export const toUTCDateFromString = (iso: string | Date) => { const d = typeof iso === 'string' ? new Date(iso + 'T00:00:00.000Z') : iso; return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate())); }; export const sevenDaysFrom = (date: Date | string): Date[] => { return Array.from({length: 7 }, (_,i) => { const d = new Date(date); d.setUTCDate(d.getUTCDate() + i ); return d; }); } export function payYearOfDate(date: string | Date, anchorISO = ANCHOR_ISO): number { const ANCHOR = toUTCDateFromString(anchorISO); const d = toUTCDateFromString(date); const days = Math.floor((+d - +ANCHOR) / MS_PER_DAY); const cycles = Math.floor(days / (PERIODS_PER_YEAR * PERIOD_DAYS)); return ANCHOR.getUTCFullYear() + 1 + cycles; } //compute labels for periods export function computePeriod(pay_year: number, period_no: number, anchorISO = ANCHOR_ISO) { const ANCHOR = toUTCDateFromString(anchorISO); const cycles = pay_year - (ANCHOR.getUTCFullYear() + 1); const offsetPeriods = cycles * PERIODS_PER_YEAR + (period_no - 1); const start = new Date(+ANCHOR + offsetPeriods * PERIOD_DAYS * MS_PER_DAY); const end = new Date(+start + (PERIOD_DAYS - 1) * MS_PER_DAY); const pay = new Date(end.getTime() + 6 * MS_PER_DAY); return { period_no: period_no, pay_year: pay_year, payday: toStringFromDate(pay), period_start: toStringFromDate(start), period_end: toStringFromDate(end), label: `${toStringFromDate(start)}.${toStringFromDate(end)}`, start, end, }; } //list of all 26 periods for a full year export function listPayYear(pay_year: number, anchorISO = ANCHOR_ISO) { return Array.from({ length: PERIODS_PER_YEAR }, (_, i) => computePeriod(pay_year, i + 1, anchorISO)); }