diff --git a/libcron/CronSchedule.cpp b/libcron/CronSchedule.cpp index 5b87a1d..a4ec362 100644 --- a/libcron/CronSchedule.cpp +++ b/libcron/CronSchedule.cpp @@ -11,50 +11,73 @@ namespace libcron { auto curr = from; - year_month_day ymd = date::floor(curr); - - // Add months until one of the allowed days are found, or stay at the current one. - while (data.get_months().find(static_cast(unsigned(ymd.month()))) == data.get_months().end()) + bool done = false; + while (!done) { - curr += months{1}; - ymd = date::floor(curr); - }; + year_month_day ymd = date::floor(curr); - - // If all days are allowed, then the 'day of week' takes precedence, which also means that - // day of week only is ignored when specific days of months are specified. - if (data.get_day_of_month().size() != CronData::value_of(DayOfMonth::Last)) - { - // Add days until one of the allowed days are found, or stay at the current one. - while (data.get_day_of_month().find(static_cast(unsigned(ymd.day()))) == - data.get_day_of_month().end()) + // Add months until one of the allowed days are found, or stay at the current one. + if (data.get_months().find(static_cast(unsigned(ymd.month()))) == data.get_months().end()) { - curr += days{1}; + curr += months{1}; ymd = date::floor(curr); - }; - } - else - { - //Add days until the current weekday is one of the allowed weekdays - year_month_weekday ymw = date::floor(curr); + continue; + } - while (data.get_day_of_week().find(static_cast(unsigned(ymw.weekday()))) == - data.get_day_of_week().end()) + // If all days are allowed, then the 'day of week' takes precedence, which also means that + // day of week only is ignored when specific days of months are specified. + if (data.get_day_of_month().size() != CronData::value_of(DayOfMonth::Last)) { - curr += days{1}; - ymw = date::floor(curr); - }; + // Add days until one of the allowed days are found, or stay at the current one. + if(data.get_day_of_month().find(static_cast(unsigned(ymd.day()))) == + data.get_day_of_month().end()) + { + curr += days{1}; + ymd = date::floor(curr); + continue; + }; + } + else + { + //Add days until the current weekday is one of the allowed weekdays + year_month_weekday ymw = date::floor(curr); + + if (data.get_day_of_week().find(static_cast(unsigned(ymw.weekday()))) == + data.get_day_of_week().end()) + { + curr += days{1}; + ymw = date::floor(curr); + continue; + }; + } + + // Add hours until the current hour is one of the allowed + auto date_time = to_calendar_time(curr); + if (data.get_hours().find(static_cast(date_time.hour)) == data.get_hours().end()) + { + curr += hours{1}; + continue; + } + else if (data.get_minutes().find(static_cast(date_time.min)) == data.get_minutes().end()) + { + curr += minutes{1}; + continue; + } + else if (data.get_seconds().find(static_cast(date_time.sec)) == data.get_seconds().end()) + { + curr += seconds{1}; + continue; + } + else if( curr <= from ) + { + curr += seconds{1}; + } + else + { + done = true; + } } - // 'curr' now represents the next year, month and day matching the expression, with a time of 0:0:0. - - // Re-add the hours, minutes and seconds to 'curr' - auto date_time = to_calendar_time(from); - curr += hours{date_time.hour}; - curr += minutes{date_time.min}; - curr += seconds{date_time.sec}; - - auto t = to_calendar_time(from); return curr; } diff --git a/test/test.cpp b/test/test.cpp index 56bb18b..d9129e6 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -249,16 +249,31 @@ SCENARIO("Calculating next runtime") REQUIRE(t.hour == 1); REQUIRE(t.min == 0); REQUIRE(t.sec == 0); + } + } + AND_WHEN("Start time 05:00:00") + { + sys_days midnight = 2010_y/1/1; + system_clock::time_point five = midnight; + five += hours{5}; + auto t2 = CronSchedule::to_calendar_time(five); - // auto t = std::chrono::system_clock::to_time_t(run_time); -// REQUIRE(t.get_seconds() == 0); -// REQUIRE(t.minute == 0); -// REQUIRE(t.hour == 1); -// REQUIRE(t.) + std::chrono::system_clock::time_point run_time = sched.calculate_from(five); + + THEN("Next runtime is 06:00 of the same date") + { + auto t = CronSchedule::to_calendar_time(run_time); + REQUIRE(t.year == 2010); + REQUIRE(t.month == 1); + REQUIRE(t.day == 1); + REQUIRE(t.hour == 6); + REQUIRE(t.min == 0); + REQUIRE(t.sec == 0); } } + } } \ No newline at end of file