Files
zarinpal-analytics-frontend/src/lib/growth-coach-schedule.ts

35 lines
990 B
TypeScript

const tehranParts = new Intl.DateTimeFormat('en-CA', {
timeZone: 'Asia/Tehran',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hourCycle: 'h23',
});
function parts(value: Date) {
return Object.fromEntries(
tehranParts
.formatToParts(value)
.filter((part) => part.type !== 'literal')
.map((part) => [part.type, Number(part.value)]),
);
}
function tehranOffset(value: Date) {
const local = parts(value);
return (
Date.UTC(local.year, local.month - 1, local.day, local.hour, local.minute, local.second) - value.getTime()
);
}
export function millisecondsUntilTehranMidnight(now = new Date()): number {
const local = parts(now);
const targetAsUTC = Date.UTC(local.year, local.month - 1, local.day + 1);
let target = new Date(targetAsUTC - tehranOffset(now));
target = new Date(targetAsUTC - tehranOffset(target));
return Math.max(0, target.getTime() - now.getTime());
}