如何确定(ASP.NET / MonoRail)网站用户位于哪个时区?
所以,我设置了一个应该过期的 cookie。 然而,我希望这种方法能够在全世界范围内发挥作用。 所以我需要根据用户的时区调整到期日期。
所以,我需要在服务器端找出用户的时区。
有没有办法在 BCL 中做到这一点? 例如,依赖 CultureInfo.CurrentUICulture 进行正确设置(我相信 MonoRail LocalizationFilter 会这样做,但尚未正确检查),然后确定用户所在的时区(以及考虑夏令时)也有)基于此?
.NET 3.5 中有 System.TimeZoneInfo,但关于 Id 是什么的文档很少 - 无论它们是 ISO 代码还是什么。
或者我是否要依靠客户端上的某些东西来推送此信息,例如 Date.getTimezoneOffset()? 我宁愿避免这种情况,因为客户端会撒谎。
So, I'm setting a cookie that should expire. However, I want this to work around the world. So I need to adjust my expiry date for the user's timezone.
So, I need to find out the user's timezone, server-side.
Is there a way to do this in the BCL? As in, something like relying on the CultureInfo.CurrentUICulture to be set correctly (which I believe the MonoRail LocalizationFilter does, but have not checked properly yet), and then figuring out which timezone the user is in (as well as accounting for daylight savings there too) based on that?
There's System.TimeZoneInfo in .NET 3.5, but little documentation on what the Ids are - whether they're ISO codes, or what.
Or am I going to rely on something on the client-side to push in this information from for example Date.getTimezoneOffset()? I'd prefer to avoid that, since, well, the client-side lies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这很简单。 在服务器上,放置一个 asp:hidden 字段并用当前服务器的日期/时间填充它。 在客户端上,从客户端上的当前日期(new Date())中减去隐藏字段中的日期/时间,即可得到与 GMT 的偏移量。 在服务器上执行相反的操作(例如隐藏字段中的 new Date(),从隐藏字段值中减去服务器时间)。
It's very simple. On the server, put a asp:hidden field and fill it with the current server's date/time. On the client, subtract the date/time in the hidden field from the current date on the client (new Date()) and you have your offset from GMT. Reverse for doing it on the server (e.g. new Date() in hidden field, subtract server time from hidden field value).
除了 IP 地理定位(由于其他人提到的原因,这是一个坏主意)或 JavaScript(也不是很好,因为它太模糊(由于时区)),没有办法获取用户的时区。
最好的选择是允许用户通过某种用户控制面板自行设置时区。 这样您就可以准确地知道用户所在的时区,并允许用户确认时区。
未来浏览器中应该会添加一些很棒的地理定位功能,但这距离在生产中使用还有很长的路要走。
如果您决定使用 JavaScript 进行自动检测,我会推荐这些脚本,它们扫描用户系统上的日期以找出 DST 发生的时间,并获取 GMT 偏移量(即使在 DST 内)。
http: //www.attackwork.com/BlogEntry/3/Time-zone-detect-and-ignore-Daylight-Saving-Time-DST/Default.aspx
http://www.attackwork.com/ BlogEntry/11/Daylight-Saving-Time-DST-Detect/Default.aspx
仍然不是一个很好的主意,但它确实有效。
Besides IP geolocation (a bad idea for reasons mentioned by others), or JavaScript (also not very good because it's too ambiguous (due to time zones)), there's no way to get the user's time zone.
Your best option is to allow the user to set the time zone themselves through some sort of user control panel. That way you know exactly what time zone the user is in and you allow the user to confirm the time zone.
There is supposed to be some great geolocation features being added into browsers in the future, but that's still a long way off to use in production.
If you decide to go with automatic detection using JavaScript, I'd recommend these scripts, which scan the dates on the user's system to find out when DST occurs and also gets back the GMT offset (even if within DST).
http://www.attackwork.com/BlogEntry/3/Time-zone-detect-and-ignore-Daylight-Saving-Time-DST/Default.aspx
http://www.attackwork.com/BlogEntry/11/Daylight-Saving-Time-DST-Detect/Default.aspx
Still not a very great idea, but it works.
没有真正好的方法来确定客户端的时区。 您可以跟踪他们的 IP 地址,然后尝试为其附加一个位置,但这很棘手且容易出错(有些定位器将所有 AOL 使用都放在弗吉尼亚州)
我想说最好在客户端执行此操作。
There's no really good way to determine a clients time-zone. You could track their IP address, and then tried to attach a location to it, but that tricky and error-prone (Some locators put all AOL uses in Virginia)
I'd say that it would be best to do it on the client side.