如何在 JavaScript 中确定系统的 Olson zoneinfo?
我有兴趣检索用户当前在 JavaScript 中为其操作系统设置的当前区域信息字符串(“美国/洛杉矶”、“欧洲/伦敦”等)。 我已经找到了如何获取以秒为单位的当前偏移量、特定日期的偏移量以及不太精确的时区(PST 等),但没有找到 Olson zoneinfo。 这可能吗?
I'm interested in retrieving the current zoneinfo string ("America/Los Angeles", "Europe/London", etc.) that the user currently has set for their OS in JavaScript. I've found how to get the current offset in seconds, offsets at specific dates, and less exact time zones (PST, etc.) but not the Olson zoneinfo. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
几个月前我研究了这个问题,结果是这样的: http: //site.pageloom.com/automatic-timezone-detection-with-javascript。 该脚本不会为您提供最精确的结果。 例如,无论您位于中欧时区的哪个位置,它都会返回 Europe/Berlin。
基本上它所做的一切都是检查夏令时转换发生时的已知日期。
希望它会有所帮助。
I worked on this very issue a couple of months back and the result is this: http://site.pageloom.com/automatic-timezone-detection-with-javascript. The script will not give you the most precise result possible. For example, it will return Europe/Berlin regardless of where in the Central European timezone you are.
Basically all it does it check for known dates when daylight saving transitions take place.
Hopefully it will help.
请参阅 fleegix.js 的 date.Date 插件。 您可能可以只破解 您需要的部件:
See fleegix.js's date.Date plugin. You can probably hack out just the parts you need:
没有。 对不起! 您也许可以使用 TimeZone 通过 Java 小程序获取它,但从 JavaScript 中您所拥有的只是与 UTC 的偏移量。 (根据时区名称进行猜测并不可靠,因为这些名称与区域设置相关。您也许可以通过与 UTC 偏移量匹配来检测一些常见情况,但这需要大量工作。)
通常,如果您想要将时区支持到像 Olson 一样细粒度的级别,您将为用户提供明确的可选择时区控制。
Nope. Sorry! You might be able to get it via a Java applet using TimeZone, but from JavaScript all you have is the offset from UTC. (Guessing from the timezone name isn't reliable as the names are locale-dependent. You could detect a few common cases by matching with the UTC offset, perhaps, but it'd be a lot of work.)
Generally if you want to support timezones to a level as fine-grained as Olson you'd give the user an explicit selectable timezone control.
我使用 Jon Nylander 出色的探测器的一些想法制作了一个更简单的脚本(该探测器基于 Josh Fraser 的脚本,http://www .onlineaspect.com )。 Nylander 的脚本(以及 https://github.com/scottwater/jquery.detect_timezone ) 使用歧义列表,DST 开始时间与当地时间相同。 我的脚本只有一个列表,所有日期均采用 UTC 格式,以便在需要时轻松添加或修改时区。
用法:
- 另存为 .js 并包含在 html 标题部分
- 调用 get_timezone_id() 并返回用户的时区,例如。 “Europe/Helsinki”
该脚本可以检测 90 个时区,但如果您需要更多时区,则必须使用 GeoLocation API 或某些服务器端库(geoIP)。 根据目的,还可以考虑添加用户可选择的时区列表。
另请注意,由于我们不知道未来的浏览器和系统如何保留 2011 年的转换数据,因此可能需要在未来的某一天进行一些调整。 (我测试了 1970-80 年的日期,同一系统中不同浏览器的结果差异很大(!))但幸运的是,2011 年已经过去了,世界上几乎所有系统都希望更新其 tz 数据库,并且检测应该是现在很好。
I made a bit simpler script using some ideas of Jon Nylander's awesome detector (which is based on script by Josh Fraser, http://www.onlineaspect.com ). Nylander's script (as well as it's jQuery counterpart in https://github.com/scottwater/jquery.detect_timezone ) uses ambiquities list and DST start times are as local times. My script has only one list and all the dates are in UTC to make it easy to add or modify timezones, if needed.
Usage:
- Save as .js and include in html header section
- Call get_timezone_id() and user's timezone is returned eg. "Europe/Helsinki"
This script can detect 90 timezones, but if you need more, you have to use GeoLocation API or some server side library (geoIP). Depending on purpose, consider also adding user selectable list of timezones.
Also note that because we don't know how future browsers and systems preserve transition data of year 2011, may be some tweaks have to be done some day in the future. (I tested dates on 1970-80 and the results varied greatly across browsers in the same system(!)) But luckily year 2011 has gone, and nearly all the systems in the world have hopefully updated their tz-databases and the detection should be pretty good now.
您必须在 Javascript 中设置区域信息数据的数组或散列,并根据您拾取的时间确定名称。
You'd have to set up an array or hash of the zoneinfo data in Javascript and determine the name based on the time you pick up.