# Re: Inferring timezone for HTTP headers

**URL:** <https://www.ruby-forum.com/t/re-inferring-timezone-for-http-headers/81683>\
**Category:** Rails\
**Created:** [January 11, 2007, 9:36am UTC](https://www.ruby-forum.com/t/re-inferring-timezone-for-http-headers/81683 "2007-01-11T09:36:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![piet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@piet](https://www.ruby-forum.com/u/piet)\
**Post date:** [January 11, 2007, 9:36am UTC](https://www.ruby-forum.com/t/re-inferring-timezone-for-http-headers/81683/1 "2007-01-11T09:36:00Z")

</div>

You could also do a javascript redirect, sending the browser’s local  
time as a parameter and storing the difference with servertime in a  
session variable.  
And then everytime you need to display a time, add (or subtract) the  
difference.

No example at hand, but I’ve done something like this in the past.

Piet.

* * *

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Hammed M.  
Sent: donderdag 11 januari 2007 5:24  
To: removed\_email\_address@domain.invalid  
Subject: [Rails] Re: Inferring timezone for HTTP headers

You could use javascript to transform the date into the client’s  
timezone. This post on \_why’s blog describes how to go about doing this:  
[http://redhanded.hobix.com/inspect/showingPerfectTime.html](http://redhanded.hobix.com/inspect/showingPerfectTime.html)

Hammed

On 1/11/07, Veera [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

```
Hello,

I am encountering the classic 'hosting timezone

```

different from customer  
timezone’ problem. The hosting timezone is CST, but  
users all over the  
world are visiting the site.

```
I understand I can make the Timezone selection a user

```

preference and use  
TZInfo ([http://tzinfo.rubyforge.org/](http://tzinfo.rubyforge.org/)) to do the math.

```
But, is there anyway to infer the client timezone from

```

the HTTP headers  
or the request object?

```
Thanks,
+Veera.

--
Posted via http://www.ruby-forum.com/.

```

---

<div class="post-metadata">

**Author:** ![piet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@piet](https://www.ruby-forum.com/u/piet)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/re-inferring-timezone-for-http-headers/81683/2 "2007-01-19T16:30:07Z")

</div>

hey,  
this is a tricky problem. What you are probably interested in is  
guessing someone’s ‘locale’, not their timezone. The reason is that  
most locales change timezones once every year; but not all do. Your  
servers current timezone might be CST, but chances are that in a few  
months it will switch to CDT. Same with most of your clients. Then it  
gets even more complicated because when timezones switch is constantly  
changing. The USA, for one, is stipulating a change this year. And to  
make it even more complicated, you have ‘odd’ cases where places like  
Arizona don’t change their timezone at all throughout the year, while  
other states like Indiana have 5 different locales (some areas don’t  
change timezones, others are in easter, some in central, etc). It is  
_complicated!!!_

So, with that in mind, one solution, that as far as I know works for  
almost all cases (see below for disclaimer 😉 is this:

- have the client, in javascript, compute the UTC offset for two dates,  
‘2005-06-30’ and ‘2005-12-30’ (or any date that will always be in one  
timezone and another date that will be in the other)
- using the two offsets, search through all possible locales for that  
which has the same winter and summer UTC offsets.

For example:  
summer\_offset == -21600  
winter\_offset == -20600  
matches the America/Los\_Angeles locale

summer\_offset == -21600  
winter\_offset == -21600  
matches the Pacific/Galapagos locale

summer\_offset == 34200  
winter\_offset == 37800  
matches the ‘Australia/Yancowinna’ locale

this works, but isn’t perfect because it is possible to match multiple  
locales. But you might be able to use a trimmed down list of locales  
that highlight only the major ones. The other catch is that you are  
choosing two dates (in our case ‘2005-06-30’ and ‘2005-12-30’) which  
you assume will always be in one timezone or the other; but what if  
that is not always the case. As far as I know it is _currently_ the  
case, but when timezones switch for different locales is always  
changing, so this solution might not be future proof.

I have my logic so when a user registers for an account, I figure out  
their current timezone. I have two hidden form fields called  
‘summer\_offset’ and ‘winter\_offset’. Then in javascript I do this:  
try {  
$(‘summer\_offset’).value = -1 \* (new Date(Date.UTC(2005, 6, 30, 0, 0,  
0, 0))).getTimezoneOffset() \* 60; // in seconds  
$(‘winter\_offset’).value = -1 \* (new Date(Date.UTC(2005, 12, 30, 0, 0,  
0, 0))).getTimezoneOffset() \* 60; //in seconds  
} catch (e) {}

in my controller, I pass the parameters to this method, which will spit  
out a string containing the locale identifier:

# trying to automatically determin a users time zone

# this is possible, but note that the locale might be off (say the

local is America/Boise, but it chooses America/Denver instead)

# if a timezone is not found, will return nil… you can then set the

default timezone to whatever you want: UTC perhaps?  
def find\_time\_zone(winter\_offset, summer\_offset)  
default\_tz = nil  
return default\_tz if winter\_offset.blank? || summer\_offset.blank?

```
# look at two dates that will, by and large, reside *between* major

```

time changes within a zone.  
# ie, they won’t fall on a day where the time is actually changing  
summer\_date = “2005-06-30”.to\_time  
winter\_date = “2005-12-30”.to\_time  
winter\_offset = winter\_offset.to\_i  
summer\_offset = summer\_offset.to\_i  
# this comes from the client, where they should have something like  
so (for a html client):  
# $(‘summer\_offset’).value = -1 \* (new Date(Date.UTC(2005, 6, 30,  
0, 0, 0, 0))).getTimezoneOffset() \* 60;  
sd\_offset, wd\_offset = nil  
# need to go through each timezone and find a match for the summer  
and winter offsets  
default\_tz = nil  
TZInfo::Timezone.all.reverse.each do |tz|  
# get the summer date and winter date total utc offset for the  
timezone  
# This includes the utc offset plus any change with daylight  
savings  
sd\_offset = tz.period\_for\_local(summer\_date,  
false).utc\_total\_offset  
wd\_offset = tz.period\_for\_local(winter\_date,  
false).utc\_total\_offset  
# at first I thought I had to have this if PLUS another one where  
the == were mixed like:  
# sd\_offset == winter\_offset && wd\_offset == summer\_offest. This  
check is not needed  
# because the summer\_date and winter\_date are aligned between the  
client and the server.  
if sd\_offset == summer\_offset && wd\_offset == winter\_offset  
default\_tz = tz  
break  
end  
end

```
unless sd_offset.blank? || wd_offset.blank?
  #THIS is a _tad_ elitest. Ccan the US zones to try and better

```

match the locale.  
#If a timezone matches in the US, use THAT locale…otherwise use  
the original one  
new\_time\_zone = TZInfo::Timezone.us\_zones.find do |z|  
z.period\_for\_local(summer\_date, false).utc\_total\_offset ==  
sd\_offset &&  
z.period\_for\_local(winter\_date, false).utc\_total\_offset ==  
wd\_offset  
end  
#we can add other ‘cleanup’ loops here if needed…  
default\_tz = new\_time\_zone unless new\_time\_zone.blank?  
end  
default\_tz.blank? ? nil : default\_tz.identifier  
end

its a heavy approach, but it is the most complete that I could come up  
with. Let me know if you think it would work for you.

cheers!  
Adam
