diff --git a/src/services/topbar-service.js b/src/services/topbar-service.js index e050667..cdcc289 100644 --- a/src/services/topbar-service.js +++ b/src/services/topbar-service.js @@ -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 }; } }