/** * Real-Time Ticking & Midnight Reset Engine (V6) * - Ticking live system clock (Tehran time) * - Dynamic event status: Past (Dim), Active (Highlight), Upcoming * - Midnight Rollover Watcher (at 00:00 automatically resets daily view & triggers overdue archive) */ export class TimeEngine { constructor(onTickCallback, onMidnightCallback) { this.onTick = onTickCallback; this.onMidnight = onMidnightCallback; this.currentDateStr = this.getTodayDateString(); this.timerId = null; } start() { this.tick(); this.timerId = setInterval(() => this.tick(), 1000); } stop() { if (this.timerId) clearInterval(this.timerId); } getTodayDateString() { const d = new Date(); return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`; } getTehranTimeString() { return new Date().toLocaleTimeString('fa-IR', { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }); } getTehranDateString() { return new Intl.DateTimeFormat('fa-IR', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }).format(new Date()); } tick() { const now = new Date(); const newDateStr = this.getTodayDateString(); // Check Midnight Reset Trigger (00:00 or date shift) if (newDateStr !== this.currentDateStr) { console.log('[TimeEngine] Midnight Rollover triggered! Resetting daily dashboard.'); this.currentDateStr = newDateStr; if (typeof this.onMidnight === 'function') { this.onMidnight(newDateStr); } } if (typeof this.onTick === 'function') { this.onTick({ timeStr: this.getTehranTimeString(), dateStr: this.getTehranDateString(), rawDate: now }); } } /** * Evaluates meeting range against current time * range format example: "۱۰:۰۰ - ۱۱:۳۰" or "10:00 - 11:30" */ evaluateMeetingStatus(timeRange) { if (!timeRange) return 'upcoming'; try { const parts = timeRange.split('-').map(s => { // convert fa digits to en return s.trim().replace(/[۰-۹]/g, d => '۰۱۲۳۴۵۶۷۸۹'.indexOf(d)); }); if (parts.length < 2) return 'upcoming'; const parseMinutes = (tStr) => { const [h, m] = tStr.split(':').map(Number); return (h || 0) * 60 + (m || 0); }; const startMin = parseMinutes(parts[0]); const endMin = parseMinutes(parts[1]); const now = new Date(); const currentMin = now.getHours() * 60 + now.getMinutes(); if (currentMin > endMin) return 'past'; if (currentMin >= startMin && currentMin <= endMin) return 'active'; return 'upcoming'; } catch (e) { return 'upcoming'; } } }