[WICKET-6712] time zone is determined in browser
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/pages/wicket-browser-info.js b/wicket-core/src/main/java/org/apache/wicket/markup/html/pages/wicket-browser-info.js
index cd0da94..07e8bb5 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/pages/wicket-browser-info.js
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/pages/wicket-browser-info.js
@@ -57,7 +57,10 @@
info.screenWidth = window.screen.width;
info.screenHeight = window.screen.height;
info.screenColorDepth = window.screen.colorDepth;
+ info.utcOffset = (new Date(new Date().getFullYear(), 0, 1, 0, 0, 0, 0).getTimezoneOffset() / -60);
}
+ var jsTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone
+ info.jsTimeZone = jsTimeZone ? jsTimeZone : null;;
info.utcOffset = (new Date(new Date().getFullYear(), 0, 1, 0, 0, 0, 0).getTimezoneOffset() / -60);
info.utcDSTOffset = (new Date(new Date().getFullYear(), 6, 1, 0, 0, 0, 0).getTimezoneOffset() / -60);
info.browserWidth = window.innerWidth || document.body.offsetWidth;
diff --git a/wicket-core/src/main/java/org/apache/wicket/protocol/http/ClientProperties.java b/wicket-core/src/main/java/org/apache/wicket/protocol/http/ClientProperties.java
index ff200a1..c16aa1e 100644
--- a/wicket-core/src/main/java/org/apache/wicket/protocol/http/ClientProperties.java
+++ b/wicket-core/src/main/java/org/apache/wicket/protocol/http/ClientProperties.java
@@ -38,17 +38,17 @@
* <p>
* A convenient way of letting Wicket do a sneaky redirect to {@link BrowserInfoPage} (and back
* again) is to put this in your Application's init method:
- *
+ *
* <pre>
* getRequestCycleSettings().setGatherExtendedBrowserInfo(true);
* </pre>
- *
+ *
* </p>
- *
+ *
* WARNING: Be sure you think about the dangers of depending on information you pull from the client
* too much. They may be easily spoofed or inaccurate in other ways, and properties like window and
* browser size are all too easy to be used naively.
- *
+ *
* @see BrowserInfoPage
* @author Frank Bille (frankbille)
*/
@@ -72,6 +72,7 @@
private int screenWidth = -1;
private String utcDSTOffset;
private String utcOffset;
+ private String jsTimeZone;
private String hostname;
private boolean javaScriptEnabled;
@@ -185,11 +186,19 @@
/**
* Get the client's time zone if that could be detected.
- *
+ *
* @return The client's time zone
*/
public TimeZone getTimeZone()
{
+ if (timeZone == null && jsTimeZone != null)
+ {
+ TimeZone temptimeZone = TimeZone.getTimeZone(jsTimeZone);
+ if (jsTimeZone.equals(temptimeZone.getID()))
+ {
+ timeZone = temptimeZone;
+ }
+ }
if (timeZone == null)
{
String utc = getUtcOffset();
@@ -331,7 +340,7 @@
/**
* Flag indicating support of JavaScript in the browser.
- *
+ *
* @return True if JavaScript is enabled
*/
public boolean isJavaScriptEnabled() {
@@ -339,8 +348,8 @@
}
/**
- *
- *
+ *
+ *
* @return The client's navigator.cookieEnabled property.
*/
public boolean isNavigatorCookieEnabled()
@@ -498,7 +507,7 @@
/**
* Sets time zone.
- *
+ *
* @param timeZone
*/
public void setTimeZone(TimeZone timeZone)
@@ -525,6 +534,14 @@
}
/**
+ * @param jsTimeZone
+ */
+ public void setJsTimeZone(String jsTimeZone)
+ {
+ this.jsTimeZone = jsTimeZone;
+ }
+
+ /**
* @param javaScriptEnabled
* is JavaScript supported in the browser
*/
@@ -591,7 +608,7 @@
/**
* Read parameters.
- *
+ *
* @param parameters
* parameters sent from browser
*/
@@ -610,6 +627,7 @@
setScreenColorDepth(parameters.getParameterValue("screenColorDepth").toInt(-1));
setUtcOffset(parameters.getParameterValue("utcOffset").toString(null));
setUtcDSTOffset(parameters.getParameterValue("utcDSTOffset").toString(null));
+ setJsTimeZone(parameters.getParameterValue("jsTimeZone").toString(null));
setBrowserWidth(parameters.getParameterValue("browserWidth").toInt(-1));
setBrowserHeight(parameters.getParameterValue("browserHeight").toInt(-1));
setHostname(parameters.getParameterValue("hostname").toString("N/A"));
diff --git a/wicket-core/src/test/java/org/apache/wicket/protocol/http/ClientPropertiesTest.java b/wicket-core/src/test/java/org/apache/wicket/protocol/http/ClientPropertiesTest.java
index f42785b..f9057b4 100644
--- a/wicket-core/src/test/java/org/apache/wicket/protocol/http/ClientPropertiesTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/protocol/http/ClientPropertiesTest.java
@@ -26,7 +26,7 @@
/**
* Tests for ClientProperties that failed on Mac OS X Java platform.
- *
+ *
* @author Martijn Dashorst
*/
class ClientPropertiesTest
@@ -149,4 +149,32 @@
assertEquals(TimeZone.getTimeZone("AET"), props.getTimeZone());
}
+
+ /**
+ * jsTimeZone "positive" test
+ */
+ @Test
+ void timezoneJsPositive()
+ {
+ ClientProperties props = new ClientProperties();
+ props.setUtcOffset("11");
+ props.setUtcDSTOffset("10");
+ props.setJsTimeZone("Asia/Novosibirsk");
+
+ assertEquals(TimeZone.getTimeZone("Asia/Novosibirsk"), props.getTimeZone());
+ }
+
+ /**
+ * jsTimeZone "negative" test
+ */
+ @Test
+ void timezoneJsNegative()
+ {
+ ClientProperties props = new ClientProperties();
+ props.setUtcOffset("11");
+ props.setUtcDSTOffset("10");
+ props.setJsTimeZone("aaa");
+
+ assertEquals(TimeZone.getTimeZone("AET"), props.getTimeZone());
+ }
}