JakeWharton / ThreeTenABP

An adaptation of the JSR-310 backport for Android.
Apache License 2.0
3.55k stars 133 forks source link

DateTimeParseException when parse #115

Closed t1r closed 5 years ago

t1r commented 5 years ago

An exception occurs when I try to parse a date and time with this pattern: E MMM dd yyyy HH:mm:ss 'GMT'Z (z)

With the standard java library (java.time.format.DateTimeFormatter) and ThreeTen library, the code works correctly.

Code:

fun main() {
    val input = "Thu Dec 31 1998 23:22:50 GMT+0300 (CET)"
    val inputPattern = "E MMM dd yyyy HH:mm:ss 'GMT'Z (z)"

    val ldt = org.threeten.bp.LocalDateTime.parse(input,
        org.threeten.bp.format.DateTimeFormatter.ofPattern(inputPattern, Locale.US))
    println("$ldt") //not Ok

    val ldt2 = java.time.LocalDateTime.parse(input,
        java.time.format.DateTimeFormatter.ofPattern(inputPattern, Locale.US))
    println("$ldt2") //Ok
}

Exception:

Exception in thread "main" org.threeten.bp.format.DateTimeParseException: Text 'Thu Dec 31 1998 23:22:50 GMT+0300 (CET)' could not be parsed at index 35
    at org.threeten.bp.format.DateTimeFormatter.parseToBuilder(DateTimeFormatter.java:1588)
    at org.threeten.bp.format.DateTimeFormatter.parse(DateTimeFormatter.java:1491)
    at org.threeten.bp.LocalDateTime.parse(LocalDateTime.java:444)
    at dateformatter.SimpleDateFormat1Kt.main(SimpleDateFormat1.kt:10)
    at dateformatter.SimpleDateFormat1Kt.main(SimpleDateFormat1.kt)
JakeWharton commented 5 years ago

I cannot reproduce. With the latest on master I can add the following to the sample app:

    String input = "Thu Dec 31 1998 23:22:50 GMT+0300 (CET)";
    String inputPattern = "E MMM dd yyyy HH:mm:ss 'GMT'Z (z)";

    LocalDateTime ldt = LocalDateTime.parse(input,
            DateTimeFormatter.ofPattern(inputPattern, Locale.US));
    tv.setText(ldt.toString());

and it displays the correct value: Screen Shot 2019-11-19 at 5 32 58 PM

Please provide a failing test case or executable sample that demonstrates the problem.

t1r commented 5 years ago

Hello, I think the problem is in parsing some of the Time Zone Abbreviations, like BST, MSK and so on. Here are test cases:

  //org.threeten.bp.format.DateTimeParseException: Text 'Tue Nov 19 2019 00:00:00 GMT+0300 (MSK)' could not be parsed at index 35
  @Test public void test_parse_tzdbGmtZone_MSK() {
    AndroidThreeTen.init(context);
    String dateString = "Tue Nov 19 2019 00:00:00 GMT+0300 (MSK)";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd yyyy HH:mm:ss 'GMT'Z (z)", Locale.US);
    LocalDateTime parsed = LocalDateTime.parse(dateString, formatter);
    assertEquals(parsed, LocalDateTime.of(2019, 11, 19, 0, 0, 0, 0));
  }

  //DateTimeParseException
  @Test public void test_parse_tzdbGmtZone_BST() {
    AndroidThreeTen.init(context);
    String dateString = "Tue Nov 19 2019 00:00:00 GMT+0100 (BST)";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd yyyy HH:mm:ss 'GMT'Z (z)", Locale.US);
    LocalDateTime parsed = LocalDateTime.parse(dateString, formatter);
    assertEquals(parsed, LocalDateTime.of(2019, 11, 19, 0, 0, 0, 0));
  }

  //DateTimeParseException
  @Test public void test_parse_tzdbGmtZone_NCT() {
    AndroidThreeTen.init(context);
    String dateString = "Tue Nov 19 2019 00:00:00 GMT+0100 (NCT)";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd yyyy HH:mm:ss 'GMT'Z (z)", Locale.US);
    LocalDateTime parsed = LocalDateTime.parse(dateString, formatter);
    assertEquals(parsed, LocalDateTime.of(2019, 11, 19, 0, 0, 0, 0));
  }

  //pass
  @Test public void test_parse_tzdbGmtZone_CET() {
    AndroidThreeTen.init(context);
    String dateString = "Tue Nov 19 2019 00:00:00 GMT+0100 (CET)";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd yyyy HH:mm:ss 'GMT'Z (z)", Locale.US);
    LocalDateTime parsed = LocalDateTime.parse(dateString, formatter);
    assertEquals(parsed, LocalDateTime.of(2019, 11, 19, 0, 0, 0, 0));
  }

All of them are pass in the ThreeTen lib. Thank You.

t1r commented 5 years ago

Hello @JakeWharton, I researched this issue more deeper. And found out a reason.

Briefly speaking problem is in getDisplayName() method of java.util.TimeZone, which has a different implementation for Android and Java.

Comment, from source code of androids TimeZone implementation. // BEGIN Android-changed: implement using android.icu.text.TimeZoneNames

More details:

We call getDisplayName() in the @Override public int parse(DateTimeParseContext context, CharSequence text, int position) of DateTimeFormatterBuilder class for build a zone ids map, that is used for parse time zone.

As a result, we have maps of size: 631 - in android 880 - in java

Android map doesn't contain "MSK", "BST", "NCT" and a lot of another time zone ids. What is the cause of DateTimeParseException.

How can we fix it - i don't know. It looks rather complicated, because we need to rewrite some of org.threeten.bp classes to change implementation of getDisplayName() specially for android.

Best wishes.

PS: Sorry for my bad english.

JakeWharton commented 5 years ago

This library is just a repackaging of the timezone database, it's not an Android-specific version of the library. The failures you're seeing would occur with usage of the normal ThreeTenBP library if you us it on Android. Working around this problem is going to be out-of-scope for this library. A fix would have to be pursued directly in ThreeTenBP.