Getting Daylight Savings Time Start and End in NodaTime

前端 未结 2 750
旧巷少年郎
旧巷少年郎 2020-12-06 02:00

How can I get the starting and ending dates for Daylight Savings Time using Noda Time? The function below accomplishes this task but it is horribly unwieldy and is begging f

相关标签:
2条回答
  • 2020-12-06 02:33

    There's not a single built-in function that I am aware of, but the data is all there, so you can certainly create your own.

    You're on the right track with what you've shown, but there are a few things to consider:

    • Normally people are interested in the end points of the intervals. By returning the start and stop of only the middle interval, you are likely getting values different than you expect. For example, if you use one of the US time zones, such as "America/Los_Angeles", your function returns the transitions as 3/9/2014 3:00:00 AM and 11/2/2014 2:00:00 AM, where you are probably expecting 2:00 AM for both.

    • Time zones south of the equator that use DST will start it towards the end of the year, and end it towards the beginning of the next year. So sometimes the items in the tuple might be reversed from what you expect them to be.

    • There are quite a lot of time zones that don't use daylight saving time, so throwing an exception isn't the best idea.

    • There are at least two time zones that presently have four transitions in a single year ("Africa/Casablanca" and "Africa/Cairo") - having a "break" in their DST periods for Ramadan. And occasionally, there are non-DST-related transitions, such as when Samoa changed its standard offset in 2011, which gave it three transitions in a single year.

    Taking all of this into account, it would seem better to return a list of single transition points, rather than a tuple of pairs of transitions.

    Also, this is minor, but it would be better form to not bind the method to the system clock at all. The year can easily be passed by parameter. Then you can use this method for non-current years if necessary.

    public IEnumerable<LocalDateTime> GetDaylightSavingTransitions(DateTimeZone timeZone, int year)
    {
        var yearStart = new LocalDateTime(year, 1, 1, 0, 0).InZoneLeniently(timeZone).ToInstant();
        var yearEnd = new LocalDateTime(year + 1, 1, 1, 0, 0).InZoneLeniently(timeZone).ToInstant();
        var intervals = timeZone.GetZoneIntervals(yearStart, yearEnd);
    
        return intervals.Select(x => x.IsoLocalEnd).Where(x => x.Year == year);
    }
    

    Also note at the end, it's important to filter just the values that are in the current year because the intervals may very well extend into the following year, or go on indefinitely.

    0 讨论(0)
  • 2020-12-06 02:38

    This snippet code also help you to check a time is in daylightsavingstime or not

    public static bool IsDaylightSavingsTime(this DateTimeOffset dateTimeOffset)
            {
                var timezone = "Europe/London"; //https://nodatime.org/TimeZones
                ZonedDateTime timeInZone = dateTimeOffset.DateTime.InZone(timezone);
                var instant = timeInZone.ToInstant();
                var zoneInterval = timeInZone.Zone.GetZoneInterval(instant);
                return zoneInterval.Savings != Offset.Zero;
            }
    

    how to use it

    var testDate = DateTimeOffset.Now;
    var isDst = testDate.IsDaylightSavingsTime();
    

    Depend on your situation, you can modify it a bit

    0 讨论(0)
提交回复
热议问题