sisyphsu / dateparser

dateparser is a smart and high-performance date parser library, it supports hundreds of different formats, nearly all format that we may used. And this is also a showcase for "retree" algorithm.
MIT License
95 stars 23 forks source link

ISO formatted string is parsed wrong #18

Closed robin-xyzt-ai closed 2 years ago

robin-xyzt-ai commented 2 years ago
    @Test
    public void testISOString() throws ParseException {
        String input = "2016-10-29T09:20:19.000Z";
        Date simpleDateFormatDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX").parse(input);
        Date instantDate = Date.from(Instant.parse(input));
        Date parsedDate = parser.parseDate(input);

        assert Objects.equals(simpleDateFormatDate, instantDate);
        //The following assert fails
        assert Objects.equals(simpleDateFormatDate, parsedDate);
    }

fails.

The input should parse to (as the JDK code does):

Sat Oct 29 11:20:19 CEST 2016

But you get

Sat Oct 29 10:20:19 CEST 2016

The ISO timestamp is expressed in GMT. CEST is Central Europe Summer Time and is GMT+2. So 09:20 in GMT becomes 11:20 CEST. To me it looks like the JDK code is correct, and this parser is 1 hour off.

This was tested with OpenJDK11, with

andriy-samson commented 2 years ago

It looks like it is the same problem as in issue#8 see this comment The problem is in parseDate(input) method, that wrongly apply a local DST(Daylight saving time) for parsed date with time zone information. The result will be parsed correctly if you use Date.from(parser.toOffsetDateTime().toInstant()); But this workaround will work only if you know in advance that a time zone information is present in parsed string. I think this problem will be fixed soon. I will submit a new pull request to @sisyphsu soon

robin-xyzt-ai commented 2 years ago

Good catch, I overlooked that issue

sisyphsu commented 2 years ago

Great thanks to @andriy-samson , the new pull request has been merged into 1.0.8 version~

robin-xyzt-ai commented 2 years ago

Thanks. I used the new version and the issue seems indeed to be fixed.