TimeZoneInfo.Local 和 FindSystemTimeZoneById 有什么区别?

What is the difference between TimeZoneInfo.Local and FindSystemTimeZoneById?

我知道时间和时区是一个难题,但我仍然对以下内容感到困惑:

    Console.WriteLine("DST Test");
    var DT = DateTime.Now;
    TimeZoneInfo tzi = TimeZoneInfo.Local;
    bool DaylightSavingTime = tzi.IsDaylightSavingTime(DT);
    Console.WriteLine($"Time: {DT} DST: {DaylightSavingTime} TimeZone: {TimeZoneInfo.Local.DaylightName}");
    tzi = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
    DaylightSavingTime = tzi.IsDaylightSavingTime(DT);
    Console.WriteLine($"Time: {DT} DST: {DaylightSavingTime} TimeZone: {TimeZoneInfo.Local.DaylightName} ");

输出:

DST Test
Time: 31/05/2021 13:19:32 DST: False TimeZone: W. Europe Standard Time
Time: 31/05/2021 13:19:32 DST: True TimeZone: W. Europe Standard Time

因此,如果我尝试通过调用 'TimeZoneInfo.Local' 中的函数来确定时间是否为夏令时,答案是错误的。但是,如果我使用具有相同时区的 .FindSystemTimeZoneById() 函数,我会得到 'true',这是正确的。这怎么可能?

根据您批准的评论,TimeZoneInfoSupportsDaylightSavingTime 收到:TimeZoneInfo.Localfalse

这取决于您的机器设置。来自 docs:

The value of the SupportsDaylightSavingTime property for the local time zone returned by the TimeZoneInfo.Local property reflects the setting of the Control Panel Date and Time application's checkbox that defines whether the system automatically adjusts for daylight saving time. If it is unchecked, or if no checkbox is displayed for a time zone, the value of this property is false.

这是您使用的两种方法之间唯一明显的区别。