]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fixes #6936 - extended the browser info timezone api
authorJonatan Kronqvist <jonatan.kronqvist@itmill.com>
Thu, 5 May 2011 10:57:28 +0000 (10:57 +0000)
committerJonatan Kronqvist <jonatan.kronqvist@itmill.com>
Thu, 5 May 2011 10:57:28 +0000 (10:57 +0000)
svn changeset:18646/svn branch:6.6

src/com/vaadin/terminal/gwt/client/ApplicationConnection.java
src/com/vaadin/terminal/gwt/client/BrowserInfo.java
src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java
src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java
src/com/vaadin/terminal/gwt/server/WebBrowser.java
tests/src/com/vaadin/tests/application/WebBrowserTest.html
tests/src/com/vaadin/tests/application/WebBrowserTest.java

index bc86b18daa66ad7046ce5d77a975e1329efbb644..6cd9d6bed9e431768a304a9afeaeaac19cc210ce 100755 (executable)
@@ -352,6 +352,9 @@ public class ApplicationConnection {
         int screenHeight = BrowserInfo.get().getScreenHeight();
         int tzOffset = BrowserInfo.get().getTimezoneOffset();
         int rtzOffset = BrowserInfo.get().getRawTimezoneOffset();
+        int dstDiff = BrowserInfo.get().getDSTDifference();
+        boolean dstInEffect = BrowserInfo.get().isDSTInEffect();
+        long curDate = BrowserInfo.get().getCurrentDate().getTime();
         String widgetsetVersion = ApplicationConfiguration.VERSION;
 
         String token = History.getToken();
@@ -362,8 +365,9 @@ public class ApplicationConnection {
         String parameters = "repaintAll=1&" + "sh=" + screenHeight + "&sw="
                 + screenWidth + "&cw=" + clientWidth + "&ch=" + clientHeight
                 + "&vw=" + offsetWidth + "&vh=" + offsetHeight + "&fr=" + token
-                + "&tzo=" + tzOffset + "&rtzo=" + rtzOffset + "&wsver="
-                + widgetsetVersion;
+                + "&tzo=" + tzOffset + "&rtzo=" + rtzOffset + "&dstd="
+                + dstDiff + "&dston=" + dstInEffect + "&curdate=" + curDate
+                + "&wsver=" + widgetsetVersion;
         return parameters;
     }
 
index 5b6c1f8d0b9d36cad5979cfa39a493a4aeebfb87..966dd3f8632cd339019df1d1a6f9e901ccc4e3a8 100644 (file)
@@ -4,6 +4,8 @@
 
 package com.vaadin.terminal.gwt.client;
 
+import java.util.Date;
+
 import com.google.gwt.user.client.ui.RootPanel;
 
 /**
@@ -320,7 +322,7 @@ public class BrowserInfo {
     }-*/;
 
     /**
-     * Get's the timezone offset from GMT in minutes, as reported by the browser
+     * Gets the timezone offset from GMT in minutes, as reported by the browser
      * AND adjusted to ignore daylight savings time. DST does not affect this
      * value.
      * 
@@ -344,6 +346,51 @@ public class BrowserInfo {
 
     }-*/;
 
+    /**
+     * Gets the difference in minutes between the browser's GMT timezone and
+     * DST.
+     * 
+     * @return the amount of minutes that the timezone shifts when DST is active
+     */
+    public native int getDSTDifference()
+    /*-{
+        var d = new Date();
+        var tzo1 = d.getTimezoneOffset(); // current offset
+
+        for (var m=12;m>0;m--) {
+            d.setUTCMonth(m);
+            var tzo2 = d.getTimezoneOffset();
+            if (tzo1 != tzo2) {
+                // NOTE js indicates this 'backwards' (e.g -180) 
+                return (tzo1 > tzo2 ? tzo1-tzo2 : tzo2-tzo1); // offset w/o DST
+            }
+        }
+
+        return 0; // no DST
+    }-*/;
+
+    /**
+     * Determines whether daylight savings time (DST) is currently in effect in
+     * the region of the browser or not.
+     * 
+     * @return true if the browser resides at a location that currently is in
+     *         DST
+     */
+    public boolean isDSTInEffect() {
+        return getTimezoneOffset() != getRawTimezoneOffset();
+    }
+
+    /**
+     * Returns the current date and time of the browser. This will not be
+     * entirely accurate due to varying network latencies, but should provide a
+     * close-enough value for most cases.
+     * 
+     * @return the current date and time of the browser.
+     */
+    public Date getCurrentDate() {
+        return new Date();
+    }
+
     /**
      * @return true if the browser runs on a touch based device.
      */
index ed7ba10ec4a3d0ac76d5ce93249eccc2b12ecf63..cb4ba16f65553e9c92a19532d2a4ad30cfe35f25 100644 (file)
@@ -566,7 +566,10 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet
                 getHTTPRequestParameter(request, "sw"),
                 getHTTPRequestParameter(request, "sh"),
                 getHTTPRequestParameter(request, "tzo"),
-                getHTTPRequestParameter(request, "rtzo"));
+                getHTTPRequestParameter(request, "rtzo"),
+                getHTTPRequestParameter(request, "dstd"),
+                getHTTPRequestParameter(request, "dstActive"),
+                getHTTPRequestParameter(request, "curdate"));
     }
 
     @Override
