What is the System.TimeZoneInfo.IsDaylightSavingTime equivalent in NodaTime?

泄露秘密 提交于 2019-12-17 20:49:47

问题


System.TimeZoneInfo has a method called IsDaylightSavingTime, which takes a DateTime object and returns true if the specified datetime falls in the DST for that timezone. Is there an equivalent function in NodaTime or another way to achieve the same result?


回答1:


You can get this from a ZoneInterval. Here is an extension method that will help.

public static bool IsDaylightSavingsTime(this ZonedDateTime zonedDateTime)
{
    var instant = zonedDateTime.ToInstant();
    var zoneInterval = zonedDateTime.Zone.GetZoneInterval(instant);
    return zoneInterval.Savings != Offset.Zero;
}

Now you can do:

zdt.IsDaylightSavingsTime();

If you don't have a ZonedDateTime, you can get one from a DateTimeZone plus either an Instant or a LocalDateTime. Or you can massage this extension method to take those as parameters.

Update: This function is now included in Noda Time v1.3 and higher, so you no longer have to write the extension method yourself.



来源:https://stackoverflow.com/questions/15211052/what-is-the-system-timezoneinfo-isdaylightsavingtime-equivalent-in-nodatime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!