Getting browser/client time zone offset for a specific date

Hi,

I am trying the get the time zone offset of the browser/client for a specific date in the future. I know you can get the current time zone offset using the following :

Page.getCurrent().getWebBrowser().getTimezoneOffset()

But is it possible to get the time zone offset for a date in the future ? The reason I need this information is that I am trying to make sure that a specific time on any day will always be displayed as the same time no matter if you are in Daylight Saving Time or not.

Thanks,

Simon

Hi,

Unless I’m missing something, here’s the best that you can do based on the current available information

int browserRawOffset = getBrowser().getRawTimezoneOffset();
String[] tzs = TimeZone.getAvailableIDs(browserRawOffset);

for (String tz : tzs) {
    boolean inDaylightTime = TimeZone.getTimeZone(tz).inDaylightTime(dateIWantToCheck);
}

so you’ll have a list of potential TimeZones the browser could be in, based on the raw offset, but if there’s more than one, could be that some of them are in DST and others are not.

You might get better results with a JavaScript snippet, perhaps using moment.js, but I’m not sure if all browsers are capable of giving accurate TZ information (more than what you get with RawTimezoneOffset).

Best regards,

Olli

Thank you for your answer.

That is exactly what I thought, it doesn’t seem to be posible using striclty Vaadin.

I have look into using Javascript with Vaadin but I am not familiar with it. I see you can use

Page.getCurrent().getJavaScript().execute(script)

But obviously I need the offset value in my Vaadin code. How would I go about returning a value from a javascript function to my Vaadin code ?

You can add JavaScript function callbacks in your Java code. See https://vaadin.com/docs/-/part/framework/advanced/advanced-javascript.html for more details on that.

-Olli

Thank you, I managed to get something that works well. Javascript gives you the offset the opposite way that java does thought so you got to be careful about that. getBrowserTimeZoneOffset() gives me -240 while javascript date.getTimezoneOffset() gives me 240 for today in Eastern Standard Time.

        final int currentOffset = TimeZoneUtils.getBrowserTimeZoneOffset();

        JavaScript.getCurrent().addFunction("fixTimeZoneOffsetForDate", new JavaScriptFunction() {

          @Override
          public void call(JsonArray arguments) {
            int specificDateOffset = -1 * (int) arguments.getNumber(0);

            Calendar cal = Calendar.getInstance();
            cal.setTime(value);
            cal.add(Calendar.MINUTE, specificDateOffset - currentOffset);

            nextDateControl.setReadOnly(false);
            nextDateControl.setValue(cal.getTime());
            nextDateControl.setReadOnly(true);
          }
        });

        JavaScript.getCurrent().execute(
            "var date = new Date(" + value.getTime()
                + "); fixTimeZoneOffsetForDate(date.getTimezoneOffset());");

That’s a neat solution, I was expecting it to be more complicated :slight_smile:

-Olli