I'm looking to implement a function with a signature something like the following:
bool IsTimeZoneValid(string countryCode, DateTime localTime);
The intention is to determine whether the country has a time zone in which the local time would be valid, given that we know the current UTC time. Let's say, for the sake of argument, that "valid" means that when converted to UTC the time is +/- 30 minutes from what we believe the time is.
For example, say it is currently 03/08/2009 18:25:00 UTC then given the following method call for Australia, it should return true
as this is a valid time in the "Eastern Standard Time" zone:
IsTimeZoneValid("AU", DateTime.Parse("04/08/2009 03:25:00"));
However the following call for France should fail as this is not a valid time in France's time zone.
IsTimeZoneValid("FR", DateTime.Parse("04/08/2009 03:25:00"));
This needs to be accurate, and take into account daylight saving time etc.
.NET 3.5 includes the new TimeZoneInfo
class which can do a lot of the conversion for me if I know what time zones exist in a specific country, but I can't seem to find any built-in lookup for this. Am I missing something, or am I going to have to manually create a table of country to time zone mappings?
To reiterate, my question is: Given a country code, how can I get a list of time zones. Alternatively, is there another way to approach this that I've missed?
By default windows only adds Time Zone info for you local time zone, which may be the cause of the problem.
This class works only for the local time zone and any predefined time zones. If you want to use this for other times zones, you must have the registry settings added to the machine for all time zones needed or create custom time zone info using CreateCustomTimeZone.
http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx?ppud=4 http://msdn.microsoft.com/en-us/library/bb384268.aspx