33 #include <boost/tokenizer.hpp>
39 using namespace boost;
43 parse_entry(spec, &m_entry);
49 int hour_increment = 0;
50 int day_increment = 0;
55 localtime_r(&now, &next_tm);
59 next_matching_day(&next_tm, day_increment);
62 for (i = next_tm.tm_hour + hour_increment; i < 24; i++) {
63 if (m_entry.hour[i]) {
64 if (i > next_tm.tm_hour) {
77 for (i = next_tm.tm_min; i < 60; i++) {
78 if (m_entry.minute[i]) {
80 return mktime(&next_tm);
89 return mktime(&next_tm);
94 bool advanced =
false;
100 t = mktime(next_tm) + 86400;
101 localtime_r(&t, next_tm);
106 if (m_entry.month[next_tm->tm_mon] &&
107 (m_entry.dom[(next_tm->tm_mday - 1)] || m_entry.dow[next_tm->tm_wday]))
109 t = mktime(next_tm) + 86400;
110 localtime_r(&t, next_tm);
116 next_tm->tm_hour = 0;
124 size_t field_count = 0;
125 bool wildcard_dom =
false;
126 bool wildcard_dow =
false;
129 char_separator<char> sep(
" ");
130 tokenizer< char_separator<char> > tokens(text, sep);
131 for (
const auto &t : tokens) {
132 if (field_count == 5)
134 fields[field_count++] = t;
142 char_separator<char> sep(
",");
143 tokenizer< char_separator<char> > tokens(fields[0], sep);
144 for (
const auto &t : tokens)
145 parse_range<60>(t, entry->
minute);
150 char_separator<char> sep(
",");
151 tokenizer< char_separator<char> > tokens(fields[1], sep);
152 for (
const auto &t : tokens)
153 parse_range<24>(t, entry->
hour);
158 if (fields[2] ==
"*")
161 char_separator<char> sep(
",");
162 tokenizer< char_separator<char> > tokens(fields[2], sep);
163 for (
const auto &t : tokens)
164 parse_range<31>(t, entry->
dom,
false);
170 char_separator<char> sep(
",");
171 tokenizer< char_separator<char> > tokens(fields[3], sep);
172 for (
const auto &t : tokens)
173 parse_range<12>(t, entry->
month,
false);
178 if (fields[4] ==
"*")
181 char_separator<char> sep(
",");
182 tokenizer< char_separator<char> > tokens(fields[4], sep);
183 for (
const auto &t : tokens)
184 parse_range<8>(t, entry->
dow);
186 entry->
dow[0] =
true;
188 entry->
dow[7] =
true;
215 char_separator<char> sep(
"/");
216 tokenizer< char_separator<char> > tokens(text, sep);
217 for (
const auto &t : tokens) {
230 for (
const char *ptr = step_str.c_str(); *ptr; ptr++) {
234 step = atoi(step_str.c_str());
241 bool got_wildcard =
false;
243 char_separator<char> sep(
"-");
244 tokenizer< char_separator<char> > tokens(range, sep);
245 for (
const auto &t : tokens) {
252 for (
const char *ptr = t.c_str(); *ptr; ptr++) {
256 start = atoi(t.c_str());
259 else if (count == 1) {
262 for (
const char *ptr = t.c_str(); *ptr; ptr++) {
266 end = atoi(t.c_str());
287 if (start >= (N+offset) || end >= (N+offset))
292 bits[start-offset] =
true;
297 for (
int i=(start-offset); i<=(end-offset); i+=step)
305 reconstruct_spec<60>(entry.
minute, spec);
307 reconstruct_spec<24>(entry.
hour, spec);
309 reconstruct_spec<31>(entry.
dom, spec);
311 reconstruct_spec<12>(entry.
month, spec);
313 reconstruct_spec<8>(entry.
dow, spec);
325 for (
int i=0; i<N; i++) {
332 else if (start != -1) {
337 spec +=
String(
"") + start;
339 spec +=
String(
"") + start +
"-" + end;
346 if (start == 0 && end == N-1)
350 spec +=
String(
"") + start;
352 spec +=
String(
"") + start +
"-" + end;
time_t next_event(time_t now)
Retrieves the timestamp of the next event.
std::string String
A String is simply a typedef to std::string.
void parse_entry(const String &spec, crontab_entry *_entry)
Parses a crontab spec into a crontab_entry.
Binary representation of crontab spec.
Logging routines and macros.
Compatibility Macros for C/C++.
std::ostream & operator<<(std::ostream &os, const crontab_entry &entry)
Helper function to write crontab_entry to an ostream.
void parse_range(const String &spec, std::bitset< N > &bits, bool zero_based=true)
Parses a crontab field and sets corresponding bits in bits.
Crontab()
Default constructor.
Crontab class for periodic events.
void next_matching_day(struct tm *next_tm, bool increment)
Determines next day on which event will occur.
void reconstruct_spec(const std::bitset< N > &bits, String &spec)
Converts binary crontab spec back into string spec.
String extensions and helpers: sets, maps, append operators etc.
#define HT_THROW(_code_, _msg_)