1

在 C# 中,给定 UTC 时间,我如何确定这是否属于美国德克萨斯州休斯顿的 DST?

var utcDateTime = new DateTime(2013,1,1,0,0,0,DateTimeKind.Utc);


//bool fallsWithinDstInAmerica = ...?
4

1 回答 1

5

获取适合TimeZoneInfo德州的,然后使用IsDaylightSavingTime.

using System;

class Test
{
    static void Main()
    {
        // Don't be fooled by the "standard" part - this is Central Time
        var zone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
        var winter = new DateTime(2013, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        var summer = new DateTime(2013, 6, 1, 0, 0, 0, DateTimeKind.Utc);
        Console.WriteLine(zone.IsDaylightSavingTime(winter)); // False
        Console.WriteLine(zone.IsDaylightSavingTime(summer)); // True
    }
}

或者使用Noda Time,您可以找到夏令时的数量,并将其与偏移量 0 进行比较:

using System;
using NodaTime;

class Test
{
    static void Main()
    {
        var zone = DateTimeZoneProviders.Tzdb["America/Chicago"];
        var winter = Instant.FromUtc(2013, 1, 1, 0, 0);
        var summer = Instant.FromUtc(2013, 6, 1, 0, 0);

        Console.WriteLine(zone.GetZoneInterval(winter).Savings); // +00
        Console.WriteLine(zone.GetZoneInterval(summer).Savings); // +01
        Console.WriteLine(zone.GetZoneInterval(winter).Savings != Offset.Zero); // False
        Console.WriteLine(zone.GetZoneInterval(summer).Savings != Offset.Zero); // True
    }
}
于 2013-09-20T12:00:17.330 回答