public void GetNextInterval_NegativeInterval_ReturnsOneTick()
        {
            var now  = DateTime.Now;
            var next = now.Subtract(TimeSpan.FromSeconds(1));

            var interval = TimerListener.GetNextTimerInterval(next, now);

            Assert.Equal(1, interval.Ticks);
        }
        public void GetNextInterval_NextWithinDST_ReturnsExpectedValue(TimerSchedule schedule, TimeSpan expectedInterval)
        {
            // This only works with a DST-supported time zone, so throw a nice exception
            if (!TimeZoneInfo.Local.SupportsDaylightSavingTime)
            {
                throw new InvalidOperationException("This test will only pass if the time zone supports DST.");
            }

            // Running at 1:59 AM, i.e. one minute before the DST switch at 2 AM on 3/11 (Pacific Standard Time)
            // Note: this test uses Local time, so if you're running in a timezone where
            // DST doesn't transition the test might not be valid.
            var now = new DateTime(2018, 3, 11, 1, 59, 0, DateTimeKind.Local);

            var next = schedule.GetNextOccurrence(now);

            var interval = TimerListener.GetNextTimerInterval(next, now, schedule.AdjustForDST);

            Assert.Equal(expectedInterval, interval);
        }
        public void GetNextInterval_NextAfterDST_ReturnsExpectedValue()
        {
            // Running on the Friday before the DST switch at 2 AM on 3/11 (Pacific Standard Time)
            // Note: this test uses Local time, so if you're running in a timezone where
            // DST doesn't transition the test might not be valid.
            var now = new DateTime(2018, 3, 9, 18, 0, 0, DateTimeKind.Local);

            // Configure schedule to run again on the next Friday (3/16) at 6 PM (Pacific Daylight Time)
            var schedule = CrontabSchedule.Parse("0 0 18 * * 5", new CrontabSchedule.ParseOptions()
            {
                IncludingSeconds = true
            });

            var next     = schedule.GetNextOccurrence(now);
            var interval = TimerListener.GetNextTimerInterval(next, now);

            // One week is normally 168 hours, but it's 167 hours across DST
            Assert.Equal(167, interval.TotalHours);
        }
        public void GetNextInterval_NextWithinDST_ReturnsExpectedValue()
        {
            // Running at 1:59 AM, i.e. one minute before the DST switch at 2 AM on 3/11 (Pacific Standard Time)
            // Note: this test uses Local time, so if you're running in a timezone where
            // DST doesn't transition the test might not be valid.
            var now = new DateTime(2018, 3, 11, 1, 59, 0, DateTimeKind.Local);

            // Configure schedule to run on the 59th minute of every hour
            var schedule = CrontabSchedule.Parse("0 59 * * * *", new CrontabSchedule.ParseOptions()
            {
                IncludingSeconds = true
            });

            // Note: NCronTab actually gives us an invalid next occurrence of 2:59 AM, which doesn't actually
            // exist because the DST switch skips from 2 to 3
            var next = schedule.GetNextOccurrence(now);

            var interval = TimerListener.GetNextTimerInterval(next, now);

            Assert.Equal(1, interval.TotalHours);
        }
        public void GetNextInterval_NextAfterDST_ReturnsExpectedValue(TimerSchedule schedule, TimeSpan expectedInterval)
        {
            // This only works with a DST-supported time zone, so throw a nice exception
            if (!TimeZoneInfo.Local.SupportsDaylightSavingTime)
            {
                throw new InvalidOperationException("This test will only pass if the time zone supports DST.");
            }

            // Running on the Friday before the DST switch at 2 AM on 3/11 (Pacific Standard Time)
            // Note: this test uses Local time, so if you're running in a timezone where
            // DST doesn't transition the test might not be valid.
            // The input schedules will run after DST changes. For some (Cron), they will subtract
            // an hour to account for the shift. For others (Constant), they will not.
            var now = new DateTime(2018, 3, 9, 18, 0, 0, DateTimeKind.Local);

            var next     = schedule.GetNextOccurrence(now);
            var interval = TimerListener.GetNextTimerInterval(next, now, schedule.AdjustForDST);

            // One week is normally 168 hours, but it's 167 hours across DST
            Assert.Equal(interval, expectedInterval);
        }