5

I'm using this :

string url = string.Format("https://maps.googleapis.com/maps/api/timezone/json?location={0},{1}&timestamp=1374868635&sensor=false", lat, lon);

using (HttpClient cl = new HttpClient())
{
  string json = await cl.GetStringAsync(url).ConfigureAwait(false);
  TimeZoneModel timezone = new JavaScriptSerializer().Deserialize<TimeZoneModel>(json);
}

To get user timezone. This works fine. I want to convert this :

"timeZoneName" : "Central European Summer Time"

To .net timezone ?

In .net Timezone that timezone does not exists.

I tried to get proper timezone with :

TimeZoneInfo tzf = TimeZoneInfo.FindSystemTimeZoneById("Central European Summer Time");

Any idea how to convert google timezone to .net timezone ? Is that possible ?

Thank you guys in advance !

Bob Swager
  • 884
  • 9
  • 25
  • 1
    Use the `timeZoneId`, not the name. –  Apr 21 '17 at 13:47
  • I would make a simple converter method. Take the timezone you're given, split out the data you want (literally with the .split method if you have to), and then put it all back together in the format you desire as a new String as the return value. You wouldn't be able to use all the fancy methods to grab year, hours, minutes, etc, but you could write those if you wanted. – coinbird Apr 21 '17 at 13:49
  • @coinbird: Are you assuming that the Windows name is some permutation of the name returned in the Google Maps API? I would definitely *not* assume that. – Jon Skeet Apr 21 '17 at 13:51
  • 2
    http://stackoverflow.com/questions/17348807/how-to-translate-between-windows-and-iana-time-zones – Ashiquzzaman Apr 21 '17 at 13:52
  • @JonSkeet why would that matter? It doesn't matter how it's returned. OP can organize the result he creates however he wants. – coinbird Apr 21 '17 at 13:55
  • @Ashiquzzaman Thank you. :) – Bob Swager Apr 21 '17 at 13:58
  • @coinbird: You're assuming all the relevant information is present in what's returned. What if it isn't? What if the Windows zone ID is "XYZ" and the term "XYZ" doesn't appear anywhere within the API response? – Jon Skeet Apr 21 '17 at 15:30
  • That's impossible. With every lat/long request to google, gets valid timezone ID. – Bob Swager Apr 24 '17 at 07:05

2 Answers2

5

Instead of using timeZoneName, I'd suggest using timeZoneId, which will be an IANA ID such as Europe/London.

You then have various options:

  • Find a mapping from that to Windows time zone IDs yourself, e.g. with TimeZoneConverter
  • Use Noda Time and map the time zone to a Windows time zone using TzdbDateTimeZoneSource, e.g. with TzdbDateTimeZoneSource.WindowsMapping. See this answer for sample code.
  • Use Noda Time and don't map it to a Windows time zone - just use the Noda Time DateTimeZone instead, and use Noda Time throughout your app, for a better date/time experience in general

Obviously I favour the last option, but I'm biased as the main author of Noda Time...

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    I used this package for visual studio : https://github.com/mj1856/TimeZoneConverter. Thank you anyway. Problem solved :) – Bob Swager Apr 21 '17 at 13:59
  • @BobSwager: Will refer to that in the answer. Still, if you're doing any significant date/time work, I'd strongly encourage you to look into Noda Time. – Jon Skeet Apr 21 '17 at 15:28
  • If using TimeZoneConverter, this is the snippet you're looking for to get the timezoneinfo object: TimeZoneInfo.FindSystemTimeZoneById(TZConvert.IanaToWindows("Europe/London")) – Taran Aug 02 '18 at 20:57
0

" Find a mapping from that to Windows time zone IDs yourself, e.g. with TimeZoneConverter "

The example usage from the docs:

string tz = TZConvert.IanaToWindows("America/New_York");

// Result:  "Eastern Standard Time"

https://github.com/mattjohnsonpint/TimeZoneConverter