// Set by the language provider so numbers and dates follow the chosen language let lang = 'en' const LOCALES = { en: 'en-GB', el: 'el-GR' } export function setFormatLanguage(l) { lang = LOCALES[l] ? l : 'en' } const locale = () => LOCALES[lang] const nfCache = new Map() const nf = (digits) => { const key = `${lang}:${digits}` if (!nfCache.has(key)) { nfCache.set(key, new Intl.NumberFormat(locale(), { minimumFractionDigits: digits, maximumFractionDigits: digits })) } return nfCache.get(key) } export function num(v, digits = 0) { return v === null || v === undefined || Number.isNaN(v) ? '—' : nf(digits).format(v) } /** Power with an auto unit: 950 W, 12.4 kW, 1.20 MW. Returns [value, unit]. */ export function powerParts(w) { if (w === null || w === undefined) return ['—', 'W'] const a = Math.abs(w) if (a >= 1e6) return [num(w / 1e6, 2), 'MW'] if (a >= 1000) return [num(w / 1000, a >= 1e5 ? 0 : 1), 'kW'] return [num(w, 0), 'W'] } export function power(w) { const [v, u] = powerParts(w) return `${v} ${u}` } /** Energy with an auto unit: 850 Wh, 42.3 kWh, 123.4 MWh. Returns [value, unit]. */ export function energyParts(wh) { if (wh === null || wh === undefined) return ['—', 'kWh'] const a = Math.abs(wh) if (a >= 1e6) return [num(wh / 1e6, a >= 1e8 ? 0 : 1), 'MWh'] if (a >= 1000) return [num(wh / 1000, a >= 1e5 ? 0 : 1), 'kWh'] return [num(wh, 0), 'Wh'] } export function energy(wh) { const [v, u] = energyParts(wh) return `${v} ${u}` } const UNITS = { en: { s: 's', min: 'min', h: 'h', d: 'd', days: 'days', ago: (x) => `${x} ago`, never: 'never' }, el: { s: 'δευτ.', min: 'λεπ.', h: 'ώρ.', d: 'ημ.', days: 'ημ.', ago: (x) => `πριν ${x}`, never: 'ποτέ' }, } export function ago(iso) { const u = UNITS[lang] if (!iso) return u.never const s = Math.max(0, Math.round((Date.now() - new Date(iso).getTime()) / 1000)) if (s < 60) return u.ago(`${s} ${u.s}`) if (s < 3600) return u.ago(`${Math.floor(s / 60)} ${u.min}`) if (s < 86400) return u.ago(`${Math.floor(s / 3600)} ${u.h}`) return u.ago(`${Math.floor(s / 86400)} ${u.d}`) } /** Length of a silence: 12 min, 3.5 h, 2.0 days. */ export function duration(seconds) { const u = UNITS[lang] if (seconds < 3600) return `${Math.floor(seconds / 60)} ${u.min}` if (seconds < 86400) return `${num(seconds / 3600, 1)} ${u.h}` return `${num(seconds / 86400, 1)} ${u.days}` } export function dateTime(iso) { return new Date(iso).toLocaleString(locale()) } export function pct(ratio) { return ratio === null || ratio === undefined ? '—' : `${Math.round(ratio * 100)}%` } export function timeLabel(unix, spanSeconds) { const d = new Date(unix * 1000) if (spanSeconds <= 2 * 86400) return d.toLocaleTimeString(locale(), { hour: '2-digit', minute: '2-digit' }) return d.toLocaleDateString(locale(), { weekday: 'short', day: 'numeric' }) } export function dateTimeLabel(unix) { return new Date(unix * 1000).toLocaleString(locale(), { weekday: 'short', day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit', }) } export function dayLabel(isoDay, long = false) { const d = new Date(`${isoDay}T12:00:00`) return long ? d.toLocaleDateString(locale(), { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric' }) : d.toLocaleDateString(locale(), { day: 'numeric', month: 'short' }) }