chrono icon indicating copy to clipboard operation
chrono copied to clipboard

ISO date without Z suffix are not parsed as local times

Open olivr70 opened this issue 1 year ago • 0 comments

ISO string without any timezone information are parsed as UTC time, not local time. This is unexpected, and leads to a strange situation, where Chrono.parseDate() does not return the same value as new Date() or dayjs()

I do not have access to the full standard, but from comments, it looks like the most common behaviour is interpreting it as local time

// test run in Europe/Paris timzone
const newYearLocal = "2024-01-01T00:00"
const newYearDayDate = new Date(newYearLocal)
const newYearDayJs = dayjs(newYearLocal)
const newYearDayChrono = new Chrono().parseDate(newYearLocal)

newYearDayJs.isSame(newYearDayDate) // true
newYearDayJs.isSame(newYearDayChrono ) // false

// on New Year in Paris, it is still 2023 in UTC
console.log(newYearDayDate .toIsoString()) // "2023-12-31T23:00:00.000Z"
console.log(newYearDayJs.toDate().toIsoString()) // "2023-12-31T23:00:00.000Z"
// But
console.log(newYearDayChrono.toIsoString()) // "2024-01-01T00:00:00.000Z"

It looks like it comes frome line 62 in ISOFormatParser.ts; which set the timezoneOffset to 0 (from a very uninformed reader)👍

           if (match[TZD_HOUR_OFFSET_GROUP] == null) {
                components["timezoneOffset"] = 0;

olivr70 avatar Aug 29 '24 21:08 olivr70