refacto: Changing actions reset windows + display

This commit is contained in:
gauvainboiche
2026-04-02 15:44:58 +02:00
parent e573ea0d37
commit 3cdb6385a7
9 changed files with 70 additions and 16 deletions
+20 -4
View File
@@ -29,12 +29,28 @@ export async function initUserActionQuotaSchema() {
/** Returns the next noon (12:00:00) UTC after the current moment. */
export function nextNoonUtc() {
return nextResetUtc(12);
}
/**
* Returns the next reset timestamp UTC based on a repeating interval.
* @param {number} intervalHours - must be a divisor of 24 (1,2,3,4,6,8,12,24)
*/
export function nextResetUtc(intervalHours) {
const now = new Date();
const noon = new Date(Date.UTC(
now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 12, 0, 0, 0
const utcHours = now.getUTCHours();
const slotsPassed = Math.floor(utcHours / intervalHours);
const nextSlotHour = (slotsPassed + 1) * intervalHours;
if (nextSlotHour < 24) {
return new Date(Date.UTC(
now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),
nextSlotHour, 0, 0, 0
));
}
return new Date(Date.UTC(
now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() + 1,
0, 0, 0, 0
));
if (noon <= now) noon.setUTCDate(noon.getUTCDate() + 1);
return noon;
}
// ── Queries ───────────────────────────────────────────────────────────────────