From 8c144b0ddb994965cdc58a9c6d79f1670f10bbdd Mon Sep 17 00:00:00 2001 From: Philemon Benner <56920372+BestITUserEUW@users.noreply.github.com> Date: Fri, 10 Oct 2025 16:14:27 +0200 Subject: [PATCH] fixed convenience scheduling (#47) --- README.md | 12 ++++++------ libcron/src/CronData.cpp | 12 ++++++------ test/CronDataTest.cpp | 9 +++++++++ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index d06f4c6..a6b874c 100644 --- a/README.md +++ b/README.md @@ -210,12 +210,12 @@ These special time specification tokens which replace the 5 initial time and dat |Token|Meaning | --- | --- | -| @yearly | Run once a year, ie. "0 0 1 1 *". -| @annually | Run once a year, ie. "0 0 1 1 *". -| @monthly | Run once a month, ie. "0 0 1 * *". -| @weekly | Run once a week, ie. "0 0 * * 0". -| @daily | Run once a day, ie. "0 0 * * *". -| @hourly | Run once an hour, ie. "0 * * * *". +| @yearly | Run once a year, ie. "0 0 0 1 1 *". +| @annually | Run once a year, ie. "0 0 0 1 1 *"". +| @monthly | Run once a month, ie. "0 0 0 1 * *". +| @weekly | Run once a week, ie. "0 0 0 * * 0". +| @daily | Run once a day, ie. "0 0 0 * * ?". +| @hourly | Run once an hour, ie. "0 0 * * * ?". # Randomization diff --git a/libcron/src/CronData.cpp b/libcron/src/CronData.cpp index 777a2c0..7b747a1 100644 --- a/libcron/src/CronData.cpp +++ b/libcron/src/CronData.cpp @@ -39,12 +39,12 @@ namespace libcron { // First, check for "convenience scheduling" using @yearly, @annually, // @monthly, @weekly, @daily or @hourly. - std::string tmp = std::regex_replace(cron_expression, std::regex("@yearly"), "0 0 1 1 *"); - tmp = std::regex_replace(tmp, std::regex("@annually"), "0 0 1 1 *"); - tmp = std::regex_replace(tmp, std::regex("@monthly"), "0 0 1 * *"); - tmp = std::regex_replace(tmp, std::regex("@weekly"), "0 0 * * 0"); - tmp = std::regex_replace(tmp, std::regex("@daily"), "0 0 * * *"); - const std::string expression = std::regex_replace(tmp, std::regex("@hourly"), "0 * * * *"); + std::string tmp = std::regex_replace(cron_expression, std::regex("@yearly"), "0 0 0 1 1 *"); + tmp = std::regex_replace(tmp, std::regex("@annually"), "0 0 0 1 1 *"); + tmp = std::regex_replace(tmp, std::regex("@monthly"), "0 0 0 1 * *"); + tmp = std::regex_replace(tmp, std::regex("@weekly"), "0 0 0 * * 0"); + tmp = std::regex_replace(tmp, std::regex("@daily"), "0 0 0 * * ?"); + const std::string expression = std::regex_replace(tmp, std::regex("@hourly"), "0 0 * * * ?"); // Second, split on white-space. We expect six parts. std::regex split{ R"#(^\s*(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s*$)#", diff --git a/test/CronDataTest.cpp b/test/CronDataTest.cpp index faaa5dc..84db2f8 100644 --- a/test/CronDataTest.cpp +++ b/test/CronDataTest.cpp @@ -237,4 +237,13 @@ SCENARIO("Replacing text with numbers") std::string s = "JAN-DEC"; REQUIRE(CronData::replace_string_name_with_numeric(s) == "1-12"); } +} + +SCENARIO("Parsing @ expressions works") { + REQUIRE(CronData::create("@yearly").is_valid()); + REQUIRE(CronData::create("@annually").is_valid()); + REQUIRE(CronData::create("@monthly").is_valid()); + REQUIRE(CronData::create("@weekly").is_valid()); + REQUIRE(CronData::create("@daily").is_valid()); + REQUIRE(CronData::create("@hourly").is_valid()); } \ No newline at end of file