first shot at fixing WeekCron-login, not done yet (#1551)
authorGerd von Egidy <gerd.von.egidy@intra2net.com>
Tue, 31 Mar 2009 16:19:02 +0000 (18:19 +0200)
committerGerd von Egidy <gerd.von.egidy@intra2net.com>
Thu, 2 Apr 2009 16:12:37 +0000 (18:12 +0200)
src/cron.cpp
src/cron.hpp
test/test_cron_interval.cpp
test/test_cron_point.cpp

index 6ccb234..86cdbd7 100644 (file)
@@ -9,6 +9,7 @@
  */
 #include <time.h>
 #include <stdexcept>
+#include <iostream>
 
 #include <cron.hpp>
 
@@ -135,16 +136,21 @@ time_t WeekCron::get_next_run(time_t calc_from) const
     {
         // interval
         time_t next_begin = get_next_point(calc_from,Begin,true);
+std::cerr << "next_begin = " << next_begin << std::endl;
+std::cerr << "next_end = " << get_next_point(calc_from,End-1,true) << std::endl;
         if (next_begin > get_next_point(calc_from,End-1,true))
         {
             // next begin > next end means we are at the begin or within the interval
 
             time_t interval_begin=get_previousnow_point(calc_from,Begin,true);
+std::cerr << "interval_begin = " << interval_begin << std::endl;
 
             time_t within_interval=calc_from - interval_begin;
             time_t since_lastrun=within_interval % Every;
             time_t next_exec=calc_from+(Every-since_lastrun);
 
+std::cerr << "next_exec = " << next_exec << std::endl;
+
             // next step at or after end?
             if (next_exec > get_next_point(calc_from,End-1,true))
                 return get_next_point(calc_from,Begin,true);
@@ -178,14 +184,15 @@ time_t WeekCron::get_next_point(const time_t calc_from, const int daysec, const
     // take care of the weekday
     ft.tm_mday+=Week.days_till_set(static_cast<Week::WeekDay>(ft.tm_wday));                     //lint !e737 !e713
 
-    ft.tm_hour=0;
-    ft.tm_min=0;
-    ft.tm_sec=0;
+    fill_tm_with_wallclock(&ft,daysec);
+
     // tm_isdst means to use the dst in use at the given time
     ft.tm_isdst=-1;
 
-    // get unixtime of start of the day and add daysec
-    time_t target=mktime(&ft)+daysec;
+    time_t target=mktime(&ft);
+
+    // check for completely illegal time, should not happen without bug in this func,
+    // return calc_from as safeguard
     if (target == (time_t)-1)
         return calc_from;
 
@@ -221,14 +228,15 @@ time_t WeekCron::get_previousnow_point(const time_t calc_from, const int daysec,
     // take care of the weekday
     ft.tm_mday-=Week.days_since_set(static_cast<Week::WeekDay>(ft.tm_wday));                            //lint !e737 !e713
 
-    ft.tm_hour=0;
-    ft.tm_min=0;
-    ft.tm_sec=0;
-    // tm_isdst=-1 means to use the dst in use at the given time
+    fill_tm_with_wallclock(&ft,daysec);
+
+    // tm_isdst means to use the dst in use at the given time
     ft.tm_isdst=-1;
 
-    // get unixtime of start of the day and add daysec
-    time_t target=mktime(&ft)+daysec;
+    time_t target=mktime(&ft);
+
+    // check for completely illegal time, should not happen without bug in this func,
+    // return calc_from as safeguard
     if (target == (time_t)-1)
         return calc_from;
 
@@ -245,5 +253,23 @@ time_t WeekCron::get_previousnow_point(const time_t calc_from, const int daysec,
     return target;
 }
 
+/**
+ * Converts #daysec into hour/minute/second and fills it into the provided struct tm
+ * @param ft struct tm to fill wallclock time into
+ * @param daysec Start point of time in seconds since the start of the day
+ */
+void WeekCron::fill_tm_with_wallclock(struct tm *ft, const int daysec) const
+{
+    int remain=daysec;
+
+    ft->tm_hour=remain/3600;
+    remain-=ft->tm_hour*3600;
+
+    ft->tm_min=remain/60;
+    remain-=ft->tm_min*60;
+
+    ft->tm_sec=remain;
+}
+
 }
 }
index 791a08f..22595f8 100644 (file)
@@ -38,6 +38,7 @@ class WeekCron
 
         time_t get_next_point(const time_t calc_from, const int daysec, const bool todaycheck) const;
         time_t get_previousnow_point(const time_t calc_from, const int daysec, const bool todaycheck) const;
+        void fill_tm_with_wallclock(struct tm *ft, const int daysec) const;
 
     public:
         WeekCron();
index f149d89..cf40cce 100644 (file)
@@ -76,6 +76,8 @@ class TestCronIntervalFunc : public TestFixture
     CPPUNIT_TEST(IntervalInDSTForward2);
     CPPUNIT_TEST(IntervalInDSTForward3);
     CPPUNIT_TEST(IntervalInDSTForward4);
+    CPPUNIT_TEST(IntervalInDSTForward5);
+    CPPUNIT_TEST(IntervalInDSTForward6);
     CPPUNIT_TEST(IntervalOutDSTForward);
     CPPUNIT_TEST(IntervalDSTForwardWholeday1);
     CPPUNIT_TEST(IntervalDSTForwardWholeday2);
@@ -447,6 +449,23 @@ public:
         CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1238319780), cron.get_next_run(1238319726));
     }
 
