31 lines
1.1 KiB
JavaScript
31 lines
1.1 KiB
JavaScript
// Resolve a wall-clock timestamp in an IANA zone, rejecting DST gaps.
|
|
export function scheduledTime(value, zone) {
|
|
if (!zone || typeof value !== 'string') throw Error('Termin und Zeitzone fehlen')
|
|
const fmt = new Intl.DateTimeFormat('sv-SE', {
|
|
timeZone: zone,
|
|
year: 'numeric',
|
|
month: '2-digit',
|
|
day: '2-digit',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
second: '2-digit',
|
|
hourCycle: 'h23',
|
|
})
|
|
if (/[zZ]$|[+-]\d{2}:\d{2}$/.test(value)) {
|
|
const d = new Date(value)
|
|
if (!Number.isFinite(+d)) throw Error('Ungültiger Termin')
|
|
return d.toISOString()
|
|
}
|
|
if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2})?$/.test(value)) throw Error('Ungültiger Termin')
|
|
const target = value.length === 16 ? value + ':00' : value
|
|
const utc = Date.parse(target + 'Z')
|
|
let guess = utc
|
|
for (let i = 0; i < 4; i++) {
|
|
const wall = fmt.format(new Date(guess)).replace(' ', 'T')
|
|
const delta = utc - Date.parse(wall + 'Z')
|
|
if (!delta) return new Date(guess).toISOString()
|
|
guess += delta
|
|
}
|
|
throw Error('Die Uhrzeit existiert in dieser Zeitzone nicht (Sommerzeitwechsel).')
|
|
}
|