Friday, February 11, 2011

What is the best way to detect and store the timezone the client of a web app is in?

I have a multi-timezone web application that stores all of the datetime values in UTC in the database, when actions happen on the server, I can easily convert the time into UTC.

However, when a client enters a time or time span, what is the best way to detect and store it?

I am currently doing the following:

  1. Get the value of Date.getTimezoneOffset() (javascript)
  2. Post that to the server-side code via the ICallbackEventHandler on Page.
  3. Store that value in the session
  4. On any subsequent request, calculate the output/input datetime value using the client's timezone.

Regardless of the actual implementation, this seems like an in-elegant solution. Does anyone have a better method?

  • I was doing something very similar, but I now think I prefer to use javascript to convert all times to local on the client side. The server will give all times in UTC in the generated page, and the javascript will convert it once the page loads.

    This eliminates confusion on the server side code, as I always know what time it is (UTC). On the client side I'm using jquery and the each() function to format all the time values at once. I write out each of the times as a unix time in a hidden field to make this easy to process with jquery.

    The only problems I see with this method is that a) I don't have a real good date/time formatting routine yet in javascript, and b) if the user has javascript turned off, then it doesn't work.

    Zachary Yates : This is a neat solution, and one I considered. The only reason I shied away from it was a "countdown timer" scenario, like when a user has X hours remaining to do an action. I sort of felt like that logic should happen on the server, but I guess it would be fine in javascript.
    Mnebuerquo : Anything that's a time limit on the user should definitely be checked against the server. The countdown timer seems related to this question: http://stackoverflow.com/questions/198049/prevent-js-alert-from-pausing-timers It's a similar idea, that client side validation is vulnerable to cheating.
    Zachary Yates : @Mnebuerquo good point. I'll probably have to go with a hybrid method.
    From Mnebuerquo
  • You could use the "header" property of the HttpRequest Object to query the "If-Modified-Since" header sent by the client. This header should contain a date in a format that includes the timezone of the client, like this:

    If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT
    

    a simple substring extraction will give you the timezone code. However, I'm afraid not all browsers are coherent in sending that header, so you should experiment a bit about it.

    regards, Fabrizio

    Zachary Yates : That's a really cool solution! I wish it did work in all browsers, because I wouldn't have to use any javascript! What I see eventually is an HttpModule or a class that derives from Page that would store that variable on the first request. Thanks!
    Mnebuerquo : How does that work exactly? It looks to me like the header has gmt instead of a timezone. Does that mean the client's computer is set to GMT or does that mean that the browser is always using GMT for the request?
    From Hermooz

0 comments:

Post a Comment