You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by so...@apache.org on 2019/10/31 10:37:11 UTC

[wicket] 01/03: [WICKET-6712] time zone is determined in browser

This is an automated email from the ASF dual-hosted git repository.

solomax pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit b10e68a70dcede5c5a0ef0f1dd0fa3a3720b13d5
Author: Maxim Solodovnik <so...@gmail.com>
AuthorDate: Tue Oct 29 21:59:44 2019 +0700

    [WICKET-6712] time zone is determined in browser
---
 .../markup/html/pages/wicket-browser-info.js       |  3 ++
 .../wicket/protocol/http/ClientProperties.java     | 38 ++++++++++++++++------
 .../wicket/protocol/http/ClientPropertiesTest.java | 30 ++++++++++++++++-
 3 files changed, 60 insertions(+), 11 deletions(-)

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 @@ import org.apache.wicket.util.string.AppendingStringBuffer;
  * <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 @@ public class ClientProperties implements IClusterable
 	private int screenWidth = -1;
 	private String utcDSTOffset;
 	private String utcOffset;
+	private String jsTimeZone;
 	private String hostname;
 
 	private boolean javaScriptEnabled;
@@ -185,11 +186,19 @@ public class ClientProperties implements IClusterable
 
 	/**
 	 * 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 @@ public class ClientProperties implements IClusterable
 
 	/**
 	 * Flag indicating support of JavaScript in the browser.
-	 * 
+	 *
 	 * @return True if JavaScript is enabled
 	 */
 	public boolean isJavaScriptEnabled() {
@@ -339,8 +348,8 @@ public class ClientProperties implements IClusterable
 	}
 
 	/**
-	 * 
-	 * 
+	 *
+	 *
 	 * @return The client's navigator.cookieEnabled property.
 	 */
 	public boolean isNavigatorCookieEnabled()
@@ -498,7 +507,7 @@ public class ClientProperties implements IClusterable
 
 	/**
 	 * Sets time zone.
-	 * 
+	 *
 	 * @param timeZone
 	 */
 	public void setTimeZone(TimeZone timeZone)
@@ -525,6 +534,14 @@ public class ClientProperties implements IClusterable
 	}
 
 	/**
+	 * @param jsTimeZone
+	 */
+	public void setJsTimeZone(String jsTimeZone)
+	{
+		this.jsTimeZone = jsTimeZone;
+	}
+
+	/**
 	 * @param javaScriptEnabled
 	 *            is JavaScript supported in the browser
 	 */
@@ -591,7 +608,7 @@ public class ClientProperties implements IClusterable
 
 	/**
 	 * Read parameters.
-	 * 
+	 *
 	 * @param parameters
 	 *            parameters sent from browser
 	 */
@@ -610,6 +627,7 @@ public class ClientProperties implements IClusterable
 		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 @@ import org.junit.jupiter.api.Test;
 
 /**
  * Tests for ClientProperties that failed on Mac OS X Java platform.
- * 
+ *
  * @author Martijn Dashorst
  */
 class ClientPropertiesTest
@@ -149,4 +149,32 @@ class ClientPropertiesTest
 
 		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());
+	}
 }