From: Thomas Jarosch Date: Wed, 28 Jan 2009 10:24:40 +0000 (+0100) Subject: Moved to i2n namespace. Constification. Calculate next_begin only once X-Git-Tag: v2.6~136^2~14^2~3 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=fd6d9c593a30560c5d7027e42294a9e088be82c8;p=libi2ncommon Moved to i2n namespace. Constification. Calculate next_begin only once --- diff --git a/src/cron.cpp b/src/cron.cpp index 77d9eb4..f77d905 100644 --- a/src/cron.cpp +++ b/src/cron.cpp @@ -8,17 +8,18 @@ * */ #include - #include - #include +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 diff --git a/src/cron.hpp b/src/cron.hpp index 91fdc23..9d6e254 100644 --- a/src/cron.hpp +++ b/src/cron.hpp @@ -11,9 +11,11 @@ #define __CRON_HPP #include - #include +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 diff --git a/test/test_cron.cpp b/test/test_cron.cpp index dacba9b..70703ad 100644 --- a/test/test_cron.cpp +++ b/test/test_cron.cpp @@ -18,6 +18,7 @@ #include using namespace std; +using namespace I2n; using namespace CppUnit; class TestCronFunc : public TestFixture