mirror of
https://github.com/PerMalmberg/libcron.git
synced 2025-04-22 00:13:01 -05:00
Test case added.
This commit is contained in:
parent
22da55666a
commit
74800b0d5e
@ -11,50 +11,73 @@ namespace libcron
|
||||
{
|
||||
auto curr = from;
|
||||
|
||||
year_month_day ymd = date::floor<days>(curr);
|
||||
|
||||
// Add months until one of the allowed days are found, or stay at the current one.
|
||||
while (data.get_months().find(static_cast<Months>(unsigned(ymd.month()))) == data.get_months().end())
|
||||
bool done = false;
|
||||
while (!done)
|
||||
{
|
||||
curr += months{1};
|
||||
ymd = date::floor<days>(curr);
|
||||
};
|
||||
year_month_day ymd = date::floor<days>(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<DayOfMonth>(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<Months>(unsigned(ymd.month()))) == data.get_months().end())
|
||||
{
|
||||
curr += days{1};
|
||||
curr += months{1};
|
||||
ymd = date::floor<days>(curr);
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
//Add days until the current weekday is one of the allowed weekdays
|
||||
year_month_weekday ymw = date::floor<days>(curr);
|
||||
continue;
|
||||
}
|
||||
|
||||
while (data.get_day_of_week().find(static_cast<DayOfWeek>(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<days>(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<DayOfMonth>(unsigned(ymd.day()))) ==
|
||||
data.get_day_of_month().end())
|
||||
{
|
||||
curr += days{1};
|
||||
ymd = date::floor<days>(curr);
|
||||
continue;
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
//Add days until the current weekday is one of the allowed weekdays
|
||||
year_month_weekday ymw = date::floor<days>(curr);
|
||||
|
||||
if (data.get_day_of_week().find(static_cast<DayOfWeek>(unsigned(ymw.weekday()))) ==
|
||||
data.get_day_of_week().end())
|
||||
{
|
||||
curr += days{1};
|
||||
ymw = date::floor<days>(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<Hours>(date_time.hour)) == data.get_hours().end())
|
||||
{
|
||||
curr += hours{1};
|
||||
continue;
|
||||
}
|
||||
else if (data.get_minutes().find(static_cast<Minutes >(date_time.min)) == data.get_minutes().end())
|
||||
{
|
||||
curr += minutes{1};
|
||||
continue;
|
||||
}
|
||||
else if (data.get_seconds().find(static_cast<Seconds>(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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user