How can I determine what time zone a user of an ASP.NET/MonoRail website is in?

So, I am setting a cookie that should expire. However, I want this to work globally. So I need to adjust the expiration date for the custom timezone.

So I need to figure out the user's timezone, server side.

Is there a way to do this in BCL? As is the case to rely on setting CultureInfo.CurrentUICulture correctly (which I believe MonoRail LocalizationFilter does, but haven't checked properly yet) and then figure out what timezone the user is in (and also daylight saving time accounting there too) based on this?

There's System.TimeZoneInfo in .NET 3.5, but little documentation on what identifiers are - be they ISO codes or what.

Or will I rely on something on the client side to use this information like Date.getTimezoneOffset ()? I would prefer to avoid this since, well, the client side is lying.

+1


source to share


3 answers


It's very simple. On the server, enter asp: hidden field and fill it with the current server 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 an offset from GMT. Reverse to execute on the server (e.g. new Date () on hidden field, subtract server time from hidden field value).



+1


source


Aside from IP geolocation (bad idea for reasons others mentioned) or JavaScript (also not very good because it's too ambiguous (due to time zones)), there is no way to get the user's timezone.

Your best option is to let the user set the time zone themselves through some sort of user control panel. This way you know exactly what timezone the user is in and you allow the user to confirm the timezone.

It is anticipated that some geolocation functionality will be added to the browser in the future, but this is far from being used in production.



If you choose to use JavaScript auto-detection, I would recommend these scripts, which scan dates in the user's system to find out when DST occurs and also return GMT offset (even if during 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 good idea, but it works.

+1


source


There is no really good way to define customers' time zone. You could track their IP address and then try to bind a location to it, but this is difficult and error prone (some locators used all AOLs in Virginia)

I would say that it would be better to do it on the client side.

0


source







All Articles