views:

1111

answers:

4

When users register with our app, we are able to infer their zip code when we validate them against a national database. What would be the best way to determine a good potential guess of their time zone from this zip code?

We are trying to minimize the amount of data we explicitly have to ask them for. They will be able to manually set the time zone later if our best guess is wrong. I realize zip codes won't help with figuring out the time zone outside the US, but in that case we'd have to manually ask anyway, and we deal predominantly with the US regardless.

I've found a lot of zip code databases, and so far only a few contain time zone information, but those that do are not free, such as this one. If it's absolutely necessary to pay a subscription to a service in order to do this, then it will not be worth it and we will just have to ask users explicitly.

Although language isn't particularly relevant as I can probably convert things however needed, we're using PHP and MySQL.

+6  A: 

Most states are in exactly one time zone (though there are a few exceptions). Most zip codes do not cross state boundaries (though there are a few exceptions).

You could quickly come up with your own list of time zones per zip by combining those facts.

Here's a list of zip code ranges per state, and a list of states per time zone.

You can see the boundaries of zip codes and compare to the timezone map using this link, or Google Earth, to map zips to time zones for the states that are split by a time zone line.

The majority of non-US countries you are dealing with are probably in exactly one time zone. You may want to look at where your top-N non-US visitors come from and just lookup their time zone.

Eric J.
Wow, what are the exceptions?
Hamish Grubijan
@Hamish: Exceptions for states in multiple timezones you can see in the Wikipedia link (states per time zone). My colleague is mapping ZIPs to states as we speak and came across a few ZIPs that cross state lines. One example is 42223.
Eric J.
From what i know, Montana, ORegon, Idaho, dakotas, nebraska, kansas, texas, and some of the mid eastern states
dassouki
Jon-Eric
+4  A: 

If you want, you can also get a feel for the timezone by asking the browser Josh Fraser has a nice write up on it here

var rightNow = new Date();
var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
var temp = jan1.toGMTString();
var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);

The second thing that you need to know is whether the location observes daylight savings time (DST) or not. Since DST is always observed during the summer, we can compare the time offset between two dates in January, to the time offset between two dates in June. If the offsets are different, then we know that the location observes DST. If the offsets are the same, then we know that the location DOES NOT observe DST.

var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
temp = june1.toGMTString();
var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
var dst;
if (std_time_offset == daylight_time_offset) {
    dst = "0"; // daylight savings time is NOT observed
} else {
    dst = "1"; // daylight savings time is observed
}

All credit for this goes to Josh Fraser.

This might help you with customers outside the US, and it might complement your zip approach.

Here is a SO questions that touch on getting the timezone from javascript: 1

Development 4.0
A: 

I just found a free zip database that includes time offset and participation in DST. I do like Erik J's answer, as it would help me choose the actual time zone as opposed to just the offset (because you never can be completely sure on the rules), but I think I might start with this, and have it try to find the best time zone match based on offset/dst configuration. I think I may try to set up a simple version of Development 4.0's answer to check against what I get from the zip info as a sanity test. It's definitely not as simple as I'd hope, but a combination should get me at least 90% sure of a user's time zone.

Doug Kavendek
@Doug: Anything that relates to GeoCoding will never be 100% correct, all the more so when you're only acting at the ZIP level. Still, if you can be correct 90%+ of the time, it will probably be a benefit to your users. Worst you're likely to be off is one hour.
Eric J.
@Doug: watch out, free zip databases are almost always obsolete, since the postal service changes that file monthly. also see http://semaphorecorp.com/cgi/zip5.html
joe snyder
That link brings up some good points, but since I'm just using this to make a guess at the general region (and allowing for corrections), I shouldn't have to worry too much. Unless a zip code can straddle multiple timezones -- but in that case, it's already an edge case, and since we will never ask for user's addresses we wouldn't be able to use those to further validate. But indeed, it probably would be useful for us to actually subscribe to one before things go live, to keep up-to-date. The free one is at least good for testing.
Doug Kavendek