Moved to i2n namespace. Constification. Calculate next_begin only once
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 28 Jan 2009 10:24:40 +0000 (11:24 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 28 Jan 2009 10:24:40 +0000 (11:24 +0100)
src/cron.cpp
src/cron.hpp
test/test_cron.cpp

index 77d9eb4..f77d905 100644 (file)
@@ -8,17 +8,18 @@
  *
  */
 #include <time.h>
-
 #include <stdexcept>
-
 #include <cron.hpp>
 
+namespace I2n
+{
+
 /**
     * Constructor
     * @param daystring String representing the active weekdays as numbers. 0 is Sunday.
     * @param begin Start point of time in seconds since the start of the day
     */
-WeekCron::WeekCron(const std::string& daystring, int begin)
+WeekCron::WeekCron(const std::string& daystring, const int begin)
     : Begin(begin)
     , End(0)
     , Every(-1)
@@ -33,7 +34,7 @@ WeekCron::WeekCron(const std::string& daystring, int begin)
     * @param end End point of time in seconds since the start of the day. Only used when every != -1
     * @param every Repeat event every xxx seconds in the half-open interval of begin and end. -1 is disabled
     */
-WeekCron::WeekCron(const std::string& daystring, int begin, int end, int every)
+WeekCron::WeekCron(const std::string& daystring, const int begin, const int end, const int every)
     : Begin(begin)
     , End(end)
     , Every(every)
@@ -77,7 +78,7 @@ time_t WeekCron::get_next_run(time_t calc_from)
         throw std::runtime_error("illegal cron value");
 
     if (calc_from <= 86400*14)
-        throw std::runtime_error("WeekCron doesn't work for times near 0");
+        throw std::runtime_error("WeekCron doesn't work for timestamps near 0");
 
     if (Week.none_set())
         return 0;
@@ -90,7 +91,8 @@ time_t WeekCron::get_next_run(time_t calc_from)
     else
     {
         // interval
-        if (get_next_point(calc_from,Begin,true) > get_next_point(calc_from,End-1,true))
+        time_t next_begin = get_next_point(calc_from,Begin,true);
+        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
 
@@ -109,7 +111,7 @@ time_t WeekCron::get_next_run(time_t calc_from)
         else
         {
             // next begin < next end means we are out of the interval: next begin is next run
-            return get_next_point(calc_from,Begin,true);
+            return next_begin;
         }
     }
 }
@@ -123,7 +125,7 @@ time_t WeekCron::get_next_run(time_t calc_from)
                                   If yes, we will advance to the next day.
  * @return Next point in time
  */
-time_t WeekCron::get_next_point(time_t calc_from, int daysec, bool todaycheck)
+time_t WeekCron::get_next_point(const time_t calc_from, const int daysec, const bool todaycheck)
 {
     struct tm ft;
     localtime_r(&calc_from,&ft);
@@ -159,7 +161,7 @@ time_t WeekCron::get_next_point(time_t calc_from, int daysec, bool todaycheck)
  * @return Last point in time
  * FIXME: Is the description correct?
  */
-time_t WeekCron::get_lastnow_point(time_t calc_from, int daysec, bool todaycheck)
+time_t WeekCron::get_lastnow_point(const time_t calc_from, const int daysec, const bool todaycheck)
 {
     struct tm ft;
     localtime_r(&calc_from,&ft);
@@ -185,3 +187,5 @@ time_t WeekCron::get_lastnow_point(time_t calc_from, int daysec, bool todaycheck
 
     return target;
 }
+
+}
\ No newline at end of file
index 91fdc23..9d6e254 100644 (file)
 #define __CRON_HPP
 
 #include <time.h>
-
 #include <timefunc.hxx>
 
+namespace I2n
+{
+
 /// Time points and intervals repeating each week
 /**
     This class represents recurring time points and intervals
@@ -31,12 +33,12 @@ class WeekCron
         /// Stores the active days this WeekCron is valid for
         WEEK Week;
 
-        time_t get_next_point(time_t calc_from, int daysec,bool todaycheck);
-        time_t get_lastnow_point(time_t calc_from, int daysec,bool todaycheck);
+        time_t get_next_point(const time_t calc_from, const int daysec, const bool todaycheck);
+        time_t get_lastnow_point(const time_t calc_from, const int daysec, const bool todaycheck);
 
     public:
-        WeekCron(const std::string& daystring, int begin);
-        WeekCron(const std::string& daystring, int begin, int end, int every);
+        WeekCron(const std::string& daystring, const int begin);
+        WeekCron(const std::string& daystring, const int begin, const int end, const int every);
 
         bool is_sane() const;
 
@@ -44,4 +46,6 @@ class WeekCron
 
 };
 
+}
+
 #endif
index dacba9b..70703ad 100644 (file)
@@ -18,6 +18,7 @@
 #include <cron.hpp>
 
 using namespace std;
+using namespace I2n;
 using namespace CppUnit;
 
 class TestCronFunc : public TestFixture