views:

1168

answers:

4

Is there a way to determine the timezone for a user agent without javasript?

normally, i would execute this snippet of javascript:

<script type="text/javascript" language="JavaScript">
 var offset = new Date();
 document.write('<input type="hidden" id="clientTzOffset" 
 name="clientTzOffset" value="' + offset.getTimezoneOffset() + '"/>');
</script>

However, for a lot of mobiles and clients, JS is either not existant or turned off. Is there something clever I can do or am I reduced to "tell me where you are and what timzeone you are in?"

+2  A: 

Maybe with a server side language you could do an IP lookup, and then determine from their country what timezone they could be in.

I'd imagine this could be problematic, though.

If going down this road, this question (and answers) may be of assistance.

http://stackoverflow.com/questions/409999/getting-location-from-ip-address

alex
it might be a good first approximation. How would you correlate ip to geolocation/timezone?
MikeJ
Good GeoIP databases can definitely give you a time zone (see http://www.siafoo.net/article/53). However, this will be less accurate than JS.
Matthew Flaschen
@Matthew - of course, however with user agents lacking JS - such as mobile handsets, its better than nothing...
MikeJ
+5  A: 

Use both. Have javascript set the default value of a list or text field. The percentage of people without javascript is so small the burden is tiny.

I was going to say "Use the request Date: header" but that's a response header only it seems.

SpliFF
+1  A: 

I've seen a website where instead of asking what timezone the user was in it simply asked "pick the correct time" and it showed them a list of times. It's a subtle difference from "what timezone are you in" but much easier to understand. Unfortunately I can't find the website anymore

codette
Sadly, this is not the same thing. It can be the same time in different time zones due to daylight savings and other considerations.
Matthew Flaschen
+2  A: 

I thought of another solution but it is a little complex. Set up some fake HEAD requests with a max-age: 1 response header to coerce the browser into re-fetching them. You should then receive an if-modified-since header from any modern browser like so:

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

Just be careful not to send any Last-Modified headers with the first response because

To get best results when sending an If- Modified-Since header field for cache validation, clients are advised to use the exact date string received in a previous Last- Modified header field whenever possible.

Note the "whenever possible" disclaimer. This, and other parts of the header description imply that the client will use its own clock when it doesn't know anything of the servers.

With the right combination of headers this could actually work very well.

EDIT: Tried some tests with FF but couldn't find a valid combination of headers to trigger an if-modified-since in client time. FF only sends the header if it got a last-modified header previously and then it just reflects the value back (even if it isn't a valid date).

SpliFF
Very clever. When I get to the office later today I will to set this up. I think you might have something here.
MikeJ