Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Correctly deserialize ISO 8601 string with timezone offset #44

Merged
merged 1 commit into from
Feb 3, 2019
Merged

[BUGFIX] Correctly deserialize ISO 8601 string with timezone offset #44

merged 1 commit into from
Feb 3, 2019

Conversation

sgrossberndt
Copy link
Contributor

When unserializing a ISO 8601 string with timezone offset like 2019-01-31T10:37:20.631+01:00 using the DateTimeConverter it currently gets converted to UTC 2019-01-31T09:37:20.631Z. In order to re-instatiate the DateTime object as it was passed to Gson the offset has to be parsed.

@sgrossberndt
Copy link
Contributor Author

We are using this in production in a project and I'd like to fix it there. Could you please release 1.7.1 after merging this pull request? Thanks in advance!

@gkopff gkopff merged commit eea3303 into gkopff:master Feb 3, 2019
@gkopff
Copy link
Owner

gkopff commented Feb 3, 2019

@sgrossberndt I should have it released in the next 18 hours.

@gkopff
Copy link
Owner

gkopff commented Feb 3, 2019

@sgrossberndt And thanks for the fix.

@gkopff
Copy link
Owner

gkopff commented Feb 3, 2019

@sgrossberndt I had hoped to release this for you, but I get unit test failures running the build after the change.


Failed tests:   testDeserializeWithMilliseconds(com.fatboyindustrial.gsonjodatime.DateTimeConverterTest): (..)
  testDeserializeWithoutMilliseconds(com.fatboyindustrial.gsonjodatime.DateTimeConverterTest): (..)
  testRoundtrip(com.fatboyindustrial.gsonjodatime.DateTimeConverterTest): (..)
  testRegisterAll(com.fatboyindustrial.gsonjodatime.ConvertersTest): (..)

Tests run: 35, Failures: 4, Errors: 0, Skipped: 0

The non-obvious thing about the failure, is that (by the DateTime.toString() output) the expected and actual results are the same:

Expected :<2019-02-04T09:17:03.074+10:00>
Actual   :<2019-02-04T09:17:03.074+10:00>

...so I take it there is some other aspect of the DateTime equality check that is failing.

I don't have time to dig into this at the moment unfortunately. Do you get the test failures? (The tests pass if I back out your change).

@gkopff
Copy link
Owner

gkopff commented Feb 3, 2019

@sgrossberndt It looks like they end up with different Chronologys.

    final Gson gson = Converters.registerDateTime(new GsonBuilder()).create();
    final DateTime dt = new DateTime();

    System.out.println(dt.getChronology());
    System.out.println(gson.fromJson(gson.toJson(dt), DateTime.class).getChronology());

yields:

ISOChronology[Australia/Brisbane]
ISOChronology[+10:00]

and if the Chronologies are different, the DateTime instances are not equal.

Would you be able to see if you can find out what the right thing to do here is. These days I am more familiar with the java.time way of handling time zones and time zone offsets, which I think is subtly different to how Jodatime does it.

@gkopff
Copy link
Owner

gkopff commented Feb 11, 2019

@sgrossberndt any luck?

@sgrossberndt
Copy link
Contributor Author

sgrossberndt commented Feb 11, 2019

You're welcome. Thanks for the merge.

ISOChronology[Australia/Brisbane] has the same value for the timezone as ISOChronology[+10:00], its the name which is lost: http://joda-time.sourceforge.net/timezones.html

In the serialized string only the timezone value is saved (not the name) - so the behaviour of losing the timezone name is to be expected. This was the case before too - its just visible now as before the reconstructed DateTime always was UTC.

Mapping back to the named timezone is not trivial as in most cases there are several named timezones with the same value.

One option would be to serialize the timezone name too, but that is potentially breaking.

Other (de)serializers have the same problem:
https://stackoverflow.com/questions/20222376/default-timezone-for-datetime-deserialization-with-jackson-joda-time-module
FasterXML/jackson-datatype-joda#43

As Java 8 DateTime do not seem to suffer from that I wonder if it is worth the hassle to fix this.

Of course with this the roundtrip is "broken", one option would be to use a UTC DateTime in that test.

@sgrossberndt sgrossberndt deleted the timezone_offset branch February 11, 2019 08:33
@gkopff
Copy link
Owner

gkopff commented Feb 12, 2019

@sgrossberndt: 1.7.1 has been release to Maven Central.

@sgrossberndt
Copy link
Contributor Author

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants