views:

1309

answers:

3

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.

A: 

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.

James Curran
+1  A: 

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).

Robert C. Barth
That doesn't work when Daylight Savings is taking into consideration. Not everywhere observes DST and there are different rules around the world (from what I recall)
Dan Herbert
Huh? Of course it works. You're getting the CURRENT offset to do a current timeshift. It will always work. I'm not saying to save the offset in durable storage, just use it as the value for that page request. I am well aware of DST as the state I live in doesn't do DST.
Robert C. Barth
+1  A: 

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.

Dan Herbert