+    void IntervalInDSTForward5()
+    {
+        // FIXME: Interval start 3600 works, 3599 and below does not
+        WeekCron cron("0123456",3599,86340,60);
+        // calc at: Sun Mar 29 09:42:06 2009
+        // expected next run: Sun Mar 29 09:43:00 2009
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1238319780), cron.get_next_run(1238319726));
+    }
+
+    void IntervalInDSTForward6()
+    {
+        WeekCron cron("0123456",3600,86340,60);
+        // calc at: Sun Mar 29 09:42:06 2009
+        // expected next run: Sun Mar 29 09:43:00 2009
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1238319780), cron.get_next_run(1238319726));
+    }
+
     void IntervalOutDSTForward()
     {
         WeekCron cron("0123456",9000,14400,60);
index 0bd5752..d59a22e 100644 (file)
@@ -37,7 +37,8 @@ class TestCronPointFunc : public TestFixture
     CPPUNIT_TEST(StartLastDaysec);
     CPPUNIT_TEST(HalfOpenInterval);
     CPPUNIT_TEST(LongBeforeDSTBackwards);
-    CPPUNIT_TEST(BeforeDSTBackwards);
+    CPPUNIT_TEST(BeforeDSTBackwards1);
+    CPPUNIT_TEST(BeforeDSTBackwards2);
     CPPUNIT_TEST(AtDSTBackwards);
     CPPUNIT_TEST(DuringDSTBackwards);
     CPPUNIT_TEST(EndDSTBackwards1);
@@ -47,12 +48,14 @@ class TestCronPointFunc : public TestFixture
     CPPUNIT_TEST(OverDSTBackwards);
     CPPUNIT_TEST(OverDSTBackwardsDaychange);
     CPPUNIT_TEST(LongBeforeDSTForward);
-    CPPUNIT_TEST(BeforeDSTForward);
+    CPPUNIT_TEST(BeforeDSTForward1);
+    CPPUNIT_TEST(BeforeDSTForward2);
     CPPUNIT_TEST(AtDSTForward);
     CPPUNIT_TEST(DuringDSTForward);
     CPPUNIT_TEST(EndDSTForward1);
     CPPUNIT_TEST(EndDSTForward2);
     CPPUNIT_TEST(EndDSTForward3);
+    CPPUNIT_TEST(EndDSTForward4);
     CPPUNIT_TEST(OverDSTForward);
     CPPUNIT_TEST(OverDSTForwardDaychange);
 
@@ -139,16 +142,24 @@ public:
     {
         WeekCron cron("0123456",75600);
         // calc at: Sun Oct 26 00:00:00 CEST 2008
-        // expected next run: Sun Oct 26 20:00:00 2008
-        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1225047600), cron.get_next_run(1224968400));
+        // expected next run: Sun Oct 26 21:00:00 CET 2008
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1225051200), cron.get_next_run(1224972000));
     }
 
