通过Web服务发现客户端的时区和文化时区、客户端、发现、文化

2023-09-03 15:16:25 作者:终于到了小时候

我有一个.NET Web服务。

I have a .NET web service.

从互联网用户提供AJAX请求。

It serves AJAX requests from web users.

我只是想知道如何自动获取用户的时区......不是当前的时间偏移,但实际时区 - 样,中部标准时间为-5:00现在,但东部标准时间将是-5 :00,一旦夏令结束。我想区分这些用户。

I would simply like to know how to automatically get the user's timezone... Not current time offset, but the actual timezone - like, Central Standard Time is -5:00 right now, but Eastern Standard Time will be -5:00 once daylight savings is over. I want to differentiate these users.

我也想知道如何让自己的文化设置(EN-US等),这样我就可以使我从Web服务创建DateTime对象和数字,其具体preferences。

I would also like to know how to get their culture settings ("en-US", etc.) so I can render DateTimes and numbers from my web service to their specific preferences.

任何JavaScript或.NET解决方案,将工作。 谢谢你。

Any Javascript or .NET solution will work. Thanks.

注:询问用户将是一个完整的最后一招,因为它是一个Web服务。

Note: Asking the user would be a complete last resort, since it's a web service.

推荐答案

假设你的客户端连接到Web服务从一个浏览器,就可以使用Request.Headers [接受语言]来检测浏览器的文化settings.It是不可能直接从服务器获取的时区,则需要在客户端上一些JavaScript来计算自己的本地时钟与格林尼治标准​​时间之间的时间差。

Assuming your clients connect to the webservice from a browser, you can use Request.Headers["Accept-Language"] to detect the browser's culture settings.It is not possible to get the timezone directly from the server, you need some javascript on the client to compute a time difference between their local clock and GMT.

如果你的web服务diferently消耗的(即不是从一个浏览器),你将需​​要作为参数添加这两条信息到你的Web服务的方法。

If your webservice is consumed diferently (ie. not from a browser), you will need to add these two pieces of information as parameters to your webservice methods.