Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the System.TimeZoneInfo.IsDaylightSavingTime equivalent in 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?

like image 353
ash Avatar asked Mar 04 '13 21:03

ash


People also ask

What is IsDaylightSavingTime?

The typical implementation of DST is to set clocks forward by one hour in the spring ("spring forward"), and to set clocks back by one hour in the fall ("fall back") to return to standard time. As a result, there is one 23-hour day in late winter or early spring and one 25-hour day in autumn.

What is the function to offset the date for daylight saving in time series?

IsDaylightSavingTime(DateTimeOffset) Indicates whether a specified date and time falls in the range of daylight saving time for the time zone of the current TimeZoneInfo object.


1 Answers

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.

like image 60
Matt Johnson-Pint Avatar answered Jan 24 '23 18:01

Matt Johnson-Pint