iobroker.pvnode 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +112 -2
- package/admin/coffee.png +0 -0
- package/admin/jsonConfig.json +188 -0
- package/admin/pvnode.png +0 -0
- package/io-package.json +199 -0
- package/lib/api.js +112 -0
- package/lib/compute.js +288 -0
- package/lib/states.js +160 -0
- package/lib/svg.js +242 -0
- package/lib/time.js +133 -0
- package/main.js +734 -0
- package/package.json +61 -4
package/lib/svg.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Ready-to-use SVG charts for VIS 1 / VIS 2 / any HTML widget.
|
|
5
|
+
* Rendered from the prepared forecast, no browser libraries needed.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const { localDate, localTime } = require('./time');
|
|
9
|
+
|
|
10
|
+
const COLORS = {
|
|
11
|
+
total: '#a6ff1e',
|
|
12
|
+
strings: ['#ffb000', '#3fc1ff', '#ff6b9a', '#b48cff', '#5be3b0', '#ff8a3d', '#e0e0e0', '#8fa3ff'],
|
|
13
|
+
clearsky: '#8a94a6',
|
|
14
|
+
band: '#a6ff1e',
|
|
15
|
+
grid: 'rgba(255,255,255,0.10)',
|
|
16
|
+
axis: 'rgba(255,255,255,0.55)',
|
|
17
|
+
now: '#ffc400',
|
|
18
|
+
bar: '#a6ff1e',
|
|
19
|
+
barMuted: 'rgba(166,255,30,0.35)',
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const TEXT = {
|
|
23
|
+
de: {
|
|
24
|
+
now: 'Jetzt',
|
|
25
|
+
total: 'Gesamt',
|
|
26
|
+
clearsky: 'Klarer Himmel',
|
|
27
|
+
string: 'String',
|
|
28
|
+
today: 'Heute',
|
|
29
|
+
tomorrow: 'Morgen',
|
|
30
|
+
days: ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'],
|
|
31
|
+
noData: 'Keine Daten',
|
|
32
|
+
},
|
|
33
|
+
en: {
|
|
34
|
+
now: 'Now',
|
|
35
|
+
total: 'Total',
|
|
36
|
+
clearsky: 'Clear sky',
|
|
37
|
+
string: 'String',
|
|
38
|
+
today: 'Today',
|
|
39
|
+
tomorrow: 'Tomorrow',
|
|
40
|
+
days: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
|
|
41
|
+
noData: 'No data',
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const esc = s => String(s).replace(/[&<>"]/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c]);
|
|
46
|
+
const fmt = (v, d = 1) =>
|
|
47
|
+
(Math.round(v * 10 ** d) / 10 ** d).toLocaleString('de-DE', { minimumFractionDigits: d, maximumFractionDigits: d });
|
|
48
|
+
|
|
49
|
+
function niceMax(v) {
|
|
50
|
+
if (v <= 0) {
|
|
51
|
+
return 1000;
|
|
52
|
+
}
|
|
53
|
+
const exp = 10 ** Math.floor(Math.log10(v));
|
|
54
|
+
for (const m of [1, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10]) {
|
|
55
|
+
if (m * exp >= v) {
|
|
56
|
+
return m * exp;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return 10 * exp;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function pathFrom(points) {
|
|
63
|
+
return points.map((p, i) => `${i ? 'L' : 'M'}${p[0].toFixed(1)},${p[1].toFixed(1)}`).join('');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Daily power curve for one day (default today) with strings and a "now" marker.
|
|
68
|
+
*
|
|
69
|
+
* @param {object} p prepared forecast (lib/compute prepare)
|
|
70
|
+
* @param {number} now epoch ms
|
|
71
|
+
* @param {object} [opt] options
|
|
72
|
+
* @param {string} [opt.lang] 'de' | 'en'
|
|
73
|
+
* @param {string} [opt.day] YYYY-MM-DD, default today
|
|
74
|
+
* @param {string[]} [opt.stringNames] names per string index
|
|
75
|
+
*/
|
|
76
|
+
function dayChart(p, now, opt = {}) {
|
|
77
|
+
const T = TEXT[opt.lang] || TEXT.de;
|
|
78
|
+
const W = 1000;
|
|
79
|
+
const H = 360;
|
|
80
|
+
const m = { l: 56, r: 16, t: 34, b: 34 };
|
|
81
|
+
const tz = p.tz;
|
|
82
|
+
const day = opt.day || localDate(now, tz);
|
|
83
|
+
const rows = p.values.filter(v => localDate(v.t, tz) === day && typeof v.pv_power === 'number');
|
|
84
|
+
if (!rows.length) {
|
|
85
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${W} ${H}" width="100%" height="100%"><text x="${W / 2}" y="${H / 2}" fill="${COLORS.axis}" font-family="sans-serif" font-size="22" text-anchor="middle">${T.noData}</text></svg>`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// x range: one hour before first / after last production, full hours
|
|
89
|
+
const prod = rows.filter(r => r.pv_power > 0 || (r.pv_power_clearsky || 0) > 0);
|
|
90
|
+
const H1 = 3600000;
|
|
91
|
+
const first = prod.length ? prod[0].t : rows[0].t;
|
|
92
|
+
const last = prod.length ? prod[prod.length - 1].t + p.slotMs : rows[rows.length - 1].t + p.slotMs;
|
|
93
|
+
const x0 = Math.floor((first - H1) / H1) * H1;
|
|
94
|
+
const x1 = Math.ceil((last + H1) / H1) * H1;
|
|
95
|
+
|
|
96
|
+
const series = rows.filter(r => r.t >= x0 && r.t <= x1);
|
|
97
|
+
let maxW = 0;
|
|
98
|
+
for (const r of series) {
|
|
99
|
+
maxW = Math.max(maxW, r.pv_power, r.pv_power_max || 0, r.pv_power_clearsky || 0);
|
|
100
|
+
}
|
|
101
|
+
const yMax = niceMax(maxW * 1.05);
|
|
102
|
+
const X = t => m.l + ((t + p.slotMs / 2 - x0) / (x1 - x0)) * (W - m.l - m.r);
|
|
103
|
+
const Y = v => H - m.b - (v / yMax) * (H - m.t - m.b);
|
|
104
|
+
|
|
105
|
+
let svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${W} ${H}" width="100%" height="100%" preserveAspectRatio="xMidYMid meet" font-family="Segoe UI, Roboto, Arial, sans-serif">`;
|
|
106
|
+
svg += `<defs><linearGradient id="pvg" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="${COLORS.total}" stop-opacity="0.45"/><stop offset="1" stop-color="${COLORS.total}" stop-opacity="0.02"/></linearGradient></defs>`;
|
|
107
|
+
|
|
108
|
+
// grid + y labels (kW)
|
|
109
|
+
for (let i = 0; i <= 4; i++) {
|
|
110
|
+
const v = (yMax / 4) * i;
|
|
111
|
+
const y = Y(v);
|
|
112
|
+
svg += `<line x1="${m.l}" y1="${y}" x2="${W - m.r}" y2="${y}" stroke="${COLORS.grid}"/>`;
|
|
113
|
+
svg += `<text x="${m.l - 8}" y="${y + 5}" fill="${COLORS.axis}" font-size="14" text-anchor="end">${fmt(v / 1000, v / 1000 < 10 && yMax < 10000 ? 1 : 0)}</text>`;
|
|
114
|
+
}
|
|
115
|
+
svg += `<text x="${m.l - 8}" y="${m.t - 14}" fill="${COLORS.axis}" font-size="13" text-anchor="end">kW</text>`;
|
|
116
|
+
|
|
117
|
+
// x labels every 2 h
|
|
118
|
+
for (let t = x0; t <= x1; t += H1) {
|
|
119
|
+
const hh = Number(localTime(t, tz).slice(0, 2));
|
|
120
|
+
if (hh % 2 === 0) {
|
|
121
|
+
const x = m.l + ((t - x0) / (x1 - x0)) * (W - m.l - m.r);
|
|
122
|
+
svg += `<line x1="${x}" y1="${m.t}" x2="${x}" y2="${H - m.b}" stroke="${COLORS.grid}"/>`;
|
|
123
|
+
svg += `<text x="${x}" y="${H - m.b + 22}" fill="${COLORS.axis}" font-size="14" text-anchor="middle">${String(hh).padStart(2, '0')}:00</text>`;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// variability band
|
|
128
|
+
if (p.hasVariability) {
|
|
129
|
+
const up = series.map(r => [X(r.t), Y(r.pv_power_max || r.pv_power)]);
|
|
130
|
+
const lo = series.map(r => [X(r.t), Y(r.pv_power_min || r.pv_power)]).reverse();
|
|
131
|
+
svg += `<path d="${pathFrom(up.concat(lo))}Z" fill="${COLORS.band}" fill-opacity="0.12"/>`;
|
|
132
|
+
}
|
|
133
|
+
// clear sky
|
|
134
|
+
if (p.hasClearsky) {
|
|
135
|
+
svg += `<path d="${pathFrom(series.map(r => [X(r.t), Y(r.pv_power_clearsky || 0)]))}" fill="none" stroke="${COLORS.clearsky}" stroke-width="2" stroke-dasharray="6 6"/>`;
|
|
136
|
+
}
|
|
137
|
+
// total area + line
|
|
138
|
+
const pts = series.map(r => [X(r.t), Y(r.pv_power)]);
|
|
139
|
+
svg += `<path d="${pathFrom(pts)}L${pts[pts.length - 1][0]},${Y(0)}L${pts[0][0]},${Y(0)}Z" fill="url(#pvg)"/>`;
|
|
140
|
+
svg += `<path d="${pathFrom(pts)}" fill="none" stroke="${COLORS.total}" stroke-width="3" stroke-linejoin="round"/>`;
|
|
141
|
+
|
|
142
|
+
// strings
|
|
143
|
+
const legend = [{ c: COLORS.total, n: T.total }];
|
|
144
|
+
if (p.strings.length > 1) {
|
|
145
|
+
p.strings.forEach((s, i) => {
|
|
146
|
+
const sr = s.rows.filter(r => r.t >= x0 && r.t <= x1 && localDate(r.t, tz) === day);
|
|
147
|
+
const c = COLORS.strings[i % COLORS.strings.length];
|
|
148
|
+
svg += `<path d="${pathFrom(sr.map(r => [X(r.t), Y(r.pv_power || 0)]))}" fill="none" stroke="${c}" stroke-width="2" stroke-linejoin="round" stroke-opacity="0.9"/>`;
|
|
149
|
+
legend.push({ c, n: (opt.stringNames && opt.stringNames[s.index]) || `${T.string} ${s.index + 1}` });
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
if (p.hasClearsky) {
|
|
153
|
+
legend.push({ c: COLORS.clearsky, n: T.clearsky, dash: true });
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// now marker
|
|
157
|
+
if (now >= x0 && now <= x1 && localDate(now, tz) === day) {
|
|
158
|
+
const x = m.l + ((now - x0) / (x1 - x0)) * (W - m.l - m.r);
|
|
159
|
+
const cur = series.find(r => r.t <= now && now < r.t + p.slotMs);
|
|
160
|
+
svg += `<line x1="${x}" y1="${m.t}" x2="${x}" y2="${H - m.b}" stroke="${COLORS.now}" stroke-width="2" stroke-dasharray="4 4"/>`;
|
|
161
|
+
if (cur) {
|
|
162
|
+
svg += `<circle cx="${x}" cy="${Y(cur.pv_power)}" r="6" fill="${COLORS.now}" stroke="#1c2430" stroke-width="2"/>`;
|
|
163
|
+
}
|
|
164
|
+
const label = `${T.now} ${localTime(now, tz)}${cur ? ` · ${fmt(cur.pv_power / 1000, 2)} kW` : ''}`;
|
|
165
|
+
const anchor = x > W - 200 ? 'end' : 'start';
|
|
166
|
+
svg += `<text x="${x + (anchor === 'start' ? 8 : -8)}" y="${m.t + 4}" fill="${COLORS.now}" font-size="14" text-anchor="${anchor}">${esc(label)}</text>`;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// legend (top right)
|
|
170
|
+
let lx = W - m.r;
|
|
171
|
+
for (const l of legend.reverse()) {
|
|
172
|
+
const w = 26 + l.n.length * 7.6;
|
|
173
|
+
lx -= w;
|
|
174
|
+
svg += `<line x1="${lx}" y1="14" x2="${lx + 16}" y2="14" stroke="${l.c}" stroke-width="3"${l.dash ? ' stroke-dasharray="4 3"' : ''}/>`;
|
|
175
|
+
svg += `<text x="${lx + 21}" y="19" fill="${COLORS.axis}" font-size="13">${esc(l.n)}</text>`;
|
|
176
|
+
lx -= 10;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
svg += '</svg>';
|
|
180
|
+
return svg;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Bar chart of the daily energy forecast (all forecast days).
|
|
185
|
+
*
|
|
186
|
+
* @param {object} p prepared forecast
|
|
187
|
+
* @param {number} now epoch ms
|
|
188
|
+
* @param {object} [opt] options
|
|
189
|
+
* @param {string} [opt.lang] 'de' | 'en'
|
|
190
|
+
*/
|
|
191
|
+
function daysChart(p, now, opt = {}) {
|
|
192
|
+
const T = TEXT[opt.lang] || TEXT.de;
|
|
193
|
+
const today = localDate(now, p.tz);
|
|
194
|
+
const days = p.daily.filter(d => d.date >= today && typeof d.pv_energy_kwh === 'number');
|
|
195
|
+
const W = 600;
|
|
196
|
+
const H = 245;
|
|
197
|
+
const m = { l: 10, r: 10, t: 30, b: 52 };
|
|
198
|
+
if (!days.length) {
|
|
199
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${W} ${H}" width="100%" height="100%"><text x="${W / 2}" y="${H / 2}" fill="${COLORS.axis}" font-family="sans-serif" font-size="22" text-anchor="middle">${T.noData}</text></svg>`;
|
|
200
|
+
}
|
|
201
|
+
const maxV = Math.max(
|
|
202
|
+
...days.map(d => Math.max(d.pv_energy_kwh, d.pv_energy_kwh_max || 0, d.pv_energy_kwh_clearsky || 0)),
|
|
203
|
+
);
|
|
204
|
+
const yMax = niceMax(maxV * 1.1);
|
|
205
|
+
const slot = (W - m.l - m.r) / Math.max(days.length, 2);
|
|
206
|
+
const bw = Math.min(slot * 0.55, 110);
|
|
207
|
+
const Y = v => H - m.b - (v / yMax) * (H - m.t - m.b);
|
|
208
|
+
|
|
209
|
+
let svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${W} ${H}" width="100%" height="100%" preserveAspectRatio="xMidYMid meet" font-family="Segoe UI, Roboto, Arial, sans-serif">`;
|
|
210
|
+
svg += `<line x1="${m.l}" y1="${Y(0)}" x2="${W - m.r}" y2="${Y(0)}" stroke="${COLORS.grid}"/>`;
|
|
211
|
+
const offset = days.length === 1 ? slot / 2 : 0;
|
|
212
|
+
days.forEach((d, i) => {
|
|
213
|
+
const cx = m.l + offset + slot * i + slot / 2;
|
|
214
|
+
const y = Y(d.pv_energy_kwh);
|
|
215
|
+
if (typeof d.pv_energy_kwh_clearsky === 'number') {
|
|
216
|
+
const yc = Y(d.pv_energy_kwh_clearsky);
|
|
217
|
+
svg += `<rect x="${cx - bw / 2}" y="${yc}" width="${bw}" height="${Y(0) - yc}" rx="8" fill="none" stroke="${COLORS.clearsky}" stroke-dasharray="5 4"/>`;
|
|
218
|
+
}
|
|
219
|
+
svg += `<rect x="${cx - bw / 2}" y="${y}" width="${bw}" height="${Math.max(Y(0) - y, 0)}" rx="8" fill="${i === 0 ? COLORS.bar : COLORS.barMuted}"/>`;
|
|
220
|
+
if (typeof d.pv_energy_kwh_min === 'number' && typeof d.pv_energy_kwh_max === 'number') {
|
|
221
|
+
svg += `<line x1="${cx}" y1="${Y(d.pv_energy_kwh_min)}" x2="${cx}" y2="${Y(d.pv_energy_kwh_max)}" stroke="#ffffff" stroke-opacity="0.7" stroke-width="2"/>`;
|
|
222
|
+
}
|
|
223
|
+
const many = days.length > 3;
|
|
224
|
+
const top = Math.min(y, typeof d.pv_energy_kwh_max === 'number' ? Y(d.pv_energy_kwh_max) : y);
|
|
225
|
+
svg += `<text x="${cx}" y="${top - 8}" fill="#ffffff" font-size="${many ? 16 : 20}" font-weight="600" text-anchor="middle">${fmt(d.pv_energy_kwh, 1)}${many ? '' : ' kWh'}</text>`;
|
|
226
|
+
const dt = new Date(`${d.date}T12:00:00Z`);
|
|
227
|
+
const label =
|
|
228
|
+
i === 0
|
|
229
|
+
? T.today
|
|
230
|
+
: i === 1
|
|
231
|
+
? T.tomorrow
|
|
232
|
+
: `${T.days[dt.getUTCDay()]} ${d.date.slice(8, 10)}.${d.date.slice(5, 7)}.`;
|
|
233
|
+
svg += `<text x="${cx}" y="${H - m.b + 24}" fill="#ffffff" font-size="${days.length > 3 ? 13 : 16}" text-anchor="middle">${esc(label)}</text>`;
|
|
234
|
+
if (typeof d.temp_min === 'number' && typeof d.temp_max === 'number') {
|
|
235
|
+
svg += `<text x="${cx}" y="${H - m.b + 44}" fill="${COLORS.axis}" font-size="${days.length > 3 ? 12 : 14}" text-anchor="middle">${fmt(d.temp_min, 0)}° / ${fmt(d.temp_max, 0)}°</text>`;
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
svg += '</svg>';
|
|
239
|
+
return svg;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
module.exports = { dayChart, daysChart };
|
package/lib/time.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Time helpers. pvnode returns site-local timestamps without offset
|
|
5
|
+
* ("2026-11-12T12:00:00") when timezone=local. We convert them to epoch ms
|
|
6
|
+
* using the site's IANA timezone, independent of the ioBroker host timezone.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const dtfCache = new Map();
|
|
10
|
+
const fmtCache = new Map();
|
|
11
|
+
|
|
12
|
+
function cachedFormat(key, locale, opts) {
|
|
13
|
+
let f = fmtCache.get(key);
|
|
14
|
+
if (!f) {
|
|
15
|
+
f = new Intl.DateTimeFormat(locale, opts);
|
|
16
|
+
fmtCache.set(key, f);
|
|
17
|
+
}
|
|
18
|
+
return f;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function getDtf(tz) {
|
|
22
|
+
let dtf = dtfCache.get(tz);
|
|
23
|
+
if (!dtf) {
|
|
24
|
+
dtf = new Intl.DateTimeFormat('en-US', {
|
|
25
|
+
timeZone: tz,
|
|
26
|
+
hourCycle: 'h23',
|
|
27
|
+
year: 'numeric',
|
|
28
|
+
month: '2-digit',
|
|
29
|
+
day: '2-digit',
|
|
30
|
+
hour: '2-digit',
|
|
31
|
+
minute: '2-digit',
|
|
32
|
+
second: '2-digit',
|
|
33
|
+
});
|
|
34
|
+
dtfCache.set(tz, dtf);
|
|
35
|
+
}
|
|
36
|
+
return dtf;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Offset of a timezone at a given instant in ms (local - UTC).
|
|
41
|
+
*
|
|
42
|
+
* @param {number} ms epoch ms
|
|
43
|
+
* @param {string} tz IANA timezone
|
|
44
|
+
*/
|
|
45
|
+
function tzOffsetMs(ms, tz) {
|
|
46
|
+
const parts = {};
|
|
47
|
+
for (const p of getDtf(tz).formatToParts(new Date(ms))) {
|
|
48
|
+
parts[p.type] = p.value;
|
|
49
|
+
}
|
|
50
|
+
const asUtc = Date.UTC(
|
|
51
|
+
Number(parts.year),
|
|
52
|
+
Number(parts.month) - 1,
|
|
53
|
+
Number(parts.day),
|
|
54
|
+
Number(parts.hour),
|
|
55
|
+
Number(parts.minute),
|
|
56
|
+
Number(parts.second),
|
|
57
|
+
);
|
|
58
|
+
return asUtc - Math.floor(ms / 1000) * 1000;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Parse a pvnode timestamp to epoch ms.
|
|
63
|
+
*
|
|
64
|
+
* @param {string} s timestamp, with or without offset
|
|
65
|
+
* @param {string} tz site timezone used for naive timestamps
|
|
66
|
+
*/
|
|
67
|
+
function parseTs(s, tz) {
|
|
68
|
+
if (typeof s !== 'string') {
|
|
69
|
+
return NaN;
|
|
70
|
+
}
|
|
71
|
+
if (/(Z|[+-]\d\d:?\d\d)$/.test(s)) {
|
|
72
|
+
return Date.parse(s);
|
|
73
|
+
}
|
|
74
|
+
const guess = Date.parse(`${s}Z`);
|
|
75
|
+
if (!tz || Number.isNaN(guess)) {
|
|
76
|
+
return guess;
|
|
77
|
+
}
|
|
78
|
+
const off1 = tzOffsetMs(guess, tz);
|
|
79
|
+
let t = guess - off1;
|
|
80
|
+
const off2 = tzOffsetMs(t, tz);
|
|
81
|
+
if (off2 !== off1) {
|
|
82
|
+
t = guess - off2;
|
|
83
|
+
}
|
|
84
|
+
return t;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Local date string YYYY-MM-DD of an instant in a timezone.
|
|
89
|
+
*
|
|
90
|
+
* @param {number} ms epoch ms
|
|
91
|
+
* @param {string} tz IANA timezone
|
|
92
|
+
*/
|
|
93
|
+
function localDate(ms, tz) {
|
|
94
|
+
return cachedFormat(`d|${tz}`, 'en-CA', {
|
|
95
|
+
timeZone: tz,
|
|
96
|
+
year: 'numeric',
|
|
97
|
+
month: '2-digit',
|
|
98
|
+
day: '2-digit',
|
|
99
|
+
}).format(new Date(ms));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Local time HH:MM of an instant in a timezone.
|
|
104
|
+
*
|
|
105
|
+
* @param {number} ms epoch ms
|
|
106
|
+
* @param {string} tz IANA timezone
|
|
107
|
+
*/
|
|
108
|
+
function localTime(ms, tz) {
|
|
109
|
+
return cachedFormat(`t|${tz}`, 'de-DE', {
|
|
110
|
+
timeZone: tz,
|
|
111
|
+
hour: '2-digit',
|
|
112
|
+
minute: '2-digit',
|
|
113
|
+
hourCycle: 'h23',
|
|
114
|
+
}).format(new Date(ms));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* ISO string with the timezone's offset, e.g. 2026-11-12T12:00:00+01:00
|
|
119
|
+
*
|
|
120
|
+
* @param {number} ms epoch ms
|
|
121
|
+
* @param {string} tz IANA timezone
|
|
122
|
+
*/
|
|
123
|
+
function isoLocal(ms, tz) {
|
|
124
|
+
const off = tzOffsetMs(ms, tz);
|
|
125
|
+
const d = new Date(ms + off);
|
|
126
|
+
const sign = off >= 0 ? '+' : '-';
|
|
127
|
+
const abs = Math.abs(off) / 60000;
|
|
128
|
+
const hh = String(Math.floor(abs / 60)).padStart(2, '0');
|
|
129
|
+
const mm = String(abs % 60).padStart(2, '0');
|
|
130
|
+
return `${d.toISOString().slice(0, 19)}${sign}${hh}:${mm}`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
module.exports = { tzOffsetMs, parseTs, localDate, localTime, isoLocal };
|