mirror of
https://github.com/pandorafms/pandorafms.git
synced 2025-07-29 08:45:12 +02:00
[Windows Agent] Added cron function isInCron
This commit is contained in:
parent
40ca8afe37
commit
b566ca29e3
@ -54,20 +54,112 @@ Cron::Cron (string cron_string) {
|
|||||||
|
|
||||||
// Wildcard
|
// Wildcard
|
||||||
if (cron_params[i][0] == '*') {
|
if (cron_params[i][0] == '*') {
|
||||||
this->params[i][0] = -1;
|
this->params[i][CRDOWN] = CR_WILDCARD_VALUE;
|
||||||
this->params[i][1] = -1;
|
this->params[i][1] = CR_WILDCARD_VALUE;
|
||||||
|
|
||||||
// Interval
|
// Interval
|
||||||
} else if (sscanf (cron_params[i], "%255[^-]-%255s", bottom, top) == 2) {
|
} else if (sscanf (cron_params[i], "%255[^-]-%255s", bottom, top) == 2) {
|
||||||
this->params[i][0] = (i != 2) ? atoi (bottom) : atoi (bottom) - 1;
|
// Check if there is an interval with two equal values
|
||||||
this->params[i][1] = (i != 2) ? atoi (top) : atoi (top) -1;
|
if (strcmp(bottom, top) == 0) {
|
||||||
|
this->params[i][CRDOWN] = (i != 3)
|
||||||
|
? atoi (cron_params[i])
|
||||||
|
: atoi (cron_params[i]) - 1;
|
||||||
|
this->params[i][CRUP] = CR_WILDCARD_VALUE;
|
||||||
|
} else {
|
||||||
|
this->params[i][CRDOWN] = (i != 3) ? atoi (bottom) : atoi (bottom) - 1;
|
||||||
|
this->params[i][CRUP] = (i != 3) ? atoi (top) : atoi (top) -1;
|
||||||
|
}
|
||||||
|
|
||||||
// Single value
|
// Single value
|
||||||
} else {
|
} else {
|
||||||
this->params[i][0] = (i != 2)
|
this->params[i][CRDOWN] = (i != 3)
|
||||||
? atoi (cron_params[i])
|
? atoi (cron_params[i])
|
||||||
: atoi (cron_params[i]) - 1;
|
: atoi (cron_params[i]) - 1;
|
||||||
this->params[i][1] = -1;
|
this->params[i][CRUP] = CR_WILDCARD_VALUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @brief Given a date, return if is inside a cron string or not
|
||||||
|
*
|
||||||
|
* @param date Date to check
|
||||||
|
* @return true If is inside cron
|
||||||
|
* @return false If is outside cron
|
||||||
|
*/
|
||||||
|
bool Cron::isInCron(time_t date) {
|
||||||
|
struct tm * timeinfo = localtime(&date);
|
||||||
|
|
||||||
|
// Convert the tm struct to an array
|
||||||
|
int date_array[4] = {
|
||||||
|
timeinfo->tm_min,
|
||||||
|
timeinfo->tm_hour,
|
||||||
|
timeinfo->tm_mday,
|
||||||
|
timeinfo->tm_mon
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check all positions faliures
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
if (!isWildCard(i)) {
|
||||||
|
if (isSingleValue(i)) {
|
||||||
|
if (this->params[i][CRDOWN] != date_array[i]) return false;
|
||||||
|
} else {
|
||||||
|
if (isNormalInterval(i)) {
|
||||||
|
if (
|
||||||
|
date_array[i] < this->params[i][CRDOWN] ||
|
||||||
|
date_array[i] > this->params[i][CRUP]
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (
|
||||||
|
date_array[i] < this->params[i][CRDOWN] &&
|
||||||
|
date_array[i] > this->params[i][CRUP]
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no failures, date is inside cron.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Check if a cron position is a wildcard
|
||||||
|
*
|
||||||
|
* @param position Position inside the param array
|
||||||
|
* @return true if is a wildcard
|
||||||
|
*/
|
||||||
|
bool Cron::isWildCard(int position) {
|
||||||
|
return this->params[position][CRDOWN] == CR_WILDCARD_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Check if a cron position is a single value
|
||||||
|
*
|
||||||
|
* @param position Position inside the param array
|
||||||
|
* @return true if is a single value
|
||||||
|
*/
|
||||||
|
bool Cron::isSingleValue(int position) {
|
||||||
|
return this->params[position][CRUP] == CR_WILDCARD_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Check if a cron position is an interval with down lower than up
|
||||||
|
*
|
||||||
|
* @param position Position inside the param array
|
||||||
|
* @return true if is a normal interval
|
||||||
|
* @return false if is an inverse interval
|
||||||
|
*/
|
||||||
|
bool Cron::isNormalInterval (int position) {
|
||||||
|
// Wildcard and single value will be treated like normal interval
|
||||||
|
if (
|
||||||
|
this->params[position][CRDOWN] == CR_WILDCARD_VALUE ||
|
||||||
|
this->params[position][CRUP] == CR_WILDCARD_VALUE
|
||||||
|
) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return (this->params[position][CRUP] >= this->params[position][CRDOWN]);
|
||||||
|
}
|
@ -29,6 +29,9 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
const string CRON_DEFAULT_STRING = "* * * * *";
|
const string CRON_DEFAULT_STRING = "* * * * *";
|
||||||
|
const int CR_WILDCARD_VALUE = -1;
|
||||||
|
const int CRDOWN = 0;
|
||||||
|
const int CRUP = 1;
|
||||||
|
|
||||||
class Cron {
|
class Cron {
|
||||||
private:
|
private:
|
||||||
@ -49,7 +52,10 @@ class Cron {
|
|||||||
string cronString;
|
string cronString;
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
bool validDate(int cronDate[5]);
|
bool isInCron(time_t date);
|
||||||
|
bool isWildCard(int position);
|
||||||
|
bool isSingleValue(int position);
|
||||||
|
bool isNormalInterval(int position);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
|
Loading…
x
Reference in New Issue
Block a user