5

I have a list of cities (New York, London, etc . .) and I need to do some timezone conversion but i see most of the APIs require that you know the time zone name (such as Eastern Standard Time, Tokyo Standard Time, etc).

Is there anyway to convert the city name into its appropriate TimeZone name ? (pass in New York and it returns "Eastern Standard Time")

leora
  • 188,729
  • 360
  • 878
  • 1,366
  • You can query this webservice: http://www.earthtools.org/webservices.htm#timezone with a lat/long and it will return XML which includes its timezone – Hunter McMillen Mar 02 '13 at 06:21
  • 1
    Be aware that all of the responses given will get you an IANA/Olson timezone identifier. These are not compatible with the `TimeZoneInfo` classes in .net. You will need a third-party library to work with them, such as [NodaTime](https://code.google.com/p/noda-time/) – Matt Johnson-Pint Mar 02 '13 at 19:05
  • thanks Matt. I wound up having to use Nodatime to work in Olson time format but agree with your point that mapping back to Windows Time Zone names is error prone. Lots of SOF questions with answers as large dictionaries. This page seems to be the best source of data that i found: http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/zone_tzid.html – leora Mar 05 '13 at 05:53

5 Answers5

7

Not aware of anything like that built into C# but you can use Google Maps API for that:

1) Get the long/lat of the city: http://maps.googleapis.com/maps/api/geocode/json?address=New%20York,%20CA&sensor=false

2) Now use those long/lat to get the timezone: https://maps.googleapis.com/maps/api/timezone/json?location=43.2994285,-74.21793260000001&timestamp=1362209227&sensor=false

Example Code:

   public TimeZoneResponse ConvertCityToTimeZoneName(string location)
   {
       TimeZoneResponse response = new TimeZoneResponse();
       var plusName = location.Replace(" ", "+");
       var address = "http://maps.google.com/maps/api/geocode/json?address=" + plusName + "&sensor=false";
       var result = new System.Net.WebClient().DownloadString(address);
       var latLongResult = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(result);

       if (latLongResult.status == "OK")
       {
           var timeZoneRespontimeZoneRequest = "https://maps.googleapis.com/maps/api/timezone/json?location=" + latLongResult.results[0].geometry.location.lat + "," + latLongResult.results[0].geometry.location.lng + "&timestamp=1362209227&sensor=false";
           var timeZoneResponseString = new System.Net.WebClient().DownloadString(timeZoneRespontimeZoneRequest);
           var timeZoneResult = JsonConvert.DeserializeObject<TimeZoneResult>(timeZoneResponseString);

           if (timeZoneResult.status == "OK")
           {

               response.TimeZoneName = timeZoneResult.timeZoneName;
               response.Success = true;
               return response;
           }
       }
       return response;
   }
leora
  • 188,729
  • 360
  • 878
  • 1,366
jesal
  • 7,852
  • 6
  • 50
  • 56
  • 2
    i have added the working code to your answer but to clarify this code above gives me Olson Time. i still needed to use another mapping to convert from Olson time to Windows Time Zone – leora Mar 05 '13 at 05:52
2

You could use the Free time Zone Database which is available under the Creative Commons Attribution 3.0 License. You can find information about it here:

http://timezonedb.com/

You could use the SQL one as-is, or you could pull in the sql database into some classes in your application...

Jeff Halverson
  • 253
  • 1
  • 8
2

EarthTools.org has a free webservice you can query by lat/long like:

http://www.earthtools.org/timezone/<latitude>/<longitude>

Example for New York:

http://www.earthtools.org/timezone-1.1/40.71417/-74.00639

But no, I'm not aware of any baked-in class that does this.

JerKimball
  • 16,584
  • 3
  • 43
  • 55
1

Using C# TimeZoneInfo you can do this

TimeZoneInfo.GetSystemTimeZones()
            .Where(k=>k.DisplayName.Substring(k.DisplayName.IndexOf(')')+2).ToLower().IndexOf("tokyo") >= 0)
            .ToList()

Explanation:

I used the display name to determin the city foreach timezone which comes in the following format for example "(UTC+02:00) Beirut". then take the index ")" + 2 and take the next string without the leading space, then do the query.

Will return the matching timezones by city.

Ali Kleit
  • 3,069
  • 2
  • 23
  • 39
0

The closest thing you can get in .net framework is TimeZonInfo.GetSystemTimeZones().

And please be aware that there is not one to one mapping in your case, for example, Sydney, one in Australia, and the other in Canada.

ZZZ
  • 2,752
  • 2
  • 25
  • 37