Is there a common TimeZoneInfo for Central Europe?

Is there a common TimeZoneInfo for Central Europe that takes into account both CET and CEST in one?

I have an application that does the following:

TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time"); DateTimeOffset dto = new DateTimeOffset(someDate, tzi.BaseUtcOffset); var utcDate = dto.ToUniversalTime().DateTime; 

The problem is that this returns the wrong utcDate, because BaseUtcOffset is +1 instead of +2. It seems that CET also has a DST, and depending on the time of year it is +1 or +2.

+8
date c # datetime
source share
2 answers

First, I would like to welcome mgnoonan's answer to using Noda Time :) But if you feel less adventurous ...

You are already using the correct time zone - but you should not use BaseUtcOffset , which is not documented for DST

Gets the time difference between the standard time of the current time zone and coordinated universal time (UTC).

It is impossible to accept DST unless you provide it with a DateTime to get the offset for :)

Assuming someDate is a DateTime , you can use:

 DateTimeOffset dto = new DateTimeOffset(someDate, tzi.GetUtcOffset(someDate)); 

Or just ConvertTimeToUtc :

 var utcDate = TimeZoneInfo.ConvertTimeToUtc(someDate, tzi); 

Please note that you must decide what you want to do if your local time occurs twice due to daylight saving or not at all.

+5
source share

Maybe Noda Time can help you?

+4
source share

All Articles