Reputation: 14216
How can I get the timezone information for a country?
Upvotes: 4
Views: 10268
Reputation: 9861
Several things to remember when consuming timezone data:
Timezone data is location specific... For whom are you showing timezone information about? The user's? A customer's?
Timezone data also includes this funny thing called Daylight Savings which means your timezone calculations will change based on which dates you are calculating them on.
Timezone data changes VERY FREQUENTLY For example, the United States changed when Daylight Savings would start and end about 2 years ago... and Australia has passed a law each of the past 2 years determining when it will start and end in each of their states.
My recommendation: Effective date your timezone calculation tables... because these things change, you need to be able to accurately calculate the time at any point in time, you need to know what the timezone standard was at THAT point in time.
Store all times in UTC in your database
Upvotes: 1
Reputation: 14543
As others have noted, there really is no good way to do this even more so if you try and guess based upon just a country as there is a pretty good chance that a given country might be in more than one timezone.
Since ASP.NET is in the tag list I'm going to go out on a limb and guess you are writing a web application of some sort. In this situation the best bet, and possibly the most reliable would be to simply have the user specificity which timezone they are in. Once you know the user's designated timezone you can do pretty much anything you want with it.
The next option is the one that John Saunders referenced in his post and that is to use a service such as EarthTools to look up a location and get the timezone information. The only catch here is that you usually need to be able to get to at least a city and country to filter the results and you have to be careful as there maybe multiple cities in the same country with the same name.
Finally, if you are willing to accept the results might be wrong sometimes, but don't have a good way of allowing the user to indicate where they are, you could try doing a reverse IP look-up to try and guess where the user's computer is at. Then you could take this information to the aforementioned EarthTools to get the timezone information. However, there is really no way of guaranteeing that this is accurate.
One final thing to note, is that if you are inside of a corporate WAN then you might be able to pull off a reverse IP look-up a lot easier as you have access to the layout of the WAN and could just have a short list of where a given IP address is going to be coming from.
Upvotes: 0
Reputation: 161773
Funny you should ask. See A REST Client Library for .NET, Part 1. It refers to a web site and set of web services that return location-based data, including time zone information.
Upvotes: 2
Reputation: 6195
As others have said, this is a moving target. I've found this site to be very useful:
http://www.timeanddate.com/worldclock/
You can grab the UTC offsets from there, and if you're really serious, do some scheduled scraping to stay up to date. I used it several years ago to put a series world times on my company's portal page to show the times in the company's major offices worldwide.
Upvotes: 1
Reputation: 112366
This web site has links to all the usual time zone data. The tz database is a good place to look. In general, you'll need to know a location in the country, not just the country.
Upvotes: 0
Reputation: 1095
This probably does not answer your question but here is a forum discussing something along those lines: get timezone from country name asp.net
Upvotes: 0