Vaadin 7.7 How can server get browsers timezone ?

I want to display dates stored in a database to the user as its (i.e. the browsers) timezone value.
Since Vaadin 8.2 class WebBrowser offers the method getTimeZoneId since.

Currently my application uses Vaadin 7.7.12 and I can not migrate to 8 due to the effort involved.

Is the a way to f.i. create a JavaScript function that returns Intl.DateTimeFormat().resolvedOptions().timeZone and fetch the value on the server ?
From the book-of-vaadin for version 7 this does not seem to be possible ?
Does anybody know a way ?

Hi,

You could create a JavaScript callback (see here: https://vaadin.com/docs/v8/framework/advanced/advanced-javascript.html ) and get the value from there. However, looks like that wouldn’t work consistently across browsers - I tested it with IE and out of resolvedOptions() I got
{calendar: "gregory", day: "numeric", locale: "fi", month: "numeric", numberingSystem: "latn", timeZone: undefined, year: "numeric"}
while Chome gives me
{locale: "en-US", numberingSystem: "latn", calendar: "gregory", timeZone: "Europe/Helsinki", year: "numeric", …}

Regards,

Olli

Hello Olli,
yes the callback works (for Firefox too), thank you!
I’m afraid InternetExplorer users will have to live with UTC time for the time being (
see also here
).

Here is how I did it (for others that might benefit):

[font=Courier New]
JavaScript.getCurrent().addFunction(“com.example.testtimezone.getTimezone”,
new JavaScriptFunction() {
@Override
public void call(JsonArray arguments) {
if(!(arguments.get(0) instanceof elemental.json.impl.JreJsonNull)) {
String timezone = arguments.getString(0);
// …
}
}});

JavaScript.getCurrent().execute(“com.example.testtimezone
.getTimezone(
Intl.DateTimeFormat().resolvedOptions().timeZone
)”);
[/font]

The JavaScript function is defined as empty function

function getTimezone(timezone)
{
}

Good to hear that you got it working.

Hello Peter,
at
icu-project.org
I found class com.ibm.icu.util.TimeZone that provides method getAvailableIDs(String country), where “country” is the country code that can be fetched via VaadinService.getCurrentRequest().getLocale().getCountry().

The method still delivers a String. If its size is > 1 I might let IE11 users choose from the result list (altough there are “strange” time zones like “Europe/Busingen” which probably no user has ever heard of).

Regards,
hp

True that this is improved in Vaadin 8.2.

However, prior to this you do indeed have a number of methods on the WebBrowser object which can be used:


isDSTInEffect(), getDSTSavings(), getTimezoneOffset()

By combining these you can create list of potential Java TimeZone candidates that matches these criteria. You then have to pick one-and-one-only from this list and this is where there’s no good answer. I just pick the first one on the list. So you end up with the algorithm picking “Europe/Rome” where the user’s actual timezone is perhaps “Europe/Stockholm”. Who cares? In my applications I can live with such minor ‘error’.