60 lines
1.7 KiB
TypeScript
Executable File
60 lines
1.7 KiB
TypeScript
Executable File
import dayjs from "dayjs";
|
|
import timezone from "dayjs/plugin/timezone";
|
|
import utc from "dayjs/plugin/utc";
|
|
import duration from "dayjs/plugin/duration";
|
|
import { DEFAULT_TZ } from "./constants";
|
|
|
|
dayjs.extend(timezone);
|
|
dayjs.extend(utc);
|
|
dayjs.extend(duration);
|
|
|
|
export function calculateTimeRemaining(
|
|
closingTime: string,
|
|
returnClosed: boolean,
|
|
timezone: string = DEFAULT_TZ
|
|
): string {
|
|
const currentDate = dayjs().tz(timezone).format("YYYY-MM-DD");
|
|
const adjustedClosingTime = `${currentDate} ${closingTime.split(" ")[1]}`;
|
|
const closingDateTime = dayjs.tz(
|
|
adjustedClosingTime,
|
|
"YYYY-MM-DD HH:mm:ss",
|
|
timezone
|
|
);
|
|
const currentDateTime = dayjs().tz(timezone);
|
|
|
|
const isValid = closingDateTime.isValid();
|
|
const isBefore = closingDateTime.isBefore(currentDateTime);
|
|
const isSame = closingDateTime.isSame(currentDateTime);
|
|
|
|
if (!isValid || isBefore || isSame) {
|
|
return returnClosed ? "closed" : "00:00:00";
|
|
}
|
|
|
|
const diffDuration = dayjs.duration(closingDateTime.diff(currentDateTime));
|
|
const formattedTime = dayjs
|
|
.utc(diffDuration.asMilliseconds())
|
|
.format("HH:mm:ss");
|
|
|
|
return formattedTime;
|
|
}
|
|
|
|
export function isDrawClosed(
|
|
closingTime: string,
|
|
timezone: string = DEFAULT_TZ
|
|
): boolean {
|
|
const out = calculateTimeRemaining(closingTime, true, timezone);
|
|
return out === "closed";
|
|
}
|
|
|
|
export function parseToDateString(date: Date): string {
|
|
return dayjs(date).format("YYYY-MM-DD");
|
|
}
|
|
|
|
// export const getCurrentDateInTimeZone(timeZone: string): string {
|
|
// const now = dayjs().utc();
|
|
// const localDateTime = now.tz(timeZone);
|
|
// const formattedDate = localDateTime.format('YYYY-MM-DD');
|
|
//
|
|
// return formattedDate;
|
|
// }
|