-    void BeforeDSTBackwards()
+    void BeforeDSTBackwards1()
+    {
+        WeekCron cron("0123456",7200);
+        // calc at: Sun Oct 26 01:59:59 CEST 2008
+        // expected next run: Sun Oct 26 02:00:00 CET 2008
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1224982800), cron.get_next_run(1224979199));
+    }
+
+    void BeforeDSTBackwards2()
     {
         WeekCron cron("0123456",7200);
         // calc at: Sun Oct 26 02:59:59 CEST 2008
         // expected next run: Sun Oct 26 02:00:00 CET 2008
-        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1224979200), cron.get_next_run(1224979199));
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1224982800), cron.get_next_run(1224982799));
     }
 
     void AtDSTBackwards()
@@ -156,7 +167,7 @@ public:
         WeekCron cron("0123456",7200);
         // calc at: Sun Oct 26 02:00:00 CET 2008
         // expected next run: Mon Oct 27 02:00:00 CET 2008
-        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1225069200), cron.get_next_run(1224979200));
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1225069200), cron.get_next_run(1224982800));
     }
 
     void DuringDSTBackwards()
@@ -164,54 +175,54 @@ public:
         WeekCron cron("0123456",7200);
         // calc at: Sun Oct 26 02:00:01 CET 2008
         // expected next run: Mon Oct 27 02:00:00 CET 2008
-        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1225069200), cron.get_next_run(1224979201));
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1225069200), cron.get_next_run(1224982801));
     }
 
     void EndDSTBackwards1()
     {
         WeekCron cron("0123456",10800);
-        // calc at: Sun Oct 26 02:00:00 CET 2008
-        // expected next run: Sun Oct 26 02:00:00 2008
-        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1224982800), cron.get_next_run(1224979200));
+        // calc at: Sun Oct 26 02:00:00 CEST 2008
+        // expected next run: Sun Oct 26 03:00:00 CET 2008
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1224986400), cron.get_next_run(1224979200));
     }
 
     void EndDSTBackwards2()
     {
         WeekCron cron("0123456",10800);
-        // calc at: Sun Oct 26 02:00:01 CET 2008
-        // expected next run: Sun Oct 26 02:00:00 2008
-        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1224982800), cron.get_next_run(1224979201));
+        // calc at: Sun Oct 26 02:00:01 CEST 2008
+        // expected next run: Sun Oct 26 03:00:00 CET 2008
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1224986400), cron.get_next_run(1224979201));
     }
 
     void EndDSTBackwards3()
     {
         WeekCron cron("0123456",10799);
-        // calc at: Sun Oct 26 02:00:01 CET 2008
+        // calc at: Sun Oct 26 02:00:01 CEST 2008
         // expected next run: Sun Oct 26 02:59:59 CET 2008
-        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1224982799), cron.get_next_run(1224979201));
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1224986399), cron.get_next_run(1224979201));
     }
 
     void EndDSTBackwards4()
     {
         WeekCron cron("0123456",10799);
-        // calc at: Sun Oct 26 02:03:20 2008
-        // expected next run: Mon Oct 27 02:59:59 CET 2008
-        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1225072799), cron.get_next_run(1224983000));
+        // calc at: Sun Oct 26 02:03:20 CET 2008
+        // expected next run: Sun Oct 26 02:59:59 CET 2008
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1224986399), cron.get_next_run(1224983000));
     }
 
     void OverDSTBackwards()
     {
         WeekCron cron("234",10800);
-        // calc at: Sat Oct 25 05:00:00 2008
-        // expected next run: Tue Oct 28 03:00:00 2008
+        // calc at: Sat Oct 25 05:00:00 CEST 2008
+        // expected next run: Tue Oct 28 03:00:00 CET 2008
         CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1225159200), cron.get_next_run(1224903600));
     }
 
     void OverDSTBackwardsDaychange()
     {
         WeekCron cron("234",0);
-        // calc at: Sat Oct 25 00:00:00 2008
-        // expected next run: Tue Oct 28 00:00:00 2008
+        // calc at: Sat Oct 25 00:00:00 CEST 2008
+        // expected next run: Tue Oct 28 00:00:00 CET 2008
         CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1225148400), cron.get_next_run(1224885600));
     }
 
