This is an automated email from the ASF dual-hosted git repository. solomax pushed a commit to branch WICKET-6781-js-timezone in repository https://gitbox.apache.org/repos/asf/wicket.git
commit 2044691aa5bf4aaed8051f423c2154732173df34 Author: Maxim Solodovnik <[email protected]> AuthorDate: Tue May 5 23:54:19 2020 +0700 [WICKET-6781] time zone is determined on client side --- .../markup/html/pages/wicket-browser-info.js | 5 ++ .../wicket/protocol/http/ClientProperties.java | 66 ++++++++++++++-------- .../wicket/protocol/http/ClientPropertiesTest.java | 32 ++++++++++- 3 files changed, 78 insertions(+), 25 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..10b0bcc 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 @@ -58,6 +58,11 @@ info.screenHeight = window.screen.height; info.screenColorDepth = window.screen.colorDepth; } + if (Intl && typeof(Intl.DateTimeFormat) === 'function' && + typeof(Intl.DateTimeFormat().resolvedOptions) === 'function') { + 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 5ca4d02..f2e5740 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) */ @@ -82,6 +82,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; @@ -217,11 +218,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(); @@ -363,7 +372,7 @@ public class ClientProperties implements IClusterable /** * Flag indicating support of JavaScript in the browser. - * + * * @return True if JavaScript is enabled */ public boolean isJavaScriptEnabled() { @@ -373,7 +382,7 @@ public class ClientProperties implements IClusterable /** * Flag indicating that the browser is a derivative of the Microsoft Internet Explorer browser * platform. - * + * * @return True if a derivative of the Microsoft Internet Explorer browser platform. * @deprecated Users are recommended to use third party library for parsing the user agent string. * @see <a href="https://cwiki.apache.org/confluence/display/WICKET/Migration+to+Wicket+8.0#MigrationtoWicket8.0-Useragentdetection">Wicket 8 migration entry</a> @@ -386,7 +395,7 @@ public class ClientProperties implements IClusterable /** * Flag indicating that the browser is a derivative of the KDE Konqueror browser platform. - * + * * @return True if a derivative of the KDE Konqueror browser platform. * @deprecated Users are recommended to use third party library for parsing the user agent string. * @see <a href="https://cwiki.apache.org/confluence/display/WICKET/Migration+to+Wicket+8.0#MigrationtoWicket8.0-Useragentdetection">Wicket 8 migration entry</a> @@ -399,7 +408,7 @@ public class ClientProperties implements IClusterable /** * Flag indicating that the browser is a derivative of the Mozilla 1.0-1.8+ browser platform. - * + * * @return True if a derivative of the Mozilla 1.0-1.8+ browser platform. * @deprecated Users are recommended to use third party library for parsing the user agent string. * @see <a href="https://cwiki.apache.org/confluence/display/WICKET/Migration+to+Wicket+8.0#MigrationtoWicket8.0-Useragentdetection">Wicket 8 migration entry</a> @@ -413,7 +422,7 @@ public class ClientProperties implements IClusterable /** * Flag indicating that the browser is a derivative of the Mozilla Firefox 1.0+ browser * platform. - * + * * @return True if a derivative of the Mozilla Firefox 1.0+ browser platform. * @deprecated Users are recommended to use third party library for parsing the user agent string. * @see <a href="https://cwiki.apache.org/confluence/display/WICKET/Migration+to+Wicket+8.0#MigrationtoWicket8.0-Useragentdetection">Wicket 8 migration entry</a> @@ -426,7 +435,7 @@ public class ClientProperties implements IClusterable /** * Flag indicating that the browser is a derivative of the Opera browser platform. - * + * * @return True if a derivative of the Opera browser platform. * @deprecated Users are recommended to use third party library for parsing the user agent string. * @see <a href="https://cwiki.apache.org/confluence/display/WICKET/Migration+to+Wicket+8.0#MigrationtoWicket8.0-Useragentdetection">Wicket 8 migration entry</a> @@ -439,7 +448,7 @@ public class ClientProperties implements IClusterable /** * Flag indicating that the browser is a derivative of the Apple Safari browser platform. - * + * * @return True if a derivative of the Apple Safari browser platform. * @deprecated Users are recommended to use third party library for parsing the user agent string. * @see <a href="https://cwiki.apache.org/confluence/display/WICKET/Migration+to+Wicket+8.0#MigrationtoWicket8.0-Useragentdetection">Wicket 8 migration entry</a> @@ -452,7 +461,7 @@ public class ClientProperties implements IClusterable /** * Flag indicating that the browser is a derivative of the Chrome browser platform. - * + * * @return True if a derivative of the Chrome browser platform. * @deprecated Users are recommended to use third party library for parsing the user agent string. * @see <a href="https://cwiki.apache.org/confluence/display/WICKET/Migration+to+Wicket+8.0#MigrationtoWicket8.0-Useragentdetection">Wicket 8 migration entry</a> @@ -476,8 +485,8 @@ public class ClientProperties implements IClusterable } /** - * - * + * + * * @return The client's navigator.cookieEnabled property. */ public boolean isNavigatorCookieEnabled() @@ -510,7 +519,7 @@ public class ClientProperties implements IClusterable /** * Flag indicating that the browser is a derivative of the Microsoft Internet Explorer browser * platform. - * + * * @param browserInternetExplorer * True if a derivative of the Microsoft Internet Explorer browser platform. * @deprecated Users are recommended to use third party library for parsing the user agent string. @@ -524,7 +533,7 @@ public class ClientProperties implements IClusterable /** * Flag indicating that the browser is a derivative of the KDE Konqueror browser platform. - * + * * @param browserKonqueror * True if a derivative of the KDE Konqueror browser platform. * @deprecated Users are recommended to use third party library for parsing the user agent string. @@ -538,7 +547,7 @@ public class ClientProperties implements IClusterable /** * Flag indicating that the browser is a derivative of the Mozilla 1.0-1.8+ browser platform. - * + * * @param browserMozilla * True if a derivative of the Mozilla 1.0-1.8+ browser platform. * @deprecated Users are recommended to use third party library for parsing the user agent string. @@ -553,7 +562,7 @@ public class ClientProperties implements IClusterable /** * Flag indicating that the browser is a derivative of the Mozilla Firefox 1.0+ browser * platform. - * + * * @param browserMozillaFirefox * True if a derivative of the Mozilla Firefox 1.0+ browser platform. * @deprecated Users are recommended to use third party library for parsing the user agent string. @@ -567,7 +576,7 @@ public class ClientProperties implements IClusterable /** * Flag indicating that the browser is a derivative of the Opera browser platform. - * + * * @param browserOpera * True if a derivative of the Opera browser platform. * @deprecated Users are recommended to use third party library for parsing the user agent string. @@ -581,7 +590,7 @@ public class ClientProperties implements IClusterable /** * Flag indicating that the browser is a derivative of the Apple Safari browser platform. - * + * * @param browserSafari * True if a derivative of the Apple Safari browser platform. * @deprecated Users are recommended to use third party library for parsing the user agent string. @@ -595,7 +604,7 @@ public class ClientProperties implements IClusterable /** * Flag indicating that the browser is a derivative of the Chrome browser platform. - * + * * @param browserChrome * True if a derivative of the Chrome browser platform. * @deprecated Users are recommended to use third party library for parsing the user agent string. @@ -772,7 +781,7 @@ public class ClientProperties implements IClusterable /** * Sets time zone. - * + * * @param timeZone */ public void setTimeZone(TimeZone timeZone) @@ -799,6 +808,14 @@ public class ClientProperties implements IClusterable } /** + * @param jsTimeZone + */ + public void setJsTimeZone(String jsTimeZone) + { + this.jsTimeZone = jsTimeZone; + } + + /** * @param javaScriptEnabled * is JavaScript supported in the browser */ @@ -865,7 +882,7 @@ public class ClientProperties implements IClusterable /** * Read parameters. - * + * * @param parameters * parameters sent from browser */ @@ -884,6 +901,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 ddff8ee..036ff3b 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 @@ -23,7 +23,7 @@ import org.junit.Test; /** * Tests for ClientProperties that failed on Mac OS X Java platform. - * + * * @author Martijn Dashorst */ public class ClientPropertiesTest extends Assert @@ -146,4 +146,34 @@ public class ClientPropertiesTest extends Assert assertEquals(TimeZone.getTimeZone("AET"), props.getTimeZone()); } + + /** + * WICKET-6781 + * + * jsTimeZone "positive" test + */ + @Test + public void timezoneJsPositive() + { + ClientProperties props = new ClientProperties(); + props.setJsTimeZone("Asia/Novosibirsk"); + + assertEquals(TimeZone.getTimeZone("Asia/Novosibirsk"), props.getTimeZone()); + } + + /** + * WICKET-6781 + * + * jsTimeZone "negative" test + */ + @Test + public void timezoneJsNegative() + { + ClientProperties props = new ClientProperties(); + props.setUtcOffset("11"); + props.setUtcDSTOffset("10"); + props.setJsTimeZone("aaa"); + + assertEquals(TimeZone.getTimeZone("AET"), props.getTimeZone()); + } }
