1
0
Fork 0
polaris-task-force/src/lib/intelligence/fridayTime.ts
Z8MB1E eed33de2e9 fix(missions): default new main ops to the upcoming Saturday
The startDateTime default in the Missions collection and the weekly
generate-mission bin both forced the date at least a week out by adding 7
days to the offset to the next Saturday. Generated midweek that landed on
the Saturday AFTER the coming one (a Tuesday generation went 11 days out
instead of 4), which also pushed the bot's roll-call (posted within 3 days
of start) days into the future and read as a missing roll-call.

Add nextSaturdayStart() to the unit-timezone date helpers: the nearest
upcoming Saturday at 20:00 America/New_York, rolling a week only once
today's 20:00 has passed. Use it for the collection default and the
generator, derive the generator codeName datestamp in unit time, and cover
the boundary cases in tests/int/next-saturday-start.int.spec.ts.
2026-10-10 14:54:06 -04:00

99 lines
3.3 KiB
TypeScript

export const UNIT_TIME_ZONE = "America/New_York";
export interface ZonedParts {
year: number;
month: number;
day: number;
hour: number;
minute: number;
second: number;
}
export function zonedParts(date: Date, timeZone: string = UNIT_TIME_ZONE): ZonedParts {
const parts = new Intl.DateTimeFormat("en-US", {
timeZone,
hour12: false,
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
}).formatToParts(date);
const get = (type: string) => Number(parts.find((p) => p.type === type)?.value ?? 0);
return {
year: get("year"),
month: get("month"),
day: get("day"),
hour: get("hour") % 24,
minute: get("minute"),
second: get("second"),
};
}
function pad2(n: number): string {
return String(n).padStart(2, "0");
}
export function dateKeyOf(year: number, month: number, day: number): string {
return `${String(year).padStart(4, "0")}-${pad2(month)}-${pad2(day)}`;
}
export function zonedDateKey(date: Date, timeZone: string = UNIT_TIME_ZONE): string {
const p = zonedParts(date, timeZone);
return dateKeyOf(p.year, p.month, p.day);
}
function tzOffsetMs(date: Date, timeZone: string): number {
const p = zonedParts(date, timeZone);
const asUtc = Date.UTC(p.year, p.month - 1, p.day, p.hour, p.minute, p.second);
return asUtc - date.getTime();
}
export function wallTimeToInstant(
year: number,
month: number,
day: number,
hour = 0,
minute = 0,
timeZone: string = UNIT_TIME_ZONE,
): Date {
const guess = Date.UTC(year, month - 1, day, hour, minute);
let ts = guess - tzOffsetMs(new Date(guess), timeZone);
ts = guess - tzOffsetMs(new Date(ts), timeZone);
return new Date(ts);
}
export function nextDayKey(key: string): string {
const [y, m, d] = key.split("-").map(Number);
const next = new Date(Date.UTC(y, m - 1, d + 1));
return dateKeyOf(next.getUTCFullYear(), next.getUTCMonth() + 1, next.getUTCDate());
}
export function zonedWeekday(date: Date, timeZone: string = UNIT_TIME_ZONE): number {
const p = zonedParts(date, timeZone);
return new Date(Date.UTC(p.year, p.month - 1, p.day)).getUTCDay();
}
export function parseDateKey(key: string): { year: number; month: number; day: number } {
const [y, m, d] = key.split("-").map(Number);
return { year: y, month: m, day: d };
}
/**
* The upcoming operational Saturday at 20:00 unit time: the *nearest* Saturday
* (today itself while it is still before 20:00), not the Saturday after it. If
* today is Saturday and 20:00 has already passed, the following Saturday is
* used. Shared by the Missions `startDateTime` default and the weekly
* generate-mission bin so a new main op lands on the coming weekend instead of
* skipping it. Computed in unit time so a UTC deploy container does not shift
* the date by a day (the trap the Friday open-slot notice hit).
*/
export function nextSaturdayStart(now: Date = new Date()): Date {
const { year, month, day } = zonedParts(now);
const weekday = zonedWeekday(now); // 0 = Sunday .. 6 = Saturday
const daysUntilSaturday = (6 - weekday + 7) % 7;
const candidate = wallTimeToInstant(year, month, day + daysUntilSaturday, 20, 0);
if (candidate.getTime() > now.getTime()) return candidate;
return wallTimeToInstant(year, month, day + daysUntilSaturday + 7, 20, 0);
}