Console side of the firmware 3.x TimeKeeper overhaul (project-vesper F-079/F-081/F-083). The Clock tab's "Network & Time" card was read-only and showed fields the firmware no longer reports, and both analog clocks drew the browser's own time. - Time & Timezone card: device local time, zone, mode, UTC offset, DST state (incl. expired manual dates), next change, RTC synced. Read live from the device (system.get_time, clock.get_timezone) - the device is the source of truth for time settings; nothing about them is mirrored to Firestore. - EditTimezoneModal: Automatic (country -> zone from the backend timezone table, or a custom yearly rule built into a POSIX string), Manual DST (std offset, amount, this season's start/end), Fixed, Local only. - SetDeviceTimeModal: this computer's UTC time, or a typed local time. - NTP Sync card + NtpSettingsModal: automatic on/off, nightly time, servers, last result/drift; Sync Now queues clock.sync_ntp and polls clock.get_ntp. - SyncClockModal (was a placeholder): pause, enter what the face shows, clock.set_face + resume. - EditClockSettingsModal: face sync policy force/smart + max wait; mirrored as clockSettings.faceSyncMode / smartMaxWaitMin (added to the backend model, which silently dropped unknown keys - the old gmtOffsetSec/ntpServer mirror never persisted, so those dead drift checks are removed). - Device Time / Clocktower clocks now show the device's wall clock and the real face position (refreshed every 30 s while the clock runs). Live reads are chained after the existing clock.get_config drift check and face polling pauses while a modal or Sync Now is sending, because device replies are matched FIFO without a request id. Stage 5 of project-vesper docs/features/timekeeper-overhaul.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
5.2 KiB
JavaScript
121 lines
5.2 KiB
JavaScript
// frontend/src/lib/deviceTimezone.js
|
|
// Helpers for device clock timezones (firmware 3.0.0+, project-vesper F-079).
|
|
//
|
|
// The device RTC holds UTC; a timezone object tells it how to show local time:
|
|
// { mode: 'auto', name, posix } — DST switches by itself
|
|
// { mode: 'manual', name, std_offset_min, dst_offset_min, dst_start, dst_end } — one DST season
|
|
// { mode: 'fixed', name, std_offset_min, dst_offset_min } — never switches
|
|
// { mode: 'local' } — offset 0, no NTP
|
|
// Offsets are minutes EAST of UTC. dst_start / dst_end are UTC epoch seconds.
|
|
// The rule list comes from the backend (GET /api/public/timezones, docs/device-timezones.md).
|
|
|
|
import api from '@/lib/api'
|
|
import { fmtUtcOffset } from '@/lib/formatters'
|
|
|
|
export const TZ_MODES = [
|
|
{ value: 'auto', label: 'Automatic' },
|
|
{ value: 'manual', label: 'Manual DST' },
|
|
{ value: 'fixed', label: 'Fixed offset' },
|
|
{ value: 'local', label: 'Local only' },
|
|
]
|
|
|
|
export const TZ_MODE_LABELS = Object.fromEntries(TZ_MODES.map(m => [m.value, m.label]))
|
|
|
|
// UTC-12:00 … UTC+14:00 in 15-minute steps (covers Nepal +5:45, Chatham +12:45)
|
|
export const STD_OFFSET_OPTIONS = Array.from({ length: (840 + 720) / 15 + 1 }, (_, i) => {
|
|
const value = -720 + i * 15
|
|
return { value, label: fmtUtcOffset(value) }
|
|
})
|
|
|
|
export const DST_AMOUNT_OPTIONS = [
|
|
{ value: 30, label: '+30 min' },
|
|
{ value: 60, label: '+1 hour' },
|
|
{ value: 120, label: '+2 hours' },
|
|
]
|
|
|
|
export const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
|
|
'August', 'September', 'October', 'November', 'December']
|
|
export const WEEKS = [
|
|
{ value: 1, label: '1st' }, { value: 2, label: '2nd' }, { value: 3, label: '3rd' },
|
|
{ value: 4, label: '4th' }, { value: 5, label: 'Last' },
|
|
]
|
|
export const WEEKDAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
|
|
|
|
// ── Timezone table (cached for the session) ───────────────────────────────────
|
|
|
|
let tablePromise = null
|
|
|
|
/** { tzdata_version, countries: [{ code, name, zones: [...] }] } — fetched once. */
|
|
export function loadTimezoneTable() {
|
|
if (!tablePromise) {
|
|
tablePromise = api.get('/public/timezones').catch(err => {
|
|
tablePromise = null // allow a retry after a failure
|
|
throw err
|
|
})
|
|
}
|
|
return tablePromise
|
|
}
|
|
|
|
/** The country containing an IANA zone, or null. */
|
|
export function findCountryOfZone(table, zoneName) {
|
|
return table?.countries.find(c => c.zones.some(z => z.name === zoneName)) || null
|
|
}
|
|
|
|
// ── POSIX rule builder (custom recurring DST) ─────────────────────────────────
|
|
|
|
// POSIX offsets are WEST-positive h[:mm] — the opposite sign of our minutes-east values.
|
|
function posixOffset(minutesEast) {
|
|
const west = -minutesEast
|
|
const sign = west < 0 ? '-' : ''
|
|
const abs = Math.abs(west)
|
|
const mm = abs % 60
|
|
return `${sign}${Math.floor(abs / 60)}${mm ? `:${String(mm).padStart(2, '0')}` : ''}`
|
|
}
|
|
|
|
// Abbreviation for a numeric offset: "<+02>", "<+0545>", "<-03>"
|
|
function posixName(minutesEast) {
|
|
const sign = minutesEast < 0 ? '-' : '+'
|
|
const abs = Math.abs(minutesEast)
|
|
const mm = abs % 60
|
|
return `<${sign}${String(Math.floor(abs / 60)).padStart(2, '0')}${mm ? String(mm).padStart(2, '0') : ''}>`
|
|
}
|
|
|
|
// "HH:MM" → "/H" or "/H:MM"
|
|
function posixTime(hhmm) {
|
|
const [h, m] = hhmm.split(':').map(Number)
|
|
return `/${h}${m ? `:${String(m).padStart(2, '0')}` : ''}`
|
|
}
|
|
|
|
/**
|
|
* Builds a POSIX TZ rule for a recurring DST schedule, e.g. std +120, DST +60,
|
|
* last Sunday of March 03:00 → last Sunday of October 04:00:
|
|
* "<+02>-2<+03>,M3.5.0/3,M10.5.0/4"
|
|
* `start.time` is local standard time, `end.time` local DST time (POSIX convention).
|
|
* dstAmountMin 0 → no DST part.
|
|
*/
|
|
export function buildPosixRule({ stdOffsetMin, dstAmountMin, start, end }) {
|
|
const std = `${posixName(stdOffsetMin)}${posixOffset(stdOffsetMin)}`
|
|
if (!dstAmountMin) return std
|
|
const total = stdOffsetMin + dstAmountMin
|
|
const dst = `${posixName(total)}${dstAmountMin === 60 ? '' : posixOffset(total)}`
|
|
const date = r => `M${r.month}.${r.week}.${r.weekday}${posixTime(r.time)}`
|
|
return `${std}${dst},${date(start)},${date(end)}`
|
|
}
|
|
|
|
// ── Wall-clock helpers ────────────────────────────────────────────────────────
|
|
|
|
/** "2026-03-29T03:00[:SS]" → epoch seconds, read as if it were UTC (no browser timezone). */
|
|
export function wallClockToEpoch(value) {
|
|
if (!value) return null
|
|
const ms = Date.parse(`${value.length === 16 ? `${value}:00` : value}Z`)
|
|
return Number.isNaN(ms) ? null : Math.floor(ms / 1000)
|
|
}
|
|
|
|
/** Epoch seconds → "YYYY-MM-DDTHH:MM" wall clock at the given offset (for datetime-local inputs). */
|
|
export function epochToWallClock(utcSeconds, offsetMin) {
|
|
if (!utcSeconds) return ''
|
|
// toISOString() is intentional here: the epoch is already shifted to the device's wall
|
|
// clock, so reading it back as UTC gives exactly those fields (no browser timezone involved).
|
|
return new Date((utcSeconds + offsetMin * 60) * 1000).toISOString().slice(0, 16)
|
|
}
|