@@ -219,11 +230,11 @@ public:
     {
         WeekCron cron("0123456",75600);
         // calc at: Sat Mar 28 22:00:00 CET 2009
-        // expected next run: Sun Mar 29 22:00:00 2009
-        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1238356800), cron.get_next_run(1238274000));
+        // expected next run: Sun Mar 29 21:00:00 CEST 2009
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1238353200), cron.get_next_run(1238274000));
     }
 
-    void BeforeDSTForward()
+    void BeforeDSTForward1()
     {
         WeekCron cron("0123456",7200);
         // calc at: Sun Mar 29 01:59:59 CET 2009
@@ -231,6 +242,14 @@ public:
         CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1238288400), cron.get_next_run(1238288399));
     }
 
+    void BeforeDSTForward2()
+    {
+        WeekCron cron("0123456",0);
+        // calc at: Sun Mar 29 01:59:59 CET 2009
+        // expected next run: Mon Mar 30 00:00:00 CEST 2009
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1238364000), cron.get_next_run(1238288399));
+    }
+
     void AtDSTForward()
     {
         WeekCron cron("0123456",7200);
@@ -251,39 +270,47 @@ public:
     {
         WeekCron cron("0123456",10800);
         // calc at: Sun Mar 29 01:59:59 CET 2009
-        // expected next run: Sun Mar 29 04:00:00 2009
-        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1238292000), cron.get_next_run(1238288399));
+        // expected next run: Sun Mar 29 03:00:00 CEST 2009
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1238288400), cron.get_next_run(1238288399));
     }
 
     void EndDSTForward2()
     {
         WeekCron cron("0123456",10800);
-        // calc at: Sun Mar 29 03:00:00 2009
-        // expected next run: Sun Mar 29 04:00:00 2009
-        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1238292000), cron.get_next_run(1238288400));
+        // calc at: Sun Mar 29 03:00:00 CEST 2009
+        // expected next run: Mon Mar 30 03:00:00 CEST 2009
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1238374800), cron.get_next_run(1238288400));
     }
 
     void EndDSTForward3()
     {
         WeekCron cron("0123456",10800);
-        // calc at: Sun Mar 29 04:00:00 2009
-        // expected next run: Mon Mar 30 03:00:00 2009
+        // calc at: Sun Mar 29 04:00:00 CEST 2009
+        // expected next run: Mon Mar 30 03:00:00 CEST 2009
         CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1238374800), cron.get_next_run(1238292000));
     }
 
+    void EndDSTForward4()
+    {
+        WeekCron cron("0123456",86339);
+        // calc at: Sun Mar 29 11:42:06 CEST 2009
+        // expected next run: Sun Mar 29 23:58:59 CEST 2009
+        CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1238363939), cron.get_next_run(1238319726));
+    }
+
     void OverDSTForward()
     {
         WeekCron cron("234",10800);
-        // calc at: Sat Mar 28 23:00:00 2009
-        // expected next run: Tue Mar 31 03:00:00 2009
+        // calc at: Sat Mar 28 23:00:00 CET 2009
+        // expected next run: Tue Mar 31 03:00:00 CEST 2009
         CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1238461200), cron.get_next_run(1238277600));
     }
 
     void OverDSTForwardDaychange()
     {
         WeekCron cron("1234",0);
-        // calc at: Sun Mar 29 00:00:00 2009
-        // expected next run: Mon Mar 30 00:00:00 2009
+        // calc at: Sun Mar 29 00:00:00 CET 2009
+        // expected next run: Mon Mar 30 00:00:00 CEST 2009
         CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(1238364000), cron.get_next_run(1238281200));
     }