2024-02-06 22:58:49 +01:00
|
|
|
import {DateTime, FixedOffsetZone, IANAZone} from 'luxon';
|
|
|
|
import {luxonIANAZonesByProfile as timezones} from '../lib/luxon-timezones.js';
|
2017-11-11 22:35:41 +01:00
|
|
|
|
2023-12-05 15:01:35 +01:00
|
|
|
const parseDaysOffset = (_, time) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
return time.length > 6
|
|
|
|
? parseInt(time.slice(0, -6))
|
|
|
|
: 0;
|
|
|
|
};
|
2023-12-05 15:01:35 +01:00
|
|
|
|
|
|
|
const parseDateTime = (ctx, date, time, tzOffset = null, timestamp = false) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
const {profile} = ctx;
|
2023-12-05 15:01:35 +01:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const pDate = [date.substr(-8, 4), date.substr(-4, 2), date.substr(-2, 2)];
|
2017-12-11 17:21:50 +01:00
|
|
|
if (!pDate[0] || !pDate[1] || !pDate[2]) {
|
2024-02-06 22:58:49 +01:00
|
|
|
throw new Error('invalid date format: ' + date);
|
2017-12-11 17:21:50 +01:00
|
|
|
}
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const pTime = [time.substr(-6, 2), time.substr(-4, 2), time.substr(-2, 2)];
|
2017-12-11 17:21:50 +01:00
|
|
|
if (!pTime[0] || !pTime[1] || !pTime[2]) {
|
2024-02-06 22:58:49 +01:00
|
|
|
throw new Error('invalid time format: ' + time);
|
2017-11-11 22:35:41 +01:00
|
|
|
}
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const daysOffset = parseDaysOffset(ctx, time);
|
2017-12-11 17:21:50 +01:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
let timezone;
|
2019-03-19 11:51:35 +01:00
|
|
|
if (tzOffset !== null) {
|
2024-02-06 22:58:49 +01:00
|
|
|
timezone = FixedOffsetZone.instance(tzOffset);
|
2019-03-19 11:51:35 +01:00
|
|
|
} else if (timezones.has(profile)) {
|
2024-02-06 22:58:49 +01:00
|
|
|
timezone = timezones.get(profile);
|
2019-03-19 11:51:35 +01:00
|
|
|
} else {
|
2024-02-06 22:58:49 +01:00
|
|
|
timezone = new IANAZone(profile.timezone);
|
|
|
|
timezones.set(profile, timezone);
|
2018-09-22 19:22:27 +02:00
|
|
|
}
|
|
|
|
|
2019-03-19 11:51:35 +01:00
|
|
|
let dt = DateTime.fromISO(pDate.join('-') + 'T' + pTime.join(':'), {
|
|
|
|
locale: profile.locale,
|
2024-02-06 22:58:49 +01:00
|
|
|
zone: timezone,
|
|
|
|
});
|
|
|
|
if (daysOffset > 0) {
|
|
|
|
dt = dt.plus({days: daysOffset});
|
|
|
|
}
|
|
|
|
return timestamp
|
|
|
|
? dt.toMillis()
|
|
|
|
: dt.toISO({suppressMilliseconds: true});
|
|
|
|
};
|
2017-11-11 22:35:41 +01:00
|
|
|
|
2022-05-07 16:17:37 +02:00
|
|
|
export {
|
|
|
|
parseDateTime,
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|