Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
966 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...