From 8de7c3713f78349dabbdb2cf1af85bfa42114a4f Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Wed, 28 Jan 2009 09:38:51 +0100 Subject: [PATCH] fix off-by-one at the end of an interval --- src/cron.cpp | 6 +++--- test/test_cron.cpp | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/cron.cpp b/src/cron.cpp index c85c680..bae1c9c 100644 --- a/src/cron.cpp +++ b/src/cron.cpp @@ -45,7 +45,7 @@ time_t WeekCron::get_next_run(time_t calc_from) if (!is_sane()) throw std::runtime_error("illegal cron value"); - if (calc_from <= 86400) + if (calc_from <= 86400*14) throw std::runtime_error("WeekCron doesn't work for times near 0"); if (week.none_set()) @@ -69,8 +69,8 @@ time_t WeekCron::get_next_run(time_t calc_from) time_t since_lastrun=within_interval % every; time_t next_exec=calc_from+(every-since_lastrun); - // next step after end? - if (next_exec > get_next_point(calc_from,end,true)) + // next step at or after end? + if (next_exec >= get_next_point(calc_from,end,true)) return get_next_point(calc_from,begin,true); else return next_exec; diff --git a/test/test_cron.cpp b/test/test_cron.cpp index 67f15ad..dbea5bb 100644 --- a/test/test_cron.cpp +++ b/test/test_cron.cpp @@ -38,6 +38,7 @@ class TestCronFunc : public TestFixture CPPUNIT_TEST(IntervalBeginStep); CPPUNIT_TEST(IntervalWithin); CPPUNIT_TEST(IntervalLaststep); + CPPUNIT_TEST(IntervalLaststepMatch); CPPUNIT_TEST(IntervalEnd); CPPUNIT_TEST(IntervalBigstep); @@ -134,6 +135,12 @@ public: CPPUNIT_ASSERT_EQUAL( 1233187200, static_cast(cron.get_next_run(1233104399))); } + void IntervalLaststepMatch() + { + WeekCron cron("2345",3600,7200,10); + CPPUNIT_ASSERT_EQUAL( 1233187200, static_cast(cron.get_next_run(1233104399))); + } + void IntervalEnd() { WeekCron cron("2345",3600,7200,10); -- 1.7.1