Merge branch 'main' of git.targo.ca:Targo/targo_backend
This commit is contained in:
commit
3741bf3b26
|
|
@ -3,32 +3,32 @@ import { ANCHOR_ISO, MS_PER_DAY, PERIODS_PER_YEAR, PERIOD_DAYS } from "src/time-
|
||||||
|
|
||||||
//ensures the week starts from sunday
|
//ensures the week starts from sunday
|
||||||
export function weekStartSunday(date_local: Date): Date {
|
export function weekStartSunday(date_local: Date): Date {
|
||||||
const start = new Date(Date.UTC(date_local.getFullYear(), date_local.getMonth(), date_local.getDate()));
|
const start = new Date(Date.UTC(date_local.getFullYear(), date_local.getMonth(), date_local.getDate()));
|
||||||
const dow = start.getDay(); // 0 = dimanche
|
const dow = start.getDay(); // 0 = dimanche
|
||||||
start.setDate(start.getDate() - dow);
|
start.setDate(start.getDate() - dow);
|
||||||
start.setHours(0, 0, 0, 0);
|
start.setHours(0, 0, 0, 0);
|
||||||
return start;
|
return start;
|
||||||
}
|
}
|
||||||
|
|
||||||
//converts string to HHmm format
|
//converts string to HHmm format
|
||||||
export const toStringFromHHmm = (date: Date): string => {
|
export const toStringFromHHmm = (date: Date): string => {
|
||||||
const hh = date.getUTCHours().toString().padStart(2, '0');
|
const hh = date.getUTCHours().toString().padStart(2, '0');
|
||||||
const mm = date.getUTCMinutes().toString().padStart(2, '0');
|
const mm = date.getUTCMinutes().toString().padStart(2, '0');
|
||||||
return `${hh}:${mm}`;
|
return `${hh}:${mm}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
//converts string to Date format
|
//converts string to Date format
|
||||||
export const toStringFromDate = (date: Date) =>
|
export const toStringFromDate = (date: Date) =>
|
||||||
date.toISOString().slice(0,10);
|
date.toISOString().slice(0, 10);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//converts HHmm format to string
|
//converts HHmm format to string
|
||||||
export const toHHmmFromString = (hhmm: string): Date => {
|
export const toHHmmFromString = (hhmm: string): Date => {
|
||||||
const [hh, mm] = hhmm.split(':').map(Number);
|
const [hh, mm] = hhmm.split(':').map(Number);
|
||||||
const date = new Date('1970-01-01T00:00:00.000Z');
|
const date = new Date('1970-01-01T00:00:00.000Z');
|
||||||
date.setUTCHours(hh, mm, 0, 0);
|
date.setUTCHours(hh, mm, 0, 0);
|
||||||
return new Date(date);
|
return new Date(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
//converts string to HHmm format
|
//converts string to HHmm format
|
||||||
|
|
@ -40,52 +40,52 @@ export const toHHmmFromDate = (input: Date | string): string => {
|
||||||
}
|
}
|
||||||
|
|
||||||
//converts Date format to string
|
//converts Date format to string
|
||||||
export const toDateFromString = (ymd: string | Date): Date => {
|
export const toDateFromString = (ymd: string | Date): Date => {
|
||||||
return new Date(`${ymd}`);
|
return new Date(ymd);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const toUTCDateFromString = (iso: string | Date) => {
|
export const toUTCDateFromString = (iso: string | Date) => {
|
||||||
const d = typeof iso === 'string' ? new Date(iso + 'T00:00:00.000Z') : iso;
|
const d = typeof iso === 'string' ? new Date(iso + 'T00:00:00.000Z') : iso;
|
||||||
return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()));
|
return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()));
|
||||||
};
|
};
|
||||||
|
|
||||||
export const sevenDaysFrom = (date: Date | string): Date[] => {
|
export const sevenDaysFrom = (date: Date | string): Date[] => {
|
||||||
return Array.from({length: 7 }, (_,i) => {
|
return Array.from({ length: 7 }, (_, i) => {
|
||||||
const d = new Date(date);
|
const d = new Date(date);
|
||||||
d.setUTCDate(d.getUTCDate() + i );
|
d.setUTCDate(d.getUTCDate() + i);
|
||||||
return d;
|
return d;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function payYearOfDate(date: string | Date, anchorISO = ANCHOR_ISO): number {
|
export function payYearOfDate(date: string | Date, anchorISO = ANCHOR_ISO): number {
|
||||||
const ANCHOR = toUTCDateFromString(anchorISO);
|
const ANCHOR = toUTCDateFromString(anchorISO);
|
||||||
const d = toUTCDateFromString(date);
|
const d = toUTCDateFromString(date);
|
||||||
const days = Math.floor((+d - +ANCHOR) / MS_PER_DAY);
|
const days = Math.floor((+d - +ANCHOR) / MS_PER_DAY);
|
||||||
const cycles = Math.floor(days / (PERIODS_PER_YEAR * PERIOD_DAYS));
|
const cycles = Math.floor(days / (PERIODS_PER_YEAR * PERIOD_DAYS));
|
||||||
return ANCHOR.getUTCFullYear() + 1 + cycles;
|
return ANCHOR.getUTCFullYear() + 1 + cycles;
|
||||||
}
|
}
|
||||||
//compute labels for periods
|
//compute labels for periods
|
||||||
export function computePeriod(pay_year: number, period_no: number, anchorISO = ANCHOR_ISO) {
|
export function computePeriod(pay_year: number, period_no: number, anchorISO = ANCHOR_ISO) {
|
||||||
const ANCHOR = toUTCDateFromString(anchorISO);
|
const ANCHOR = toUTCDateFromString(anchorISO);
|
||||||
const cycles = pay_year - (ANCHOR.getUTCFullYear() + 1);
|
const cycles = pay_year - (ANCHOR.getUTCFullYear() + 1);
|
||||||
const offsetPeriods = cycles * PERIODS_PER_YEAR + (period_no - 1);
|
const offsetPeriods = cycles * PERIODS_PER_YEAR + (period_no - 1);
|
||||||
const start = new Date(+ANCHOR + offsetPeriods * PERIOD_DAYS * MS_PER_DAY);
|
const start = new Date(+ANCHOR + offsetPeriods * PERIOD_DAYS * MS_PER_DAY);
|
||||||
const end = new Date(+start + (PERIOD_DAYS - 1) * MS_PER_DAY);
|
const end = new Date(+start + (PERIOD_DAYS - 1) * MS_PER_DAY);
|
||||||
const pay = new Date(end.getTime() + 6 * MS_PER_DAY);
|
const pay = new Date(end.getTime() + 6 * MS_PER_DAY);
|
||||||
return {
|
return {
|
||||||
period_no: period_no,
|
period_no: period_no,
|
||||||
pay_year: pay_year,
|
pay_year: pay_year,
|
||||||
payday: toStringFromDate(pay),
|
payday: toStringFromDate(pay),
|
||||||
period_start: toStringFromDate(start),
|
period_start: toStringFromDate(start),
|
||||||
period_end: toStringFromDate(end),
|
period_end: toStringFromDate(end),
|
||||||
label: `${toStringFromDate(start)}.${toStringFromDate(end)}`,
|
label: `${toStringFromDate(start)}.${toStringFromDate(end)}`,
|
||||||
start, end,
|
start, end,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
//list of all 26 periods for a full year
|
//list of all 26 periods for a full year
|
||||||
export function listPayYear(pay_year: number, anchorISO = ANCHOR_ISO) {
|
export function listPayYear(pay_year: number, anchorISO = ANCHOR_ISO) {
|
||||||
return Array.from({ length: PERIODS_PER_YEAR }, (_, i) => computePeriod(pay_year, i + 1, anchorISO));
|
return Array.from({ length: PERIODS_PER_YEAR }, (_, i) => computePeriod(pay_year, i + 1, anchorISO));
|
||||||
}
|
}
|
||||||
|
|
||||||
export const overlaps = (a: { start: Date; end: Date }, b: { start: Date; end: Date }) =>
|
export const overlaps = (a: { start: Date; end: Date }, b: { start: Date; end: Date }) =>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user