2022-05-07 16:17:37 +02:00
|
|
|
import {DateTime, FixedOffsetZone, IANAZone} from 'luxon'
|
2022-11-30 17:43:40 +01:00
|
|
|
import {luxonIANAZonesByProfile as timezones} from '../lib/luxon-timezones.js'
|
2017-11-11 22:35:41 +01:00
|
|
|
|
2019-10-20 00:19:11 +02:00
|
|
|
const parseDateTime = ({profile}, date, time, tzOffset = null, timestamp = false) => {
|
2017-12-11 17:21:50 +01:00
|
|
|
const pDate = [date.substr(-8, 4), date.substr(-4, 2), date.substr(-2, 2)]
|
|
|
|
if (!pDate[0] || !pDate[1] || !pDate[2]) {
|
|
|
|
throw new Error('invalid date format: ' + date)
|
|
|
|
}
|
|
|
|
|
|
|
|
const pTime = [time.substr(-6, 2), time.substr(-4, 2), time.substr(-2, 2)]
|
|
|
|
if (!pTime[0] || !pTime[1] || !pTime[2]) {
|
|
|
|
throw new Error('invalid time format: ' + time)
|
2017-11-11 22:35:41 +01:00
|
|
|
}
|
|
|
|
|
2018-10-15 16:21:29 +02:00
|
|
|
const daysOffset = time.length > 6 ? parseInt(time.slice(0, -6)) : 0
|
2017-12-11 17:21:50 +01:00
|
|
|
|
2018-09-22 19:22:27 +02:00
|
|
|
let timezone
|
2019-03-19 11:51:35 +01:00
|
|
|
if (tzOffset !== null) {
|
|
|
|
timezone = FixedOffsetZone.instance(tzOffset)
|
|
|
|
} else if (timezones.has(profile)) {
|
|
|
|
timezone = timezones.get(profile)
|
|
|
|
} else {
|
2018-09-22 19:22:27 +02:00
|
|
|
timezone = new IANAZone(profile.timezone)
|
|
|
|
timezones.set(profile, timezone)
|
|
|
|
}
|
|
|
|
|
2019-03-19 11:51:35 +01:00
|
|
|
let dt = DateTime.fromISO(pDate.join('-') + 'T' + pTime.join(':'), {
|
|
|
|
locale: profile.locale,
|
|
|
|
zone: timezone
|
|
|
|
})
|
|
|
|
if (daysOffset > 0) dt = dt.plus({days: daysOffset})
|
2019-04-23 17:30:53 +02:00
|
|
|
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,
|
|
|
|
}
|