2024-02-06 22:58:49 +01:00
|
|
|
import {DateTime, IANAZone} from 'luxon';
|
|
|
|
import {luxonIANAZonesByProfile as timezones} from '../lib/luxon-timezones.js';
|
2018-09-24 15:40:00 +02:00
|
|
|
|
2024-12-16 21:33:10 +00:00
|
|
|
const getTimezone = (profile) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
let timezone;
|
|
|
|
if (timezones.has(profile)) {
|
|
|
|
timezone = timezones.get(profile);
|
|
|
|
} else {
|
|
|
|
timezone = new IANAZone(profile.timezone);
|
|
|
|
timezones.set(profile, timezone);
|
2018-09-24 15:40:00 +02:00
|
|
|
}
|
2024-12-16 21:33:10 +00:00
|
|
|
return timezone;
|
2024-12-17 14:56:51 +00:00
|
|
|
};
|
2024-12-16 21:33:10 +00:00
|
|
|
|
2024-12-17 14:56:51 +00:00
|
|
|
const formatTime = (profile, when, includeOffset = false) => {
|
2024-12-16 21:33:10 +00:00
|
|
|
const timezone = getTimezone(profile);
|
2018-09-24 15:40:00 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
return DateTime
|
|
|
|
.fromMillis(Number(when), {
|
|
|
|
locale: profile.locale,
|
|
|
|
zone: timezone,
|
|
|
|
})
|
2024-12-07 22:46:04 +00:00
|
|
|
.startOf('second')
|
2024-12-08 21:42:57 +00:00
|
|
|
.toISO({includeOffset: includeOffset, suppressMilliseconds: true});
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|
2017-11-12 00:45:51 +01:00
|
|
|
|
2024-12-17 14:56:51 +00:00
|
|
|
const formatTimeOfDay = (profile, when) => {
|
2024-12-16 21:33:10 +00:00
|
|
|
const timezone = getTimezone(profile);
|
|
|
|
|
|
|
|
return DateTime
|
|
|
|
.fromMillis(Number(when), {
|
|
|
|
locale: profile.locale,
|
|
|
|
zone: timezone,
|
|
|
|
})
|
|
|
|
.toFormat('HH:mm');
|
|
|
|
};
|
|
|
|
|
2022-05-07 16:17:37 +02:00
|
|
|
export {
|
2024-12-17 14:56:51 +00:00
|
|
|
formatTime,
|
|
|
|
formatTimeOfDay,
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|
2024-12-17 14:56:51 +00:00
|
|
|
|
2024-12-16 21:33:10 +00:00
|
|
|
|