views:

786

answers:

4

The question is as it sounds - how to retrieve the client user's time zone based on their browser input? If it matters, server side, J2EE is used - but I don't think that really matters in this case. Right now, HTTP is the protocol, it may use HTTPS when it goes live.

A: 

I'm sure this is a duplicate.

The best way is to ask the user. Time Zone is not sent in the HTML headers, by default. Also consider that a user from one time zone may be visiting another. Which time zone should you use?

John Saunders
If it is a duplicate, I couldn't find it on the list of "related questions". There are some questions that are similar, but none asked what I was looking for.
MetroidFan2002
+3  A: 

Seems somebody hacked some javascript to deduce this but yes, the best way is to let the user decide. But you can use the above link to choose the default for the user.

lpfavreau
+1  A: 

You can also use IP geolocation services like ip2location. They sell a database subscription that will return the timezone in which the IP address is located. Of course, this has a price. A Javascript solution would work but does require that the client supports it.

The geolocation will support any client but can provide the wrong result depending on how the IP block was attributed or if the client is using a proxy server located in a different timezone (most likely an anomysing proxy service). The Javascript solution will support a large variety of client but will return the timezone base on what the computer is set to and not the timezone base on the actual location (traveling user don't always change the time on their computer). So there is no single solution that covers them all.

Using one of these two methods to make a guess and then allowing the user to correct it would be the best approach I would think. If you want to go overkill use both and let the user decide which makes most sense.

Pierre-Luc Simard
But, what about proxy servers that mask the origination of the ip address? Wouldn't you only get the timezone of the last proxy server?
MetroidFan2002
I've updated the answer pointing out the fact that proxy server would be an issue but only if they are in a different timezone. Thanks for pointing that one out.
Pierre-Luc Simard
A: 

As stated by Pierre-Luc Simard, which is a very good idea, you can use the IP to deduce that information. Credit goes to him but I'll add another link to a free alternative which is Geolite from Maxmind. There's also a sample code in PHP for the timezones and I'm pretty sure you'll find the equivalent in their Java API.

lpfavreau