realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

datetime conversion is off by 1 hour?

Open JohnnyTwoShoes opened this issue 10 years ago • 8 comments

Hi,

I have a rest call, with a datetime value of "2016-06-10T13:00:00Z" when it's imported into realm model, the value is 10 Jun 2016, 14:00:00

Is this a bug?

Realm version(s): 0.87.0

Android Studio version: 1.5

Which Android version and device: 4.4.4 api19 genymotion

JohnnyTwoShoes avatar Jun 07 '16 16:06 JohnnyTwoShoes

What is your timezone?

Zhuinden avatar Jun 07 '16 19:06 Zhuinden

The final Z at the end of the string is part of the ISO 8601 standard and stands for Zulu, which means UTC. If you don't like in a UTC zone, the Date class will take care of the conversion.

emanuelez avatar Jun 07 '16 19:06 emanuelez

I'm not setting any timezones. I'm just reading some rest data, and using copyToRealmOrUpdate to generate the model data. I'm in the UK; so my timezone is BST. Also, if I remove the Z, it fails to convert the string into the realm object.

JohnnyTwoShoes avatar Jun 08 '16 00:06 JohnnyTwoShoes

How are you checking the date? Calling Date.toString() will just use the default timezone when formatting the date. For BST that currently means +1 compared to UTC due to summer time.

cmelchior avatar Jun 08 '16 04:06 cmelchior

Z is GMT BST is GMT+1

Your Date instance receives the millis and uses the Locale of the current timezone, which is BST, which is GMT+1

Which means that 10 Jun 2016, 13:00:00 in GMT is actually 10 Jun 2016, 14:00:00 in BST

So in this case, java.util.Date is working as intended

Zhuinden avatar Jun 08 '16 06:06 Zhuinden

Like most databases, Realm stores time/date as UTC i.e., with no information about timezone. We don't provide a time/date-with-timezone type. I guess we could clarify that in the documentation.

Fun fact: Iceland is the only country which has been in the same time zone (no summer/winter time).

kneth avatar Jun 08 '16 07:06 kneth

Hi,

ok, thanks. Quick question, Using "2016-06-10T13:00:00BST" fails ( realm fails to read the data and convert to object). Is that as expected also?

JohnnyTwoShoes avatar Jun 08 '16 09:06 JohnnyTwoShoes

Yes, adding BST is not a valid ISO8601 format you need to specify the offset using +xx:yy: https://en.wikipedia.org/wiki/ISO_8601

cmelchior avatar Jun 08 '16 09:06 cmelchior