2017-11-11 22:35:41 +01:00
|
|
|
'use strict'
|
|
|
|
|
2017-12-11 17:21:50 +01:00
|
|
|
const {DateTime} = require('luxon')
|
2017-11-11 22:35:41 +01:00
|
|
|
|
2017-12-11 17:21:50 +01:00
|
|
|
const parseDateTime = (profile, date, time) => {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-12-11 17:21:50 +01:00
|
|
|
const offset = time.length > 6 ? parseInt(time.slice(0, -6)) : 0
|
|
|
|
|
|
|
|
const dt = DateTime.fromISO(pDate.join('-') + 'T' + pTime.join(':'), {
|
|
|
|
locale: profile.locale,
|
|
|
|
zone: profile.timezone
|
|
|
|
})
|
|
|
|
return offset > 0 ? dt.plus({days: offset}) : dt
|
2017-11-11 22:35:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = parseDateTime
|