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

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

问题描述

System.TimeZoneInfo 有一个名为 IsDaylightSavingTime 的方法,如果指定的日期时间落在该时区的 DST 中,它接受一个 DateTime 对象并返回 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 加上 InstantLocalDateTime 获得一个.或者您可以按摩此扩展方法以将其作为参数.

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天全站免登陆