Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT DateTimeFormat reverses timezone value

Consider the following code is run in GWT:

import com.google.gwt.i18n.client.DateTimeFormat;
...
DateTimeFormat fullDateTimeFormat = DateTimeFormat.getFullDateTimeFormat();
Log.info(fullDateTimeFormat.format(date, TimeZone.createTimeZone(-120)));
Log.info(fullDateTimeFormat.format(date, TimeZone.createTimeZone(0)));
Log.info(fullDateTimeFormat.format(date, TimeZone.createTimeZone(180))); 

And supposing it is Greenwich time 16:00.

Why do I get the following output?

Monday, February 21, 2011 6:00:00 PM Etc/GMT-2
Monday, February 21, 2011 4:00:00 PM Etc/GMT
Monday, February 21, 2011 1:00:00 PM Etc/GMT+3

The expected one is

Monday, February 21, 2011 2:00:00 PM Etc/GMT-2
Monday, February 21, 2011 4:00:00 PM Etc/GMT
Monday, February 21, 2011 7:00:00 PM Etc/GMT+3

What is the right way to fix it?

like image 435
Zalivaka Avatar asked Feb 21 '11 16:02

Zalivaka


1 Answers

"Etc/GMT-2" is actually (and very surprisingly) "+02:00", see http://en.wikipedia.org/wiki/Tz_database#Area:

In order to conform with the POSIX style, those zones beginning with "Etc/GMT" have their sign reversed from what most people expect. In this style, zones west of GMT have a positive sign and those east have a negative sign.

Your code leads to a different output on my machine (probably because of my different Locale):

Monday, 2011 February 21 18:00:00 UTC+2
Monday, 2011 February 21 16:00:00 UTC
Monday, 2011 February 21 13:00:00 UTC-3

So, it's not DateTimeFormat which is responsible for the reversing, but TimeZone.createTimeZone(int timeZoneOffsetInMinutes)!


Let's look a bit more into the GWT javadocs of com.google.gwt.i18n.client.TimeZone:

getOffset(Date date):

* Returns the RFC representation of the time zone name for the given date.
* To be consistent with JDK/Javascript API, west of Greenwich will be
* positive.

and composeGMTString(int offset):

* In GMT representation, +/- has reverse sign of time zone offset.
* when offset == 480, it should output GMT-08:00.
like image 152
Chris Lercher Avatar answered Oct 13 '22 01:10

Chris Lercher