Is there a generic TimeZoneInfo For Central Europe?

前端 未结 2 1045
被撕碎了的回忆
被撕碎了的回忆 2021-02-13 04:41

Is there a generic TimeZoneInfo for Central Europe that takes into consideration both CET and CEST into one?

I have an app that is doing the following:

T         


        
2条回答
  •  一生所求
    2021-02-13 05:08

    Firstly, I'd like to applaud mgnoonan's answer of using Noda Time :) But if you're feeling less adventurous...

    You're already using the right time zone - but you shouldn't be using BaseUtcOffset which is documented not to be about DST:

    Gets the time difference between the current time zone's standard time and Coordinated Universal Time (UTC).

    It can't possibly take DST into consideration when you're not providing it a DateTime to fetch the offset for :)

    Assuming someDate is a DateTime, you could use:

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

    Or just ConvertTimeToUtc:

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

    Note that you should work out what you want to do if your local time occurs twice due to a DST transition, or doesn't occur at all.

提交回复
热议问题