开发者

Client side time zone support in GWT

开发者 https://www.devze.com 2022-12-10 21:37 出处:网络
I\'m working on a GWT app where I need to support the following scenario: The server is located in time zone A

I'm working on a GWT app where I need to support the following scenario:

The server is located in time zone A

The client's browser is set to time zone B

The GWT app is configured to display date/time in time zone C

Since GWT does not support Calendar and the native support for time 开发者_开发技巧zones in javascript is non-existent I can't think of a nice and clean solution to this problem.

Have any of you done something similar or do you know of any good utils I could use?

Thanks!


In my experience, the following best practice significantly reduces complexity and confusion when dealing with dates and timezones in gwt:

  1. Whenever operating/storing dates within the application, treat all dates as milliseconds since epoch in GMT timezone. You can store them as string or int, it doesn't really make a difference.
  2. Whenever displaying the date to the end user, format the date using appropriate timezone.

For your case, when you create a date on the Server (timezone A) convert it to milliseconds since epoch in GMT before sending it to the Client. On the client, use DateTimeFormat (or write your own date formatter util) to convert it into either timezone B or timezone C as appropriate.


You can't change the GWT timezone, hence all java.util.Date's has the browser timezone. You will need to handle the current timezone setting manually.

I see 3 options:

  1. You manage the timezone conversion yourself.

  2. You override the serializer/deserializer of java.util.Date like in this post. And maybe using a custom java.util.Date implemtation, that overrides the getTimezoneOffset(). This approach requires recompilation of the GWT API!.

  3. You implement your own Date, either by extending java.util.Date (like in option 2) or wrapping it with some timezone object. In this option CustomFieldSerializer's may still be usefull, but there is no need for recompiling the GWT API.

I would prefer option 3. At least until GWT RPC maybe someday will support for overriding the CustomFieldSerializer's


Usefull date/time formatting hints.


Dave Paroulek's answer is the right approach. If you want to see an example of this, we created widgets that work independent of TimeZone and process the values on the server-side where we have all of the TimeZone information we need.

UTCDateBox - Wrapper around the GWT DateBox and always chooses the date at midnight in GMT and represents the value as a Long instead of a Date.

UTCTimeBox - New widget that always chooses a time as millis since midnight, independent of timezone, also represented as a Long.

UTCDateTimeUtils - Server-side code that splits a Date into 2 Long values appropriate for UTCDateBox and UTCTimeBox in a given TimeZone and combines them back into a Date in a given TimeZone.

Here is an example of the date the time controls being used together.

Blog article describing their implementation.

These widgets are available on GitHub.


I'm assuming you are using RPC calls for server-client communication here. Also assuming that you don't care about timezone B, and you know what timezone C is on the server.

You have a few options here:

  • Calculate the desired date in the server (no Java limits on what you can do there) and send it in a String to be displayed to the client, so you don't have to do anymore transformations on the client.

or:

  • Calculate the offset between timezone A and C on the server, apply it to all the Date objects you are passing to the client and just display them on the client.

if for some reason none of these were valid for you

  • Calculate the offset, send it to the client and apply it to any Date you receive from the server by transforming to ms, adding the offset and then creating a Date object again.


see this demo project GWT timezone demo project


I created a GWT-compatible Java version of the jsTimezoneDetect Javascript library specifically for this purpose. This should provide a (very good guess of) the timezone name purely on the client side. Feel free to try it out and let me know if it works or doesn't work for you.

0

精彩评论

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