fix off-by-one at the end of an interval
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 28 Jan 2009 08:38:51 +0000 (09:38 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 28 Jan 2009 08:38:51 +0000 (09:38 +0100)
src/cron.cpp
test/test_cron.cpp

index c85c680..bae1c9c 100644 (file)
@@ -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;
index 67f15ad..dbea5bb 100644 (file)
@@ -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<int>(cron.get_next_run(1233104399)));
     }
 
+    void IntervalLaststepMatch()
+    {
+        WeekCron cron("2345",3600,7200,10);
+        CPPUNIT_ASSERT_EQUAL( 1233187200, static_cast<int>(cron.get_next_run(1233104399)));
+    }
+
     void IntervalEnd()
     {
         WeekCron cron("2345",3600,7200,10);