Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting google time zone to .net timezone

Tags:

c#

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 !

like image 983
Bob Swager Avatar asked Apr 21 '17 13:04

Bob Swager


People also ask

How do I convert UTC to C#?

To convert the time in a non-local time zone to UTC, use the TimeZoneInfo. ConvertTimeToUtc(DateTime, TimeZoneInfo) method. To convert a time whose offset from UTC is known, use the ToUniversalTime method.

How do I convert a date time field for any TimeZone?

Calendar calendar = new GregorianCalendar(TimeZone. getTimeZone("GMT")); DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z"); formatter. setTimeZone(TimeZone.


1 Answers

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...

like image 190
Jon Skeet Avatar answered Sep 22 '22 05:09

Jon Skeet