fix: header "Next update" uses second Tuesday (closes #12)

The topbar showed the first Tuesday of the *next* month instead of
PDGA's actual cycle (second Tuesday of the month). Replace the
duplicated computeNextUpdate() with the central
getNextPDGAUpdateDate() from rating-calculator, keeping only the
formatter ("Tue 9 Jun") here.
This commit is contained in:
Samuel Enocsson
2026-05-22 09:54:57 +02:00
parent e1b9e97484
commit d336156bbb
+9 -12
View File
@@ -1,6 +1,10 @@
const { getLastRefresh } = require('../models/player');
const { getNextPDGAUpdateDate } = require('./rating-calculator');
const logger = require('../logger');
const DAY_NAMES = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const MONTH_NAMES = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
function formatRelative(isoString) {
if (!isoString) return 'Never';
const then = new Date(isoString.replace(' ', 'T') + (isoString.endsWith('Z') ? '' : 'Z'));
@@ -18,25 +22,18 @@ function formatRelative(isoString) {
return then.toISOString().slice(0, 10);
}
// First Tuesday of next month — approximation of PDGA's monthly cycle
function computeNextUpdate(now = new Date()) {
const year = now.getUTCFullYear();
const month = now.getUTCMonth() + 1; // next month, may roll over
const candidate = new Date(Date.UTC(month === 12 ? year + 1 : year, month === 12 ? 0 : month, 1));
// 0=Sun, 1=Mon, 2=Tue
const offset = (2 - candidate.getUTCDay() + 7) % 7;
candidate.setUTCDate(1 + offset);
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return `${['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][candidate.getUTCDay()]} ${candidate.getUTCDate()} ${months[candidate.getUTCMonth()]}`;
function formatNextUpdate(date) {
return `${DAY_NAMES[date.getDay()]} ${date.getDate()} ${MONTH_NAMES[date.getMonth()]}`;
}
async function getTopbarLocals() {
const nextUpdate = formatNextUpdate(getNextPDGAUpdateDate());
try {
const lastIso = await getLastRefresh();
return { lastRefresh: formatRelative(lastIso), nextUpdate: computeNextUpdate() };
return { lastRefresh: formatRelative(lastIso), nextUpdate };
} catch (err) {
logger.warn({ err }, 'topbar locals fallback');
return { lastRefresh: 'Unknown', nextUpdate: computeNextUpdate() };
return { lastRefresh: 'Unknown', nextUpdate };
}
}