Files
AtreidesAssetsFree/shared/time.js
T

77 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2026-10-29 11:57:29 +01:00
// Generated, do not edit. sha256:ca177447c22d0d66351f016a9d6a55b9929893b9ec7e4207164d9f1592938144
export const ZONE = 'Europe/Amsterdam';
function offsetMinutes(momentMs) {
const fmt = new Intl.DateTimeFormat('en-US', {
timeZone: ZONE, hour12: false,
year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit',
});
const d = Object.fromEntries(fmt.formatToParts(new Date(momentMs))
.filter(p => p.type !== 'literal').map(p => [p.type, p.value]));
const uur = d.hour === '24' ? '00' : d.hour;
return (Date.UTC(+d.year, +d.month - 1, +d.day, +uur, +d.minute, +d.second) - momentMs) / 60000;
}
export function toUtcIso(text) {
if (text === null || text === undefined || text === '') return text;
if (/Z$/.test(text)) return text;
if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/.test(text)) return text;
const alsofUtc = Date.parse(text + 'Z');
if (Number.isNaN(alsofUtc)) throw new Error(`Onleesbaar tijdstempel: ${text}`);
const schatting = alsofUtc - offsetMinutes(alsofUtc) * 60000;
return new Date(alsofUtc - offsetMinutes(schatting) * 60000).toISOString().replace(/\.000Z$/, 'Z');
}
export function toLocalIso(iso) {
if (iso === null || iso === undefined || iso === '') return iso;
const ms = Date.parse(iso);
if (Number.isNaN(ms)) throw new Error(`Onleesbaar tijdstempel: ${iso}`);
return new Date(ms + offsetMinutes(ms) * 60000).toISOString()
.replace(/\.000Z$/, '').replace(/Z$/, '');
}
export function localDate(iso) {
const lokaal = toLocalIso(iso);
return lokaal ? String(lokaal).slice(0, 10) : '';
}
export function localDateNl(iso) {
return dayNl(localDate(iso));
}
let dateTag = 'nl-NL';
let dateFmt = null;
export function setDateLocale(tag) {
if (tag !== dateTag) { dateTag = tag; dateFmt = null; }
}
export function dayNl(day) {
if (!day) return '';
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(String(day));
if (!m) return String(day);
dateFmt ??= new Intl.DateTimeFormat(dateTag, { day: '2-digit', month: '2-digit', year: 'numeric', timeZone: 'UTC' });
return dateFmt.format(Date.UTC(+m[1], +m[2] - 1, +m[3]));
}
export function localDateTimeNl(iso) {
const lokaal = toLocalIso(iso);
return lokaal ? `${dayNl(String(lokaal).slice(0, 10))} ${String(lokaal).slice(11, 16)}` : '';
}
export function localTime(iso) {
const lokaal = toLocalIso(iso);
return lokaal ? String(lokaal).slice(11, 16) : '';
}
export function localDateTime(iso) {
const lokaal = toLocalIso(iso);
return lokaal ? `${String(lokaal).slice(0, 10)} ${String(lokaal).slice(11, 16)}` : '';
}
export function nowUtcIso() {
return new Date().toISOString();
}
export function nowLocalIso() {
return toLocalIso(new Date().toISOString());
}