index 8a39b0307e9b9a779009e99f0d4499c1acf1d985..f274a474dd62c9f3b01f00541650f7d0c0d9b544 100644 (file)
@@ -593,7 +593,8 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements
                 request.getRemoteAddr(), request.isSecure(),
                 request.getHeader("user-agent"), request.getParameter("sw"),
                 request.getParameter("sh"), request.getParameter("tzo"),
-                request.getParameter("rtzo"));
+                request.getParameter("rtzo"), request.getParameter("dstd"),
+                request.getParameter("dston"), request.getParameter("curdate"));
     }
 
     protected ClassLoader getClassLoader() throws ServletException {
index 59cb6e690357ffc0e4943636530711ac6c784a06..7c773130193fa39c3a25d80eb370481b22249252 100644 (file)
@@ -4,6 +4,7 @@
 
 package com.vaadin.terminal.gwt.server;
 
+import java.util.Date;
 import java.util.Locale;
 
 import com.vaadin.terminal.Terminal;
@@ -27,6 +28,9 @@ public class WebBrowser implements Terminal {
     private boolean secureConnection;
     private int timezoneOffset = 0;
     private int rawTimezoneOffset = 0;
+    private int dstDifference;
+    private boolean dstInEffect;
+    private Date currentDate;
 
     private VBrowserDetails browserDetails;
 
@@ -87,10 +91,17 @@ public class WebBrowser implements Terminal {
      *            TimeZone offset in minutes from GMT
      * @param rtzo
      *            raw TimeZone offset in minutes from GMT (w/o DST adjustment)
+     * @param dstDiff
+     *            the difference between the raw TimeZone and DST in minutes
+     * @param dstInEffect
+     *            is DST currently active in the region or not?
+     * @param curDate
+     *            the current date in milliseconds since the epoch
      */
     void updateBrowserProperties(Locale locale, String address,
             boolean secureConnection, String agent, String sw, String sh,
-            String tzo, String rtzo) {
+            String tzo, String rtzo, String dstDiff, String dstInEffect,
+            String curDate) {
         this.locale = locale;
         this.address = address;
         this.secureConnection = secureConnection;
@@ -123,6 +134,25 @@ public class WebBrowser implements Terminal {
                 rawTimezoneOffset = 0; // default gmt+0
             }
         }
+        if (dstDiff != null) {
+            try {
+                // browser->java conversion: min->ms
+                dstDifference = Integer.parseInt(dstDiff) * 60 * 1000;
+            } catch (final NumberFormatException e) {
+                dstDifference = 0; // default no difference
+            }
+        }
+        if (dstInEffect != null) {
+            this.dstInEffect = Boolean.parseBoolean(dstInEffect);
+        }
+        if (curDate != null) {
+            try {
+                long curTime = Long.parseLong(curDate);
+                currentDate = new Date(curTime);
+            } catch (final NumberFormatException e) {
+                currentDate = new Date();
+            }
+        }
     }
 
     /**
@@ -313,4 +343,36 @@ public class WebBrowser implements Terminal {
         return rawTimezoneOffset;
     }
 
+    /**
+     * Gets the difference in minutes between the browser's GMT TimeZone and
+     * DST.
+     * 
+     * @return the amount of minutes that the TimeZone shifts when DST is active
+     */
+    public int getDSTDifference() {
+        return dstDifference;
+    }
+
+    /**
+     * Determines whether daylight savings time (DST) is currently in effect in
+     * the region of the browser or not.
+     * 
+     * @return true if the browser resides at a location that currently is in
+     *         DST
+     */
+    public boolean isDSTInEffect() {
+        return dstInEffect;
+    }
+
+    /**
+     * Returns the current date and time of the browser. This will not be
+     * entirely accurate due to varying network latencies, but should provide a
+     * close-enough value for most cases.
+     * 
+     * @return the current date and time of the browser.
+     */
+    public Date getCurrentDate() {
+        return currentDate;
+    }
+
 }
index e0f76263b142a8943375255f653ddefa7f010511..75cd03dce1745c99c45080ecfa2ec6b308b22a8b 100644 (file)
 <tr><td rowspan="1" colspan="3">New Test</td></tr>
 </thead><tbody>
 <tr>
-       <td>open</td>
-       <td>/run/com.vaadin.tests.application.WebBrowserTest?restartApplication</td>
-       <td></td>
+    <td>open</td>
+    <td>/run/com.vaadin.tests.application.WebBrowserTest?restartApplication</td>
+    <td></td>
 </tr>
 <tr>
-       <td>click</td>
-       <td>vaadin=runcomvaadintestsapplicationWebBrowserTest::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VButton[0]/domChild[0]/domChild[0]</td>
-       <td></td>
+    <td>click</td>
+    <td>vaadin=runcomvaadintestsapplicationWebBrowserTest::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VButton[0]/domChild[0]/domChild[0]</td>
+    <td></td>
 </tr>
+<!-- Raw offset -->
 <tr>
-       <td>assertText</td>
-       <td>vaadin=runcomvaadintestsapplicationWebBrowserTest::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[2]/VLabel[0]</td>
-       <td>7200000</td>
+    <td>assertText</td>
+    <td>vaadin=runcomvaadintestsapplicationWebBrowserTest::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[2]/VLabel[0]</td>
+    <td>7200000</td>
 </tr>
+<!-- offset to Helsinki -->
 <tr>
-       <td>assertText</td>
-       <td>vaadin=runcomvaadintestsapplicationWebBrowserTest::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[3]/VLabel[0]</td>
-       <td>0</td>
+    <td>assertText</td>
+    <td>vaadin=runcomvaadintestsapplicationWebBrowserTest::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[6]/VLabel[0]</td>
+    <td>0</td>
 </tr>
+<!-- in Helsinki? -->
 <tr>
-       <td>assertText</td>
-       <td>vaadin=runcomvaadintestsapplicationWebBrowserTest::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[4]/VLabel[0]</td>
-       <td>Yes</td>
+    <td>assertText</td>
+    <td>vaadin=runcomvaadintestsapplicationWebBrowserTest::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[7]/VLabel[0]</td>
+    <td>Yes</td>
+</tr>
+<!-- DST active? -->
+<tr>
+    <td>assertText</td>
+    <td>vaadin=runcomvaadintestsapplicationWebBrowserTest::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[4]/VLabel[0]</td>
+    <td>Yes</td>
 </tr>
-
 </tbody></table>
 </body>
 </html>
index d304ddc8b3cf0451e18cb3714e3156dd40b1cae7..60339efdafffe1d3af79996cb734c1e1979c3e5c 100644 (file)
@@ -20,6 +20,15 @@ public class WebBrowserTest extends TestBase {
         final Label rawOffsetLabel = new Label("n/a");\r
         rawOffsetLabel.setCaption("Browser raw offset");\r
 \r
+        final Label dstDiffLabel = new Label("n/a");\r
+        dstDiffLabel.setCaption("Difference between raw offset and DST");\r
+\r
+        final Label dstInEffectLabel = new Label("n/a");\r
+        dstInEffectLabel.setCaption("Is DST currently active?");\r
+\r
+        final Label curDateLabel = new Label("n/a");\r
+        curDateLabel.setCaption("Current date in the browser");\r
+\r
         final Label diffLabel = new Label("n/a");\r
         diffLabel.setCaption("Browser to Europe/Helsinki offset difference");\r
 \r
@@ -52,12 +61,25 @@ public class WebBrowserTest extends TestBase {
                                 - hkiOffset));\r
 \r
                         containsLabel.setValue(contains ? "Yes" : "No");\r
+\r
+                        dstDiffLabel.setValue(String.valueOf(getBrowser()\r
+                                .getDSTDifference()));\r
+\r
+                        dstInEffectLabel\r
+                                .setValue(getBrowser().isDSTInEffect() ? "Yes"\r
+                                        : "No");\r
+\r
+                        curDateLabel.setValue(getBrowser().getCurrentDate()\r
+                                .toString());\r
                     }\r
                 });\r
 \r
         addComponent(update);\r
         addComponent(offsetLabel);\r
         addComponent(rawOffsetLabel);\r
+        addComponent(dstDiffLabel);\r
+        addComponent(dstInEffectLabel);\r
+        addComponent(curDateLabel);\r
         addComponent(diffLabel);\r
         addComponent(containsLabel);\r
 \r