开发者

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.for开发者_开发问答mat(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?


"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.


I had a look at the source code. It has comments saying

20:00 GMT0000, or 16:00 GMT-0400, or 12:00 GMT-0800

are all the same. As per that I infer the relation between time and timezone as the amount of time subtracted from GMT or added to the GMT. So 16.00 GMT becomes 1400 GMT -0200 or 1900 GMT +0300. Keeping that in mind we have to work the other way around to get the desired result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