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?"

MikeJ
  • 14,430
  • 21
  • 71
  • 87

4 Answers4

5

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
  • 38,186
  • 16
  • 91
  • 120
3

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
  • 38,186
  • 16
  • 91
  • 120
  • Very clever. When I get to the office later today I will to set this up. I think you might have something here. – MikeJ May 21 '09 at 06:20
2

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.

Getting the location from an IP address

Community
  • 1
  • 1
alex
  • 479,566
  • 201
  • 878
  • 984
  • it might be a good first approximation. How would you correlate ip to geolocation/timezone? – MikeJ May 19 '09 at 02:24
  • 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 May 19 '09 at 02:27
  • @Matthew - of course, however with user agents lacking JS - such as mobile handsets, its better than nothing... – MikeJ May 19 '09 at 03:44
1

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
  • 12,343
  • 9
  • 37
  • 38
  • 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 May 19 '09 at 02:56
  • An offset including DST, whether it is in or not in effect, is perfect for local-to-utc-to-local back and forth calculations. In that regard, please note that some user agents may specify the local offset to UTC which is the inverse of the offset from UTC to local. Unless of course the actual timezone designation is crucial (ie. it is vital for the application that a user is told the correct name of the timezone) – andkrup May 16 '19 at 09:13