NodaTime中的System.TimeZoneInfo.IsDaylightSavingTime等价物是什么? [英] What is the System.TimeZoneInfo.IsDaylightSavingTime equivalent in NodaTime?

查看:294
本文介绍了NodaTime中的System.TimeZoneInfo.IsDaylightSavingTime等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.TimeZoneInfo有一个方法叫做 IsDaylightSavingTime ,它需要一个DateTime对象,如果指定的datetime属于该时区的DST,则返回true。在 NodaTime 中还有其他方法可以实现相同功能吗?

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?

推荐答案

你可以从 ZoneInterval 中获得这个。这是一个可以帮助的扩展方法。

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;
}

现在可以做:

zdt.IsDaylightSavingsTime();

如果您没有 ZonedDateTime ,您可以从 DateTimeZone 加上一个 Instant LocalDateTime 。或者您可以按照这种扩展方法将其作为参数。

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.

更新:此功能现已包含在Noda Time v1.3及更高版本中,所以您不再需要自己编写扩展方法。

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

这篇关于NodaTime中的System.TimeZoneInfo.IsDaylightSavingTime等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