Problem/Motivation
I noticed that while my local time-zone is Europe/Amsterdam
Drupal auto-selects the Europe/Paris time-zone.
While technically correct I started digging into why it would not use the Systems time-zone.
I discovered that there is a ECMAScript® Internationalization API that would allow a better timezone selection based on the system or browser reported time-zone.
Proposed resolution
Implement a patch to the core/misc/timezone.es6.js library to first try and auto-detect the time-zone as it should be reported by Intl.DateTimeFormat().resolvedOptions().timeZone;, if unsuccessful or a failure fall back on the already written detection algorithm.
This leads to more accurate results and less processing to find out the users Time-zone.
Remaining tasks
Review is needed of this solution as provided in the patch.
User interface changes
No UI changes, the UX is improved by faster Javascript execution when possible.
API changes
No changes.
Data model changes
The Data model is unaffected.
Release notes snippet
Javascript Timezone default detection improved with ECMAScript Internationalization API implementation
Original report by sysosmaster
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | core-js-timezonedetect-3016427-22.patch | 1.39 KB | nod_ |
| #20 | default_timezone-3016427-20.patch | 8.38 KB | kunalgautam |
| #18 | 3016427-18.patch | 8.4 KB | neslee canil pinto |
| #2 | Default_timezone_selection_incorrect_3016427_1.patch | 8.4 KB | sysosmaster |
Comments
Comment #2
sysosmaster commentedAttached path file.
Comment #3
andypostComment #5
tatarbjTagging it for DrupalCamp Belarus - it might need a reroll, but mainly review and test.
https://twitter.com/LKvBuel/status/1127858724205408257
Comment #6
dmitriy.sukhodolsky commentedI tested and it works as expected:
* Drupal shows unrelated timezone during clear installation.
* Drupal shows timezone which was set in my OS settings after I applied the patch.
I did not face any issues or error messages during testing.
Comment #7
sysosmaster commentedSince @dmitriysukhodolsky reviewed this I will now consider this RTBC and mark it as such.
Comment #8
alexpott@sysosmaster thanks for the fix. This code does look better than HEAD and in fact looking at https://caniuse.com/#search=Date vs https://caniuse.com/#search=Intl you can see your proposal has more support so that's great.
However, by convention we don't rtbc our own patches after a single review that's basically "it works". There are other considerations that need to made - for example does this have test coverage and can we provide better automated test coverage with Drupal 8's Javascript tests.
Comment #9
sysosmaster commentedI see, I agree that I jumped ahead a bit with the RTBC. (My main goal was to get this moving, not to jump the queue)
as for testing. I fail to see how you would want to test something as fundamental as detection of timezone in a JavaScript test suite.
I would need a full system test to do see since it involves changing the timezone settings on the machine itself. (which is I believe out of scope for a JavaScript test framework)
Any test that I can think of within a test framework would basically either duplicate this code. Or must be running on a known per-defined timezone. (and could only test that Timezone). If that timezone is one where it does not show a difference between the old and new techniques (like testing this in Germany which would resolve in both cases to Berlin/CE(s)T)
For most other JavaScript I agree it needs to have test. but we have to ask ourselves at what point are we still testing the JavaScript, and at what point are we testing the JavaScript implementation (a.k.a. the JavaScript engine)
This patch also does not remove the old style of detection either. it only utilizes the Browsers knowledge about the environment it is in through the internationalization API.
TL;DR
-because of the small nature and IMHO untestability of this change after the first rreview Iset this to RTBC. (this was incorrect of me I will not do this again)
- This javascript uses fundamental API's of browsers. testing them IMHO is redundent. except if we can controle the Timezone what the test machine is in.
Comment #10
alexpottWhen is
autoDetectedTimezone.lengthsmaller than 2?Are we 100% sure that the ECMAScript Internationalization API will definitely return a value that's supported by PHP? Timezones are a bit complicated. For example, timezones get deprecated see https://www.php.net/manual/en/timezones.others.php
So there's a risk that your browser and PHP are out of sync. Imo we need to ensure that the value is in the select list and if it is not then we need to fallback to the old behaviour.
Intl.DateTimeFormat().resolvedOptions().timeZoneper se but we should test that the logic in Drupal.behaviors.setTimezone() works as expected. And if we don't have an existing automated test then this change runs the risk of breaking it without us knowing.timezone-detectbutjs-timezone-detectas this class exists purely the javascript to find the select element. (Note this change is out-scope for this issue).Comment #11
alexpottWe need to change code for #10.3 so setting appropriate status.
Comment #12
sysosmaster commentedBroken Timezone Detection (think really old Javascript Engineds / Some NodeJS engines)
Its basically to catch the defunct timezone awnser of '1' or alike.
Anser: Yes, although we have to assume that both Unicode and PHP will keep there defenitions uptodate with ther eupstream source of IANA. (I say we can assume this based on importance and past compliance efferts by both unicode and PHP.) :
Source:ECMAScript Internationalization API Specification
To be clear:
(I did a API / Defenition / Code / Specification lookup and check to validate this is all true. feel free to double check it)
Yes there is...
however this requires the Operating system to be out of sync with the Timezone database
(if you use NTP you are required to keep this up to date).
Most OS's have an update mechanism for this.
If this Database is out-of-date the system will also have edge case errors with things like Certificate validation. (So I consider those systems edge case and out of scope for consideration in this issue)
I'll investigate if I can write a test for this, If I can I will add it asap.
Comment #13
alexpott@sysosmaster note is we only set a value that's in the list of allowed values already we ensure we're only setting to a value supported by the PHP on the server so if we change the patch around like i suggest then that is covered. We can not assume that browser manufactures and PHP and especially the version PHP on the server all move in sync.
Comment #14
sysosmaster commentedHow old version. Do you want to support. Your talking about browsers and php versions that are more than 5 years out of date.
TimeZones are changed / added / removed but that’s a slow process and often the timezonedb all ready has them long before there I acted.
We could add some detection and instructions for updating the php timezone definitions as shown here l timezone-version-get
As for browsers being out of date. That would result in an older name Timezone name being send and php will recognize it. (It’s list is bigger than IANA’s and include most of the obsolete definitions only archaic ones are not always there as listed on the php timezones page.
I’ll work on a test function for dB out of date check.
Comment #15
bannss1 commentedThis issue is still persistent in the user register form after applying the patch.
Drupal 8.7.3
Comment #17
handkerchiefAny news on this?
Comment #18
neslee canil pintoComment #20
kunalgautam commentedThis is the patch for Drupal 9.1.
Comment #21
nod_resolvedOptions is a instance method, not a static one.
const autoDetectedTimeZone = (new Intl.DateTimeFormat()).resolvedOptions.timeZone;I'd reverse the if, to avoid splitting the new code all over the place, and probably return early so that we don't have to touch the indentation of existing code.
To be extra sure, we could always check if the value autodetected with JS exists in the select before setting it, and fallback to the old way of doing things.
Comment #22
nod_Patch is more readable than interdiff so leaving it out.
Comment #23
nod_Sorry was a bit too fast, didn't see the novice tag, should have waited a bit more before sending a patch.
Comment #25
nod_credit from duplicate issue
Comment #26
justafishCool! 👍
Comment #27
alexpottTested locally. This works and save a request to the server during interactive install and also account creation - plus it's more accurate. Given that the effect is the same I don't think this change needs a CR.
Also while the timezone select in the summary is valid it is not correct to reclassifying this as a bug and backporting.
Committed and pushed 13d0584f61 to 9.1.x and 98bbe51036 to 9.0.x. Thanks!
Comment #30
alexpott