business-time-calculator
Version:
Simple tool to calculate the business time between two dates
84 lines (83 loc) • 3.27 kB
TypeScript
export declare const DayOfWeek: {
readonly MONDAY: "MONDAY";
readonly TUESDAY: "TUESDAY";
readonly WEDNESDAY: "WEDNESDAY";
readonly THURSDAY: "THURSDAY";
readonly FRIDAY: "FRIDAY";
readonly SATURDAY: "SATURDAY";
readonly SUNDAY: "SUNDAY";
};
export type DAY_OF_WEEK = keyof typeof DayOfWeek;
export type Holiday = `${3 | 2 | 1 | 0}${number}/${1 | 0}${number}`;
export declare class BusinessTime {
private businessTimezone;
private businessDays;
private holidays;
private startHour;
private endHour;
static computeWorkingHours: (startHour: number, endHour: number) => number;
constructor({ businessTimezone, businessDays, businessHours, holidays, }: {
businessTimezone: string;
businessDays: DAY_OF_WEEK[];
businessHours: number[];
holidays: Holiday[];
});
private setToStartOfDay;
private setToEndOfDay;
computeWorkingHours: () => number;
isBusinessDay(datetime: Temporal.ZonedDateTime): boolean;
computeBusinessDaysInInterval({ start, end, }: {
start: Temporal.ZonedDateTime;
end: Temporal.ZonedDateTime;
}): number;
computeBusinessHoursInInterval({ start, end, }: {
start: Temporal.ZonedDateTime;
end: Temporal.ZonedDateTime;
}): number;
computeBusinessMinutesInInterval({ start, end, }: {
start: Temporal.ZonedDateTime;
end: Temporal.ZonedDateTime;
}): number;
computeBusinessSecondsInInterval({ start, end, }: {
start: Temporal.ZonedDateTime;
end: Temporal.ZonedDateTime;
}): number;
computeBusinessTimeInInterval({ start, end, unit, }: {
start: Temporal.ZonedDateTime;
end: Temporal.ZonedDateTime;
unit: 'hours' | 'minutes' | 'seconds';
}): number;
/**
* Move the date in a business time (moveBehind = false)
* e.g. 06:00 => 10:00 of the current day
* e.g. 22:00 => 10:00 of the next day
*
* Move the date in a business time (moveBehind = true)
* e.g. 06:00 => 19:00 of the previous day
* e.g. 22:00 => 19:00 of the current day
*
* Warning ⚠️ _moveDateInBusinessTime doesn't retain the original timezone of the datetime in input, but it returns a datetime with the same timezone used to compute business times.
* It follows that behaviour because this method should be private and used only as helper. It is public only for testing purpose.
*/
_moveDateInBusinessTime({ datetime, moveBehind, }: {
datetime: Temporal.ZonedDateTime;
moveBehind?: boolean;
}): Temporal.ZonedDateTime;
addBusinessHoursToDate({ datetime, hours, }: {
datetime: Temporal.ZonedDateTime;
hours: number;
}): Temporal.ZonedDateTime;
addBusinessSecondsToDate({ datetime, seconds, }: {
datetime: Temporal.ZonedDateTime;
seconds: number;
}): Temporal.ZonedDateTime;
removeBusinessHoursFromDate({ datetime, hours, }: {
datetime: Temporal.ZonedDateTime;
hours: number;
}): Temporal.ZonedDateTime;
removeBusinessSecondsFromDate({ datetime, seconds, }: {
datetime: Temporal.ZonedDateTime;
seconds: number;
}): Temporal.ZonedDateTime;
hoursToDays(hours: number): number;
}