Converting time from different time zones to daylight saving time

I have a process that takes a datetime value with a time zone as a string (data comes from an external system). I need to translate this datetime to what time would be in the timezone of local computers.

Code example:

string cetId = "Central European Standard Time"; if (timeZone == "CET") { TimeZoneInfo cetZone = TimeZoneInfo.FindSystemTimeZoneById(cetId); returnDateTime = TimeZoneInfo.ConvertTime(statusDateTime, cetZone, TimeZoneInfo.Local); } else if (timeZone == "CEST") { TimeZoneInfo cestZone = TimeZoneInfo.FindSystemTimeZoneById(cetId); returnDateTime = TimeZoneInfo.ConvertTime(statusDateTime, cestZone, TimeZoneInfo.Local); } 

Do I need to do something specific if CEST (Central European Summer Time) instead of CET (Central European Time) or does the .net TimeZoneInfo object process this script?

+6
source share
3 answers

You must be fine.

You tell ConvertTime what both time zones are (source and target).

Do you have a specific problem with this or are you just asking for confirmation?

+1
source

From TimeZoneInfo.ConvertTime ()

 The value of the Kind property of the dateTime parameter must correspond to the sourceTimeZone parameter, as the following table shows. 

I just want to add that you need to monitor the "Type" property of your StatusDateTime. In your case, it should be "Unspecified". Check the Notes section.

0
source

I created the library some time ago to encapsulate these kinds of transformations:

https://github.com/b9chris/TimeZoneInfoLib.Net/blob/master/TimeZoneInfoLib/TimeZone/UtcTimeZone.cs

You might find it helpful, or you can simply select it to double check your code. One of the things he encapsulates is @Brian caveat - so it has explicit method names related to the .Kind property, which throw useful exceptions if .Kind is wrong. It also takes a little effort to find useful / common / day names and abbreviations:

https://github.com/b9chris/TimeZoneInfoLib.Net/blob/master/TimeZoneInfoLib/TimeZone/TimeZoneShortNameMap.cs

0
source

All Articles