How accurate is automatic detection of device time

2019-09-02 21:03发布

Was looking at this tutorial here http://pellepim.bitbucket.org/jstz/ on automatic timezone detection using javaScript which detects the device's time zone setting.

I was wondering if anyone had any idea (ballpark is fine) of how accurate this method is of detecting timezones?

Thanks for your time!

2条回答
乱世女痞
2楼-- · 2019-09-02 21:13

I cannot speak to how accurate this specific library (jsTimezoneDetect) is, but assuming that the library is using the native «Date().getTimezoneOffset()» method and simply has a lookup table to return the correspoding timezone...and the lookup table is correct, then this will be a pretty robust way of doing it.

From the Mozilla documentation:

The time-zone offset is the difference, in minutes, between UTC and local time.

查看更多
不美不萌又怎样
3楼-- · 2019-09-02 21:19

From the "limitations" section of the project page:

This script does not do geo-location, nor does it care very much about historical time zones.

So if you are unhappy with the time zone "Europe/Berlin" when the user is in fact in "Europe/Stockholm" - this script is not for you. (They are both identical in modern time).

Also, if it is important to you to know that in Europe/Simferopool (Ukraine) the UTC offset before 1924 was +2.67, sorry, this script will not help you.

Time zones are a screwed up thing, generally speaking, and the scope of this script is to solve problems concerning modern time zones, in this case from 2010 and forward.

You can see several issue where detection is imperfect in the project's issue tracker.

From a design perspective, it's attempting to do the right thing by taking several readings at different points in time using getTimezoneOffset() and comparing them against known values.

The biggest problem is that JavaScript lies. It's API would make you think you could check for the local offset of any date/time. But in reality, it only knows about the current rules for the time zone. Since different time zones change at different points in time, it's very difficult to know whether or not you are working with the "current" time zone rules unless you are specifically checking "now". This was brought up in issue #68, but was decided to be out of scope.

Also consider that if you're only testing the current rules, there are often several different time zones that overlap. See issue #74 and issue #64.

As time zone rules change over time, new versions of jstzDetect will have to be released to compensate. Apps that use it will have to plan on updating as well. So far, there haven't been many updates.

Unfortunately, there is not a perfect way to detect the user's time zone. Some browsers are starting to support the following built-in mechanism:

Intl.DateTimeFormat().resolvedOptions().timeZone

But it's not widely adopted yet, and unfortunately there are bugs there too.

The best advise I can offer is to use jstzDetect as a starting point. It's good at making an initial guess for some other time zone selection control in your application. If you care about accuracy, don't use it for more than that.

See also, this related answer.

查看更多
登录 后发表回答