konsoletyper / teavm

Compiles Java bytecode to JavaScript, WebAssembly and C
https://teavm.org
Apache License 2.0
2.66k stars 265 forks source link

JS exported DateTimeFormatter fails to parse date time with time zone #960

Closed dangmai closed 1 month ago

dangmai commented 1 month ago

I create a small repo to show this behavior here: https://github.com/dangmai/teavm-datetimeformatter-bug

In a nutshell, this code works when run directly in Java (./gradlew run in the repo above):

// Parse without timezone
System.out.println(
    (new DateTimeFormatterBuilder()).append(
        DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"))
        .toFormatter()
        .parse("2018-01-02T03:04:05")
        .toString());
// Parse with timezone
System.out.println(
    (new DateTimeFormatterBuilder()).append(
        DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz"))
        .toFormatter()
        .parse("2018-01-02T03:04:05+01:00")
        .toString());

The same code, after being generated by TeaVM to Javascript, does not work. The first string does parse correctly, but the second string fails with error Uncaught Error: Text '2018-01-02T03:04:05+01:00' could not be parsed at index 19.

I've configured both TimeZone autodetect and added a few more locales, as can be seen here.

I've tried searching issues/discussions in this repo with keywords like Timezone and locale, but I didn't find any solution.

Thank you!

dangmai commented 1 month ago

Might be related to threeten's implementation: https://github.com/ThreeTen/threetenbp/issues/67

dangmai commented 1 month ago

Closing this one since it's on threeten side. For my particular use case I have to change the code to something like:

DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(
        DateTimeFormatter.ofPattern(
            "yyyy-MM-dd'T'HH:mm:ss"))
        .appendOptional(new DateTimeFormatterBuilder().appendOffset("+HH:MM", "Z").toFormatter())
        .appendOptional(new DateTimeFormatterBuilder().appendOffset("+HHMM", "Z").toFormatter())
        .toFormatter();
konsoletyper commented 1 month ago

@dangmai anyway, it does not look like they going to fix it. So I believe, I could fix this myself someday in the forked version (which is used by TeaVM).