Change name of class and update API.

Add Howard Hinnats's date library.
This commit is contained in:
Per Malmberg 2018-03-09 01:17:46 -08:00
parent d19de56b4f
commit a6dd20d150
8 changed files with 8222 additions and 128 deletions

View File

@ -4,6 +4,8 @@ project(libcron)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wpedantic -fsanitize=address")
include_directories(externals/date)
add_library(${PROJECT_NAME}
Cron.h
Cron.cpp Task.h CronTime.h TimeTypes.h CronTime.cpp)
Cron.cpp Task.h CronData.h TimeTypes.h CronData.cpp)

View File

@ -3,7 +3,7 @@
bool libcron::Cron::add_schedule(const std::string &schedule, std::function<void()> work)
{
auto cron = CronTime::create(schedule);
auto cron = CronData::create(schedule);
bool res = cron.is_valid();
if (res)
{

View File

@ -1,23 +1,23 @@
#include "CronTime.h"
#include "CronData.h"
namespace libcron
{
CronTime CronTime::create(const std::string& cron_expression)
CronData CronData::create(const std::string& cron_expression)
{
CronTime c;
CronData c;
c.parse(cron_expression);
return std::move(c);
}
CronTime::CronTime()
CronData::CronData()
: month_names({"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}),
day_names({"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"})
{
}
void CronTime::parse(const std::string& cron_expression)
void CronData::parse(const std::string& cron_expression)
{
// First, split on white-space. We expect six parts.
std::regex split{R"#(^\s*(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s*$)#",
@ -36,7 +36,7 @@ namespace libcron
}
}
std::vector<std::string> CronTime::split(const std::string& s, char token)
std::vector<std::string> CronData::split(const std::string& s, char token)
{
std::vector<std::string> res;
@ -53,7 +53,7 @@ namespace libcron
return res;
}
bool CronTime::is_number(const std::string& s)
bool CronData::is_number(const std::string& s)
{
// Find any character that isn't a number.
return !s.empty()
@ -62,7 +62,7 @@ namespace libcron
{ return !std::isdigit(c); }) == s.end();
}
bool CronTime::is_between(int32_t value, int32_t low_limit, int32_t high_limt)
bool CronData::is_between(int32_t value, int32_t low_limit, int32_t high_limt)
{
return value >= low_limit && value <= high_limt;
}

View File

@ -10,6 +10,9 @@
namespace libcron
{
/*
This class parses strings in the format specified below and holds the resulting allowed values
in its internal sets.
Cron format, 6 parts:
seconds (0 - 59)
@ -51,27 +54,49 @@ namespace libcron
*/
class CronTime
class CronData
{
public:
static CronTime create(const std::string& cron_expression);
static CronData create(const std::string& cron_expression);
CronTime();
bool operator<(const CronTime& other) const
{
return next_run_time < other.next_run_time;
}
CronData();
bool is_valid() const
{
return valid;
}
#ifndef EXPOSE_PRIVATE_PARTS
const std::set<Seconds>& get_seconds() const
{
return seconds;
}
const std::set<Minutes>& get_minutes() const
{
return minutes;
}
const std::set<Hours>& get_hours() const
{
return hours;
}
const std::set<DayOfMonth>& get_day_of_month() const
{
return day_of_month;
}
const std::set<Months>& get_months() const
{
return months;
}
const std::set<DayOfWeek>& get_day_of_week() const
{
return day_of_week;
}
private:
#endif
void parse(const std::string& cron_expression);
@ -110,7 +135,6 @@ namespace libcron
bool is_between(int32_t value, int32_t low_limit, int32_t high_limit);
std::chrono::system_clock::time_point next_run_time{};
std::set<Seconds> seconds{};
std::set<Minutes> minutes{};
std::set<Hours> hours{};
@ -127,7 +151,7 @@ namespace libcron
};
template<typename T>
bool CronTime::validate_numeric(const std::string& s, std::set<T>& numbers)
bool CronData::validate_numeric(const std::string& s, std::set<T>& numbers)
{
std::vector<std::string> parts = split(s, ',');
@ -135,7 +159,7 @@ namespace libcron
}
template<typename T>
bool CronTime::validate_literal(const std::string& s,
bool CronData::validate_literal(const std::string& s,
std::set<T>& numbers,
const std::vector<std::string>& names)
{
@ -164,7 +188,7 @@ namespace libcron
}
template<typename T>
bool CronTime::process_parts(const std::vector<std::string>& parts, std::set<T>& numbers)
bool CronData::process_parts(const std::vector<std::string>& parts, std::set<T>& numbers)
{
bool res = true;
@ -227,7 +251,7 @@ namespace libcron
}
template<typename T>
bool CronTime::get_range(const std::string& s, T& low, T& high)
bool CronData::get_range(const std::string& s, T& low, T& high)
{
bool res = false;
@ -254,7 +278,7 @@ namespace libcron
}
template<typename T>
bool CronTime::get_step(const std::string& s, uint8_t& start, uint8_t& step)
bool CronData::get_step(const std::string& s, uint8_t& start, uint8_t& step)
{
bool res = false;
@ -281,7 +305,7 @@ namespace libcron
}
template<typename T>
void CronTime::add_full_range(std::set<T>& set)
void CronData::add_full_range(std::set<T>& set)
{
for (auto v = value_of(T::First); v <= value_of(T::Last); ++v)
{
@ -293,7 +317,7 @@ namespace libcron
}
template<typename T>
bool CronTime::add_number(std::set<T>& set, int32_t number)
bool CronData::add_number(std::set<T>& set, int32_t number)
{
bool res = true;
@ -315,7 +339,7 @@ namespace libcron
}
template<typename T>
bool CronTime::is_within_limits(int32_t low, int32_t high)
bool CronData::is_within_limits(int32_t low, int32_t high)
{
return is_between(low, value_of(T::First), value_of(T::Last))
&& is_between(high, value_of(T::First), value_of(T::Last));

View File

@ -1,7 +1,7 @@
#pragma once
#include <functional>
#include "CronTime.h"
#include "CronData.h"
namespace libcron
{
@ -9,18 +9,18 @@ namespace libcron
{
public:
Task(CronTime time, std::function<void()> task)
Task(CronData time, std::function<void()> task)
: time(std::move(time)), task(std::move(task))
{
}
bool operator<(const Task& other) const
{
return time < other.time;
return false;
}
private:
CronTime time{};
CronData time{};
std::function<void()> task;
};
}

8046
libcron/externals/date/date.h vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -4,8 +4,6 @@ project(cron_test)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wpedantic -fsanitize=address -lasan")
add_definitions(-DEXPOSE_PRIVATE_PARTS)
include_directories(
externals/Catch2/single_include/
..

View File

@ -3,7 +3,7 @@
#include <catch.hpp>
#include <libcron/Cron.h>
#include <libcron/CronTime.h>
#include <libcron/CronData.h>
using namespace libcron;
@ -39,68 +39,68 @@ SCENARIO("Numerical inputs")
{
THEN("All parts are filled")
{
auto c = CronTime::create("* * * * * *");
auto c = CronData::create("* * * * * *");
REQUIRE(c.is_valid());
REQUIRE(c.seconds.size() == 60);
REQUIRE(has_value_range(c.seconds, 0, 59));
REQUIRE(c.minutes.size() == 60);
REQUIRE(has_value_range(c.minutes, 0, 59));
REQUIRE(c.hours.size() == 24);
REQUIRE(has_value_range(c.hours, 0, 23));
REQUIRE(c.day_of_month.size() == 31);
REQUIRE(has_value_range(c.day_of_month, 1, 31));
REQUIRE(c.day_of_week.size() == 7);
REQUIRE(has_value_range(c.day_of_week, 0, 6));
REQUIRE(c.get_seconds().size() == 60);
REQUIRE(has_value_range(c.get_seconds(), 0, 59));
REQUIRE(c.get_minutes().size() == 60);
REQUIRE(has_value_range(c.get_minutes(), 0, 59));
REQUIRE(c.get_hours().size() == 24);
REQUIRE(has_value_range(c.get_hours(), 0, 23));
REQUIRE(c.get_day_of_month().size() == 31);
REQUIRE(has_value_range(c.get_day_of_month(), 1, 31));
REQUIRE(c.get_day_of_week().size() == 7);
REQUIRE(has_value_range(c.get_day_of_week(), 0, 6));
}
}
AND_WHEN("Using full forward range")
{
THEN("Ranges are correct")
{
auto c = CronTime::create("* 0-59 * * * *");
auto c = CronData::create("* 0-59 * * * *");
REQUIRE(c.is_valid());
REQUIRE(c.seconds.size() == 60);
REQUIRE(c.minutes.size() == 60);
REQUIRE(c.hours.size() == 24);
REQUIRE(c.day_of_month.size() == 31);
REQUIRE(c.day_of_week.size() == 7);
REQUIRE(has_value_range(c.seconds, 0, 59));
REQUIRE(c.get_seconds().size() == 60);
REQUIRE(c.get_minutes().size() == 60);
REQUIRE(c.get_hours().size() == 24);
REQUIRE(c.get_day_of_month().size() == 31);
REQUIRE(c.get_day_of_week().size() == 7);
REQUIRE(has_value_range(c.get_seconds(), 0, 59));
}
}
AND_WHEN("Using partial range")
{
THEN("Ranges are correct")
{
auto c = CronTime::create("* * * 20-30 * *");
auto c = CronData::create("* * * 20-30 * *");
REQUIRE(c.is_valid());
REQUIRE(c.seconds.size() == 60);
REQUIRE(c.minutes.size() == 60);
REQUIRE(c.hours.size() == 24);
REQUIRE(c.day_of_month.size() == 11);
REQUIRE(c.day_of_week.size() == 7);
REQUIRE(has_value_range(c.day_of_month, 20, 30));
REQUIRE(c.get_seconds().size() == 60);
REQUIRE(c.get_minutes().size() == 60);
REQUIRE(c.get_hours().size() == 24);
REQUIRE(c.get_day_of_month().size() == 11);
REQUIRE(c.get_day_of_week().size() == 7);
REQUIRE(has_value_range(c.get_day_of_month(), 20, 30));
}
}
AND_WHEN("Using backward range")
{
THEN("Number of hours are correct")
{
auto c = CronTime::create("* * 20-5 * * *");
auto c = CronData::create("* * 20-5 * * *");
REQUIRE(c.is_valid());
REQUIRE(c.hours.size() == 10);
REQUIRE(c.hours.find(Hours::First) != c.hours.end());
REQUIRE(c.get_hours().size() == 10);
REQUIRE(c.get_hours().find(Hours::First) != c.get_hours().end());
}
}
AND_WHEN("Using various ranges")
{
THEN("Validation succeeds")
{
REQUIRE(CronTime::create("0-59 * * * * *").is_valid());
REQUIRE(CronTime::create("* 0-59 * * * *").is_valid());
REQUIRE(CronTime::create("* * 0-23 * * *").is_valid());
REQUIRE(CronTime::create("* * * 1-31 * *").is_valid());
REQUIRE(CronTime::create("* * * * 1-12 *").is_valid());
REQUIRE(CronTime::create("* * * * * 0-6").is_valid());
REQUIRE(CronData::create("0-59 * * * * *").is_valid());
REQUIRE(CronData::create("* 0-59 * * * *").is_valid());
REQUIRE(CronData::create("* * 0-23 * * *").is_valid());
REQUIRE(CronData::create("* * * 1-31 * *").is_valid());
REQUIRE(CronData::create("* * * * 1-12 *").is_valid());
REQUIRE(CronData::create("* * * * * 0-6").is_valid());
}
}
}
@ -110,22 +110,22 @@ SCENARIO("Numerical inputs")
{
THEN("Validation fails")
{
REQUIRE_FALSE(CronTime::create("").is_valid());
REQUIRE_FALSE(CronTime::create("-").is_valid());
REQUIRE_FALSE(CronTime::create("* ").is_valid());
REQUIRE_FALSE(CronTime::create("* 0-60 * * * *").is_valid());
REQUIRE_FALSE(CronTime::create("* * 0-25 * * *").is_valid());
REQUIRE_FALSE(CronTime::create("* * * 1-32 * *").is_valid());
REQUIRE_FALSE(CronTime::create("* * * * 1-13 *").is_valid());
REQUIRE_FALSE(CronTime::create("* * * * * 0-7").is_valid());
REQUIRE_FALSE(CronTime::create("* * * 0-31 * *").is_valid());
REQUIRE_FALSE(CronTime::create("* * * * 0-12 *").is_valid());
REQUIRE_FALSE(CronTime::create("60 * * * * *").is_valid());
REQUIRE_FALSE(CronTime::create("* 60 * * * *").is_valid());
REQUIRE_FALSE(CronTime::create("* * 25 * * *").is_valid());
REQUIRE_FALSE(CronTime::create("* * * 32 * *").is_valid());
REQUIRE_FALSE(CronTime::create("* * * * 13 *").is_valid());
REQUIRE_FALSE(CronTime::create("* * * * * 7").is_valid());
REQUIRE_FALSE(CronData::create("").is_valid());
REQUIRE_FALSE(CronData::create("-").is_valid());
REQUIRE_FALSE(CronData::create("* ").is_valid());
REQUIRE_FALSE(CronData::create("* 0-60 * * * *").is_valid());
REQUIRE_FALSE(CronData::create("* * 0-25 * * *").is_valid());
REQUIRE_FALSE(CronData::create("* * * 1-32 * *").is_valid());
REQUIRE_FALSE(CronData::create("* * * * 1-13 *").is_valid());
REQUIRE_FALSE(CronData::create("* * * * * 0-7").is_valid());
REQUIRE_FALSE(CronData::create("* * * 0-31 * *").is_valid());
REQUIRE_FALSE(CronData::create("* * * * 0-12 *").is_valid());
REQUIRE_FALSE(CronData::create("60 * * * * *").is_valid());
REQUIRE_FALSE(CronData::create("* 60 * * * *").is_valid());
REQUIRE_FALSE(CronData::create("* * 25 * * *").is_valid());
REQUIRE_FALSE(CronData::create("* * * 32 * *").is_valid());
REQUIRE_FALSE(CronData::create("* * * * 13 *").is_valid());
REQUIRE_FALSE(CronData::create("* * * * * 7").is_valid());
}
}
}
@ -139,59 +139,59 @@ SCENARIO("Literal input")
{
THEN("Range is valid")
{
auto c = CronTime::create("* * * * JAN-MAR *");
auto c = CronData::create("* * * * JAN-MAR *");
REQUIRE(c.is_valid());
REQUIRE(has_value_range(c.months, 1, 3));
REQUIRE(has_value_range(c.get_months(), 1, 3));
}
AND_THEN("Range is valid")
{
auto c = CronTime::create("* * * * * SUN-FRI");
auto c = CronData::create("* * * * * SUN-FRI");
REQUIRE(c.is_valid());
REQUIRE(has_value_range(c.day_of_week, 0, 5));
REQUIRE(has_value_range(c.get_day_of_week(), 0, 5));
}
}
AND_WHEN("Using both range and specific month")
{
THEN("Range is valid")
{
auto c = CronTime::create("* * * * JAN-MAR,DEC *");
auto c = CronData::create("* * * * JAN-MAR,DEC *");
REQUIRE(c.is_valid());
REQUIRE(has_value_range(c.months, 1, 3));
REQUIRE_FALSE(has_any_in_range(c.months, 4, 11));
REQUIRE(has_value_range(c.months, 12, 12));
REQUIRE(has_value_range(c.get_months(), 1, 3));
REQUIRE_FALSE(has_any_in_range(c.get_months(), 4, 11));
REQUIRE(has_value_range(c.get_months(), 12, 12));
}
AND_THEN("Range is valid")
{
auto c = CronTime::create("* * * * JAN-MAR,DEC FRI,MON,THU");
auto c = CronData::create("* * * * JAN-MAR,DEC FRI,MON,THU");
REQUIRE(c.is_valid());
REQUIRE(has_value_range(c.months, 1, 3));
REQUIRE_FALSE(has_any_in_range(c.months, 4, 11));
REQUIRE(has_value_range(c.months, 12, 12));
REQUIRE(has_value_range(c.day_of_week, 5, 5));
REQUIRE(has_value_range(c.day_of_week, 1, 1));
REQUIRE(has_value_range(c.day_of_week, 4, 4));
REQUIRE_FALSE(has_any_in_range(c.day_of_week, 0, 0));
REQUIRE_FALSE(has_any_in_range(c.day_of_week, 2, 3));
REQUIRE_FALSE(has_any_in_range(c.day_of_week, 6, 6));
REQUIRE(has_value_range(c.get_months(), 1, 3));
REQUIRE_FALSE(has_any_in_range(c.get_months(), 4, 11));
REQUIRE(has_value_range(c.get_months(), 12, 12));
REQUIRE(has_value_range(c.get_day_of_week(), 5, 5));
REQUIRE(has_value_range(c.get_day_of_week(), 1, 1));
REQUIRE(has_value_range(c.get_day_of_week(), 4, 4));
REQUIRE_FALSE(has_any_in_range(c.get_day_of_week(), 0, 0));
REQUIRE_FALSE(has_any_in_range(c.get_day_of_week(), 2, 3));
REQUIRE_FALSE(has_any_in_range(c.get_day_of_week(), 6, 6));
}
}
AND_WHEN("Using backward range")
{
THEN("Range is valid")
{
auto c = CronTime::create("* * * * APR-JAN *");
auto c = CronData::create("* * * * APR-JAN *");
REQUIRE(c.is_valid());
REQUIRE(has_value_range(c.months, 4, 12));
REQUIRE(has_value_range(c.months, 1, 1));
REQUIRE_FALSE(has_any_in_range(c.months, 2, 3));
REQUIRE(has_value_range(c.get_months(), 4, 12));
REQUIRE(has_value_range(c.get_months(), 1, 1));
REQUIRE_FALSE(has_any_in_range(c.get_months(), 2, 3));
}
AND_THEN("Range is valid")
{
auto c = CronTime::create("* * * * * sat-tue,wed");
auto c = CronData::create("* * * * * sat-tue,wed");
REQUIRE(c.is_valid());
REQUIRE(has_value_range(c.day_of_week, 6, 6)); // Has saturday
REQUIRE(has_value_range(c.day_of_week, 0, 3)); // Has sun, mon, tue, wed
REQUIRE_FALSE(has_any_in_range(c.day_of_week, 4, 5)); // Does not have thu or fri.
REQUIRE(has_value_range(c.get_day_of_week(), 6, 6)); // Has saturday
REQUIRE(has_value_range(c.get_day_of_week(), 0, 3)); // Has sun, mon, tue, wed
REQUIRE_FALSE(has_any_in_range(c.get_day_of_week(), 4, 5)); // Does not have thu or fri.
}
}
}
@ -205,22 +205,46 @@ SCENARIO("Using step syntax")
{
THEN("Range is valid")
{
auto c = CronTime::create("* * * * JAN/2 *");
auto c = CronData::create("* * * * JAN/2 *");
REQUIRE(c.is_valid());
REQUIRE(has_value_range(c.months, 1, 1));
REQUIRE(has_value_range(c.months, 3, 3));
REQUIRE(has_value_range(c.months, 5, 5));
REQUIRE(has_value_range(c.months, 7, 7));
REQUIRE(has_value_range(c.months, 9, 9));
REQUIRE(has_value_range(c.months, 11, 11));
REQUIRE_FALSE(has_any_in_range(c.months, 2, 2));
REQUIRE_FALSE(has_any_in_range(c.months, 4, 4));
REQUIRE_FALSE(has_any_in_range(c.months, 6, 6));
REQUIRE_FALSE(has_any_in_range(c.months, 8, 8));
REQUIRE_FALSE(has_any_in_range(c.months, 10, 10));
REQUIRE_FALSE(has_any_in_range(c.months, 12, 12));
REQUIRE(has_value_range(c.get_months(), 1, 1));
REQUIRE(has_value_range(c.get_months(), 3, 3));
REQUIRE(has_value_range(c.get_months(), 5, 5));
REQUIRE(has_value_range(c.get_months(), 7, 7));
REQUIRE(has_value_range(c.get_months(), 9, 9));
REQUIRE(has_value_range(c.get_months(), 11, 11));
REQUIRE_FALSE(has_any_in_range(c.get_months(), 2, 2));
REQUIRE_FALSE(has_any_in_range(c.get_months(), 4, 4));
REQUIRE_FALSE(has_any_in_range(c.get_months(), 6, 6));
REQUIRE_FALSE(has_any_in_range(c.get_months(), 8, 8));
REQUIRE_FALSE(has_any_in_range(c.get_months(), 10, 10));
REQUIRE_FALSE(has_any_in_range(c.get_months(), 12, 12));
}
}
}
}
SCENARIO("Calculating next runtime")
{
GIVEN("An item schedule for the top of every hour")
{
auto c = CronData::create("0 0 * * * *");
REQUIRE(c.is_valid());
WHEN("Start time is midnight")
{
// std::chrono::system_clock::time_point run_time = c.calculate_from(midnight);
// THEN("Next runtime is 01:00")
// {
// 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.)
// }
}
}
}