2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-04-15 12:48:12 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/legacytimeperiod.hpp"
|
2015-01-21 08:47:45 +01:00
|
|
|
#include "base/function.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/debug.hpp"
|
|
|
|
#include "base/utility.hpp"
|
2013-04-15 12:48:12 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
REGISTER_FUNCTION_NONCONST(Internal, LegacyTimePeriod, &LegacyTimePeriod::ScriptFunc, "tp:begin:end");
|
2013-04-15 12:48:12 +02:00
|
|
|
|
2021-07-20 11:55:39 +02:00
|
|
|
/**
|
|
|
|
* Returns the same as mktime() but does not modify its argument and takes a const pointer.
|
|
|
|
*
|
|
|
|
* @param t struct tm to convert to time_t
|
|
|
|
* @return time_t representing the timestamp given by t
|
|
|
|
*/
|
|
|
|
static time_t mktime_const(const tm *t) {
|
|
|
|
tm copy = *t;
|
|
|
|
return mktime(©);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LegacyTimePeriod::IsInTimeRange(const tm *begin, const tm *end, int stride, const tm *reference)
|
2013-04-17 14:26:04 +02:00
|
|
|
{
|
|
|
|
time_t tsbegin, tsend, tsref;
|
2021-07-20 11:55:39 +02:00
|
|
|
tsbegin = mktime_const(begin);
|
|
|
|
tsend = mktime_const(end);
|
|
|
|
tsref = mktime_const(reference);
|
2013-04-17 14:26:04 +02:00
|
|
|
|
2024-01-29 08:28:31 +01:00
|
|
|
if (tsref < tsbegin || tsref >= tsend)
|
2013-04-17 14:26:04 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
int daynumber = (tsref - tsbegin) / (24 * 60 * 60);
|
|
|
|
|
2018-11-01 23:51:42 +01:00
|
|
|
if (stride > 1 && daynumber % stride > 0)
|
2013-04-17 14:26:04 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-20 11:55:39 +02:00
|
|
|
/**
|
|
|
|
* Update all day-related fields of reference (tm_year, tm_mon, tm_mday, tm_wday, tm_yday) to reference the n-th
|
|
|
|
* occurrence of a weekday (given by wday) in the month represented by the original value of reference.
|
|
|
|
*
|
|
|
|
* If n is negative, counting is done from the end of the month, so for example with wday=1 and n=-1, the result will be
|
|
|
|
* the last Monday in the month given by reference.
|
|
|
|
*
|
|
|
|
* @param wday Weekday (0 = Sunday, 1 = Monday, ..., 6 = Saturday, like tm_wday)
|
|
|
|
* @param n Search the n-th weekday (given by wday) in the month given by reference
|
|
|
|
* @param reference Input for the current month and output for the given day of that moth
|
|
|
|
*/
|
2013-04-17 14:26:04 +02:00
|
|
|
void LegacyTimePeriod::FindNthWeekday(int wday, int n, tm *reference)
|
|
|
|
{
|
2021-07-20 11:55:39 +02:00
|
|
|
// Work on a copy to only update specific fields of reference (as documented).
|
|
|
|
tm t = *reference;
|
|
|
|
|
2013-05-08 12:48:45 +02:00
|
|
|
int dir, seen = 0;
|
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
dir = 1;
|
2024-08-02 16:27:15 +02:00
|
|
|
|
|
|
|
/* Postitive days are relative to the first day of the month. */
|
|
|
|
t.tm_mday = 1;
|
2013-05-08 12:48:45 +02:00
|
|
|
} else {
|
|
|
|
n *= -1;
|
|
|
|
dir = -1;
|
|
|
|
|
2024-08-02 16:27:15 +02:00
|
|
|
/* Negative days are relative to the last day of the month which is
|
|
|
|
* what mktime() normalizes the 0th day of the next month to. */
|
2021-07-20 11:55:39 +02:00
|
|
|
t.tm_mon++;
|
2024-08-02 16:27:15 +02:00
|
|
|
t.tm_mday = 0;
|
2013-05-08 12:48:45 +02:00
|
|
|
}
|
2013-04-17 14:26:04 +02:00
|
|
|
|
|
|
|
ASSERT(n > 0);
|
|
|
|
|
|
|
|
for (;;) {
|
2021-07-20 11:55:39 +02:00
|
|
|
// Always operate on 00:00:00 with automatic DST detection, otherwise days could
|
|
|
|
// be skipped or counted twice if +-24 hours is not on the next or previous day.
|
|
|
|
t.tm_hour = 0;
|
|
|
|
t.tm_min = 0;
|
|
|
|
t.tm_sec = 0;
|
|
|
|
t.tm_isdst = -1;
|
2013-04-17 14:26:04 +02:00
|
|
|
|
2021-07-20 11:55:39 +02:00
|
|
|
mktime(&t);
|
|
|
|
|
|
|
|
if (t.tm_wday == wday) {
|
2013-04-17 14:26:04 +02:00
|
|
|
seen++;
|
|
|
|
|
|
|
|
if (seen == n)
|
2021-07-20 11:55:39 +02:00
|
|
|
break;
|
2013-04-17 14:26:04 +02:00
|
|
|
}
|
|
|
|
|
2021-07-20 11:55:39 +02:00
|
|
|
t.tm_mday += dir;
|
2013-04-17 14:26:04 +02:00
|
|
|
}
|
2021-07-20 11:55:39 +02:00
|
|
|
|
|
|
|
reference->tm_year = t.tm_year;
|
|
|
|
reference->tm_mon = t.tm_mon;
|
|
|
|
reference->tm_mday = t.tm_mday;
|
|
|
|
reference->tm_wday = t.tm_wday;
|
|
|
|
reference->tm_yday = t.tm_yday;
|
2013-04-17 14:26:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int LegacyTimePeriod::WeekdayFromString(const String& daydef)
|
|
|
|
{
|
|
|
|
if (daydef == "sunday")
|
|
|
|
return 0;
|
|
|
|
else if (daydef == "monday")
|
|
|
|
return 1;
|
|
|
|
else if (daydef == "tuesday")
|
|
|
|
return 2;
|
|
|
|
else if (daydef == "wednesday")
|
|
|
|
return 3;
|
|
|
|
else if (daydef == "thursday")
|
|
|
|
return 4;
|
|
|
|
else if (daydef == "friday")
|
|
|
|
return 5;
|
|
|
|
else if (daydef == "saturday")
|
|
|
|
return 6;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LegacyTimePeriod::MonthFromString(const String& monthdef)
|
|
|
|
{
|
|
|
|
if (monthdef == "january")
|
|
|
|
return 0;
|
|
|
|
else if (monthdef == "february")
|
|
|
|
return 1;
|
|
|
|
else if (monthdef == "march")
|
|
|
|
return 2;
|
|
|
|
else if (monthdef == "april")
|
|
|
|
return 3;
|
|
|
|
else if (monthdef == "may")
|
|
|
|
return 4;
|
|
|
|
else if (monthdef == "june")
|
|
|
|
return 5;
|
|
|
|
else if (monthdef == "july")
|
|
|
|
return 6;
|
|
|
|
else if (monthdef == "august")
|
|
|
|
return 7;
|
|
|
|
else if (monthdef == "september")
|
|
|
|
return 8;
|
|
|
|
else if (monthdef == "october")
|
|
|
|
return 9;
|
|
|
|
else if (monthdef == "november")
|
|
|
|
return 10;
|
|
|
|
else if (monthdef == "december")
|
|
|
|
return 11;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:13:54 +02:00
|
|
|
boost::gregorian::date LegacyTimePeriod::GetEndOfMonthDay(int year, int month)
|
|
|
|
{
|
|
|
|
boost::gregorian::date d(boost::gregorian::greg_year(year), boost::gregorian::greg_month(month), 1);
|
|
|
|
|
|
|
|
return d.end_of_month();
|
|
|
|
}
|
|
|
|
|
2021-07-20 11:55:39 +02:00
|
|
|
/**
|
|
|
|
* Finds the first day on or after the day given by reference and writes the beginning and end time of that day to
|
|
|
|
* the output parameters begin and end.
|
|
|
|
*
|
|
|
|
* @param timespec Day to find, for example "2021-10-20", "sunday", ...
|
|
|
|
* @param begin if != nullptr, set to 00:00:00 on that day
|
|
|
|
* @param end if != nullptr, set to 24:00:00 on that day (i.e. 00:00:00 of the next day)
|
|
|
|
* @param reference Time to begin the search at
|
|
|
|
*/
|
|
|
|
void LegacyTimePeriod::ParseTimeSpec(const String& timespec, tm *begin, tm *end, const tm *reference)
|
2013-04-17 14:26:04 +02:00
|
|
|
{
|
|
|
|
/* YYYY-MM-DD */
|
|
|
|
if (timespec.GetLength() == 10 && timespec[4] == '-' && timespec[7] == '-') {
|
|
|
|
int year = Convert::ToLong(timespec.SubStr(0, 4));
|
|
|
|
int month = Convert::ToLong(timespec.SubStr(5, 2));
|
2013-11-13 14:56:31 +01:00
|
|
|
int day = Convert::ToLong(timespec.SubStr(8, 2));
|
2013-04-17 14:26:04 +02:00
|
|
|
|
2016-02-09 19:04:37 +01:00
|
|
|
if (month < 1 || month > 12)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid month in time specification: " + timespec));
|
|
|
|
if (day < 1 || day > 31)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid day in time specification: " + timespec));
|
|
|
|
|
2013-04-17 14:26:04 +02:00
|
|
|
if (begin) {
|
2016-05-11 09:23:55 +02:00
|
|
|
*begin = *reference;
|
2013-04-17 14:26:04 +02:00
|
|
|
begin->tm_year = year - 1900;
|
2016-02-09 19:04:37 +01:00
|
|
|
begin->tm_mon = month - 1;
|
|
|
|
begin->tm_mday = day;
|
2013-04-17 14:26:04 +02:00
|
|
|
begin->tm_hour = 0;
|
|
|
|
begin->tm_min = 0;
|
|
|
|
begin->tm_sec = 0;
|
2021-07-20 11:55:39 +02:00
|
|
|
begin->tm_isdst = -1;
|
2013-04-17 14:26:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (end) {
|
2016-05-11 09:23:55 +02:00
|
|
|
*end = *reference;
|
2013-04-17 14:26:04 +02:00
|
|
|
end->tm_year = year - 1900;
|
2016-02-09 19:04:37 +01:00
|
|
|
end->tm_mon = month - 1;
|
|
|
|
end->tm_mday = day;
|
2013-04-17 14:26:04 +02:00
|
|
|
end->tm_hour = 24;
|
|
|
|
end->tm_min = 0;
|
|
|
|
end->tm_sec = 0;
|
2021-07-20 11:55:39 +02:00
|
|
|
end->tm_isdst = -1;
|
2013-04-17 14:26:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-04 18:24:45 +01:00
|
|
|
std::vector<String> tokens = timespec.Split(" ");
|
2013-04-17 14:26:04 +02:00
|
|
|
|
2013-05-03 12:44:27 +02:00
|
|
|
int mon = -1;
|
2013-05-03 12:31:11 +02:00
|
|
|
|
|
|
|
if (tokens.size() > 1 && (tokens[0] == "day" || (mon = MonthFromString(tokens[0])) != -1)) {
|
|
|
|
if (mon == -1)
|
|
|
|
mon = reference->tm_mon;
|
|
|
|
|
2013-04-17 14:26:04 +02:00
|
|
|
int mday = Convert::ToLong(tokens[1]);
|
|
|
|
|
|
|
|
if (begin) {
|
|
|
|
*begin = *reference;
|
2013-05-03 12:31:11 +02:00
|
|
|
begin->tm_mon = mon;
|
2013-04-17 14:26:04 +02:00
|
|
|
begin->tm_mday = mday;
|
|
|
|
begin->tm_hour = 0;
|
|
|
|
begin->tm_min = 0;
|
|
|
|
begin->tm_sec = 0;
|
2021-07-20 11:55:39 +02:00
|
|
|
begin->tm_isdst = -1;
|
2013-04-17 14:26:04 +02:00
|
|
|
|
2019-07-09 16:13:54 +02:00
|
|
|
/* day -X: Negative days are relative to the next month. */
|
2013-04-17 14:26:04 +02:00
|
|
|
if (mday < 0) {
|
2019-07-09 16:13:54 +02:00
|
|
|
boost::gregorian::date d(GetEndOfMonthDay(reference->tm_year + 1900, mon + 1)); //TODO: Refactor this mess into full Boost.DateTime
|
|
|
|
|
2019-07-09 16:21:07 +02:00
|
|
|
//Depending on the number, we need to substract specific days (counting starts at 0).
|
|
|
|
d = d - boost::gregorian::days(mday * -1 - 1);
|
|
|
|
|
2019-07-09 16:13:54 +02:00
|
|
|
*begin = boost::gregorian::to_tm(d);
|
|
|
|
begin->tm_hour = 0;
|
|
|
|
begin->tm_min = 0;
|
|
|
|
begin->tm_sec = 0;
|
2013-04-17 14:26:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end) {
|
|
|
|
*end = *reference;
|
2013-05-03 12:31:11 +02:00
|
|
|
end->tm_mon = mon;
|
2013-04-17 14:26:04 +02:00
|
|
|
end->tm_mday = mday;
|
|
|
|
end->tm_hour = 24;
|
|
|
|
end->tm_min = 0;
|
|
|
|
end->tm_sec = 0;
|
2021-07-20 11:55:39 +02:00
|
|
|
end->tm_isdst = -1;
|
2013-04-17 14:26:04 +02:00
|
|
|
|
2019-07-09 16:13:54 +02:00
|
|
|
/* day -X: Negative days are relative to the next month. */
|
2013-04-17 14:26:04 +02:00
|
|
|
if (mday < 0) {
|
2019-07-09 16:13:54 +02:00
|
|
|
boost::gregorian::date d(GetEndOfMonthDay(reference->tm_year + 1900, mon + 1)); //TODO: Refactor this mess into full Boost.DateTime
|
|
|
|
|
2019-07-09 16:21:07 +02:00
|
|
|
//Depending on the number, we need to substract specific days (counting starts at 0).
|
|
|
|
d = d - boost::gregorian::days(mday * -1 - 1);
|
|
|
|
|
2019-07-09 16:13:54 +02:00
|
|
|
// End date is one day in the future, starting 00:00:00
|
|
|
|
d = d + boost::gregorian::days(1);
|
|
|
|
|
|
|
|
*end = boost::gregorian::to_tm(d);
|
|
|
|
end->tm_hour = 0;
|
|
|
|
end->tm_min = 0;
|
|
|
|
end->tm_sec = 0;
|
2013-04-17 14:26:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int wday;
|
|
|
|
|
2013-04-18 08:20:16 +02:00
|
|
|
if (tokens.size() >= 1 && (wday = WeekdayFromString(tokens[0])) != -1) {
|
2013-04-17 14:26:04 +02:00
|
|
|
tm myref = *reference;
|
2021-07-20 11:55:39 +02:00
|
|
|
myref.tm_isdst = -1;
|
2013-04-17 14:26:04 +02:00
|
|
|
|
|
|
|
if (tokens.size() > 2) {
|
2013-05-03 12:31:11 +02:00
|
|
|
mon = MonthFromString(tokens[2]);
|
2013-04-17 14:26:04 +02:00
|
|
|
|
|
|
|
if (mon == -1)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid month in time specification: " + timespec));
|
|
|
|
|
|
|
|
myref.tm_mon = mon;
|
|
|
|
}
|
|
|
|
|
2013-05-03 12:44:27 +02:00
|
|
|
int n = 0;
|
2013-04-18 08:20:16 +02:00
|
|
|
|
|
|
|
if (tokens.size() > 1)
|
|
|
|
n = Convert::ToLong(tokens[1]);
|
2013-04-17 14:26:04 +02:00
|
|
|
|
|
|
|
if (begin) {
|
|
|
|
*begin = myref;
|
2013-04-18 08:20:16 +02:00
|
|
|
|
|
|
|
if (tokens.size() > 1)
|
|
|
|
FindNthWeekday(wday, n, begin);
|
|
|
|
else
|
2014-12-20 22:04:24 +01:00
|
|
|
begin->tm_mday += (7 - begin->tm_wday + wday) % 7;
|
2013-04-18 08:20:16 +02:00
|
|
|
|
2013-04-17 14:26:04 +02:00
|
|
|
begin->tm_hour = 0;
|
|
|
|
begin->tm_min = 0;
|
|
|
|
begin->tm_sec = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end) {
|
|
|
|
*end = myref;
|
2013-04-18 08:20:16 +02:00
|
|
|
|
|
|
|
if (tokens.size() > 1)
|
|
|
|
FindNthWeekday(wday, n, end);
|
|
|
|
else
|
2014-12-20 22:04:24 +01:00
|
|
|
end->tm_mday += (7 - end->tm_wday + wday) % 7;
|
2013-04-18 08:20:16 +02:00
|
|
|
|
2013-04-17 14:26:04 +02:00
|
|
|
end->tm_hour = 0;
|
|
|
|
end->tm_min = 0;
|
|
|
|
end->tm_sec = 0;
|
|
|
|
end->tm_mday++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid time specification: " + timespec));
|
|
|
|
}
|
|
|
|
|
2021-07-20 11:55:39 +02:00
|
|
|
/**
|
|
|
|
* Parse a range of days.
|
|
|
|
*
|
|
|
|
* The input can have the following formats:
|
|
|
|
* begin
|
|
|
|
* begin - end
|
|
|
|
* begin / stride
|
|
|
|
* begin - end / stride
|
|
|
|
*
|
|
|
|
* @param timerange Text representation of a day range or a single day, for example "2021-10-20", "monday - friday", ...
|
|
|
|
* @param begin Output parameter set to 00:00:00 of the first day of the range
|
|
|
|
* @param end Output parameter set to 24:00:00 of the last day of the range (i.e. 00:00:00 of the day after)
|
|
|
|
* @param stride Output parameter for the stride (for every n-th day)
|
|
|
|
* @param reference Expand the range relative to this timestamp
|
|
|
|
*/
|
|
|
|
void LegacyTimePeriod::ParseTimeRange(const String& timerange, tm *begin, tm *end, int *stride, const tm *reference)
|
2013-04-17 14:26:04 +02:00
|
|
|
{
|
|
|
|
String def = timerange;
|
|
|
|
|
|
|
|
/* Figure out the stride. */
|
|
|
|
size_t pos = def.FindFirstOf('/');
|
|
|
|
|
|
|
|
if (pos != String::NPos) {
|
2015-08-27 18:06:20 +02:00
|
|
|
String strStride = def.SubStr(pos + 1).Trim();
|
2013-04-17 14:26:04 +02:00
|
|
|
*stride = Convert::ToLong(strStride);
|
|
|
|
|
|
|
|
/* Remove the stride parameter from the definition. */
|
|
|
|
def = def.SubStr(0, pos);
|
|
|
|
} else {
|
|
|
|
*stride = 1; /* User didn't specify anything, assume default. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Figure out whether the user has specified two dates. */
|
|
|
|
pos = def.Find("- ");
|
|
|
|
|
|
|
|
if (pos != String::NPos) {
|
2015-08-27 18:06:20 +02:00
|
|
|
String first = def.SubStr(0, pos).Trim();
|
2013-04-17 14:26:04 +02:00
|
|
|
|
2015-08-27 18:06:20 +02:00
|
|
|
String second = def.SubStr(pos + 1).Trim();
|
2013-04-17 14:26:04 +02:00
|
|
|
|
2017-12-14 15:37:20 +01:00
|
|
|
ParseTimeSpec(first, begin, nullptr, reference);
|
2013-04-17 14:26:04 +02:00
|
|
|
|
|
|
|
/* If the second definition starts with a number we need
|
|
|
|
* to add the first word from the first definition, e.g.:
|
|
|
|
* day 1 - 15 --> "day 15" */
|
|
|
|
bool is_number = true;
|
|
|
|
size_t xpos = second.FindFirstOf(' ');
|
|
|
|
String fword = second.SubStr(0, xpos);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Convert::ToLong(fword);
|
|
|
|
} catch (...) {
|
|
|
|
is_number = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_number) {
|
|
|
|
xpos = first.FindFirstOf(' ');
|
|
|
|
ASSERT(xpos != String::NPos);
|
|
|
|
second = first.SubStr(0, xpos + 1) + second;
|
|
|
|
}
|
|
|
|
|
2017-12-14 15:37:20 +01:00
|
|
|
ParseTimeSpec(second, nullptr, end, reference);
|
2013-04-17 14:26:04 +02:00
|
|
|
} else {
|
2015-03-29 08:13:11 +02:00
|
|
|
ParseTimeSpec(def, begin, end, reference);
|
2013-04-17 14:26:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 11:55:39 +02:00
|
|
|
bool LegacyTimePeriod::IsInDayDefinition(const String& daydef, const tm *reference)
|
2013-04-15 12:48:12 +02:00
|
|
|
{
|
2013-04-17 14:26:04 +02:00
|
|
|
tm begin, end;
|
|
|
|
int stride;
|
|
|
|
|
|
|
|
ParseTimeRange(daydef, &begin, &end, &stride, reference);
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "LegacyTimePeriod")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "ParseTimeRange: '" << daydef << "' => " << mktime(&begin)
|
|
|
|
<< " -> " << mktime(&end) << ", stride: " << stride;
|
2013-06-24 15:01:23 +02:00
|
|
|
|
2013-04-17 14:26:04 +02:00
|
|
|
return IsInTimeRange(&begin, &end, stride, reference);
|
2013-04-15 12:48:12 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 18:50:37 +02:00
|
|
|
static inline
|
2021-07-20 11:55:39 +02:00
|
|
|
void ProcessTimeRaw(const String& in, const tm *reference, tm *out)
|
2019-08-22 18:50:37 +02:00
|
|
|
{
|
|
|
|
*out = *reference;
|
|
|
|
|
|
|
|
auto hd (in.Split(":"));
|
|
|
|
|
|
|
|
switch (hd.size()) {
|
|
|
|
case 2:
|
|
|
|
out->tm_sec = 0;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
out->tm_sec = Convert::ToLong(hd[2]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid time specification: " + in));
|
|
|
|
}
|
|
|
|
|
|
|
|
out->tm_hour = Convert::ToLong(hd[0]);
|
|
|
|
out->tm_min = Convert::ToLong(hd[1]);
|
|
|
|
}
|
|
|
|
|
2021-07-20 11:55:39 +02:00
|
|
|
void LegacyTimePeriod::ProcessTimeRangeRaw(const String& timerange, const tm *reference, tm *begin, tm *end)
|
2013-04-15 12:48:12 +02:00
|
|
|
{
|
2018-01-04 18:24:45 +01:00
|
|
|
std::vector<String> times = timerange.Split("-");
|
2013-04-15 12:48:12 +02:00
|
|
|
|
|
|
|
if (times.size() != 2)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid timerange: " + timerange));
|
|
|
|
|
2019-08-22 18:50:37 +02:00
|
|
|
ProcessTimeRaw(times[0], reference, begin);
|
|
|
|
ProcessTimeRaw(times[1], reference, end);
|
2015-03-29 08:05:01 +02:00
|
|
|
|
|
|
|
if (begin->tm_hour * 3600 + begin->tm_min * 60 + begin->tm_sec >=
|
2017-12-19 15:50:05 +01:00
|
|
|
end->tm_hour * 3600 + end->tm_min * 60 + end->tm_sec)
|
2019-03-12 15:26:08 +01:00
|
|
|
end->tm_hour += 24;
|
2013-08-09 11:07:55 +02:00
|
|
|
}
|
2013-04-15 12:48:12 +02:00
|
|
|
|
2021-07-20 11:55:39 +02:00
|
|
|
Dictionary::Ptr LegacyTimePeriod::ProcessTimeRange(const String& timestamp, const tm *reference)
|
2013-08-09 11:07:55 +02:00
|
|
|
{
|
|
|
|
tm begin, end;
|
2015-02-12 09:12:55 +01:00
|
|
|
|
2015-03-29 08:13:11 +02:00
|
|
|
ProcessTimeRangeRaw(timestamp, reference, &begin, &end);
|
2015-02-12 09:12:55 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
return new Dictionary({
|
|
|
|
{ "begin", (long)mktime(&begin) },
|
|
|
|
{ "end", (long)mktime(&end) }
|
|
|
|
});
|
2013-04-15 12:48:12 +02:00
|
|
|
}
|
|
|
|
|
2021-07-20 11:55:39 +02:00
|
|
|
/**
|
|
|
|
* Takes a list of timeranges end expands them to concrete timestamp based on a reference time.
|
|
|
|
*
|
|
|
|
* @param timeranges String of comma separated time ranges, for example "10:00-12:00", "12:15:30-12:23:43,16:00-18:00"
|
|
|
|
* @param reference Starting point for searching the segments
|
|
|
|
* @param result For each range, a dict with keys "begin" and "end" is added
|
|
|
|
*/
|
|
|
|
void LegacyTimePeriod::ProcessTimeRanges(const String& timeranges, const tm *reference, const Array::Ptr& result)
|
2013-04-15 12:48:12 +02:00
|
|
|
{
|
2018-01-04 18:24:45 +01:00
|
|
|
std::vector<String> ranges = timeranges.Split(",");
|
2013-04-15 12:48:12 +02:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& range : ranges) {
|
2015-03-29 08:13:11 +02:00
|
|
|
Dictionary::Ptr segment = ProcessTimeRange(range, reference);
|
2015-02-12 09:12:55 +01:00
|
|
|
|
2015-01-29 14:14:56 +01:00
|
|
|
if (segment->Get("begin") >= segment->Get("end"))
|
2015-03-29 08:05:01 +02:00
|
|
|
continue;
|
2015-01-29 14:14:56 +01:00
|
|
|
|
2013-04-15 12:48:12 +02:00
|
|
|
result->Add(segment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 11:55:39 +02:00
|
|
|
Dictionary::Ptr LegacyTimePeriod::FindRunningSegment(const String& daydef, const String& timeranges, const tm *reference)
|
2018-10-18 18:42:21 +02:00
|
|
|
{
|
|
|
|
tm begin, end, iter;
|
|
|
|
time_t tsend, tsiter, tsref;
|
|
|
|
int stride;
|
|
|
|
|
2021-07-20 11:55:39 +02:00
|
|
|
tsref = mktime_const(reference);
|
2018-10-18 18:42:21 +02:00
|
|
|
|
|
|
|
ParseTimeRange(daydef, &begin, &end, &stride, reference);
|
|
|
|
|
|
|
|
iter = begin;
|
|
|
|
|
|
|
|
tsend = mktime(&end);
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (IsInTimeRange(&begin, &end, stride, &iter)) {
|
|
|
|
Array::Ptr segments = new Array();
|
|
|
|
ProcessTimeRanges(timeranges, &iter, segments);
|
|
|
|
|
|
|
|
Dictionary::Ptr bestSegment;
|
2019-03-08 14:07:29 +01:00
|
|
|
double bestEnd = 0.0;
|
2018-10-18 18:42:21 +02:00
|
|
|
|
|
|
|
ObjectLock olock(segments);
|
|
|
|
for (const Dictionary::Ptr& segment : segments) {
|
|
|
|
double begin = segment->Get("begin");
|
|
|
|
double end = segment->Get("end");
|
|
|
|
|
|
|
|
if (begin >= tsref || end < tsref)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!bestSegment || end > bestEnd) {
|
|
|
|
bestSegment = segment;
|
|
|
|
bestEnd = end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bestSegment)
|
|
|
|
return bestSegment;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter.tm_mday++;
|
|
|
|
iter.tm_hour = 0;
|
|
|
|
iter.tm_min = 0;
|
|
|
|
iter.tm_sec = 0;
|
|
|
|
tsiter = mktime(&iter);
|
|
|
|
} while (tsiter < tsend);
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-07-20 11:55:39 +02:00
|
|
|
Dictionary::Ptr LegacyTimePeriod::FindNextSegment(const String& daydef, const String& timeranges, const tm *reference)
|
2013-11-13 14:56:31 +01:00
|
|
|
{
|
|
|
|
tm begin, end, iter, ref;
|
|
|
|
time_t tsend, tsiter, tsref;
|
|
|
|
int stride;
|
|
|
|
|
|
|
|
for (int pass = 1; pass <= 2; pass++) {
|
|
|
|
if (pass == 1) {
|
|
|
|
ref = *reference;
|
|
|
|
} else {
|
|
|
|
ref = end;
|
|
|
|
ref.tm_mday++;
|
|
|
|
}
|
|
|
|
|
|
|
|
tsref = mktime(&ref);
|
|
|
|
|
|
|
|
ParseTimeRange(daydef, &begin, &end, &stride, &ref);
|
|
|
|
|
|
|
|
iter = begin;
|
|
|
|
|
|
|
|
tsend = mktime(&end);
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (IsInTimeRange(&begin, &end, stride, &iter)) {
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr segments = new Array();
|
2013-11-13 14:56:31 +01:00
|
|
|
ProcessTimeRanges(timeranges, &iter, segments);
|
|
|
|
|
|
|
|
Dictionary::Ptr bestSegment;
|
|
|
|
double bestBegin;
|
|
|
|
|
2014-04-06 08:09:49 +02:00
|
|
|
ObjectLock olock(segments);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Ptr& segment : segments) {
|
2013-11-13 14:56:31 +01:00
|
|
|
double begin = segment->Get("begin");
|
|
|
|
|
|
|
|
if (begin < tsref)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!bestSegment || begin < bestBegin) {
|
|
|
|
bestSegment = segment;
|
|
|
|
bestBegin = begin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bestSegment)
|
|
|
|
return bestSegment;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter.tm_mday++;
|
|
|
|
iter.tm_hour = 0;
|
|
|
|
iter.tm_min = 0;
|
|
|
|
iter.tm_sec = 0;
|
|
|
|
tsiter = mktime(&iter);
|
|
|
|
} while (tsiter < tsend);
|
|
|
|
}
|
|
|
|
|
2017-11-30 08:36:35 +01:00
|
|
|
return nullptr;
|
2013-11-13 14:56:31 +01:00
|
|
|
}
|
|
|
|
|
2013-04-15 12:48:12 +02:00
|
|
|
Array::Ptr LegacyTimePeriod::ScriptFunc(const TimePeriod::Ptr& tp, double begin, double end)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr segments = new Array();
|
2013-04-15 12:48:12 +02:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
Dictionary::Ptr ranges = tp->GetRanges();
|
2013-04-15 12:48:12 +02:00
|
|
|
|
2013-04-16 10:55:23 +02:00
|
|
|
if (ranges) {
|
2021-10-26 13:21:49 +02:00
|
|
|
tm tm_begin = Utility::LocalTime(begin);
|
|
|
|
|
|
|
|
// Always evaluate time periods for full days as their ranges are given per day.
|
|
|
|
tm_begin.tm_hour = 0;
|
|
|
|
tm_begin.tm_min = 0;
|
|
|
|
tm_begin.tm_sec = 0;
|
|
|
|
tm_begin.tm_isdst = -1;
|
|
|
|
|
|
|
|
// Helper to move a struct tm to midnight of the next day for the loop below.
|
|
|
|
// Due to DST changes, this may move the time by something else than 24 hours.
|
|
|
|
auto advance_to_next_day = [](tm *t) {
|
|
|
|
t->tm_mday++;
|
|
|
|
t->tm_hour = 0;
|
|
|
|
t->tm_min = 0;
|
|
|
|
t->tm_sec = 0;
|
|
|
|
t->tm_isdst = -1;
|
|
|
|
|
|
|
|
// Normalize fields using mktime.
|
|
|
|
mktime(t);
|
|
|
|
|
|
|
|
// Reset tm_isdst so that future calls figure out the correct time zone after setting tm_hour/tm_min/tm_sec.
|
|
|
|
t->tm_isdst = -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (tm reference = tm_begin; mktime_const(&reference) <= end; advance_to_next_day(&reference)) {
|
2013-04-15 12:48:12 +02:00
|
|
|
|
2014-12-19 12:19:28 +01:00
|
|
|
#ifdef I2_DEBUG
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "LegacyTimePeriod")
|
2021-10-26 13:21:49 +02:00
|
|
|
<< "Checking reference time " << mktime_const(&reference);
|
2014-12-19 12:19:28 +01:00
|
|
|
#endif /* I2_DEBUG */
|
2013-04-15 12:48:12 +02:00
|
|
|
|
2013-04-16 10:12:53 +02:00
|
|
|
ObjectLock olock(ranges);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : ranges) {
|
2013-11-30 17:42:50 +01:00
|
|
|
if (!IsInDayDefinition(kv.first, &reference)) {
|
2014-12-19 12:19:28 +01:00
|
|
|
#ifdef I2_DEBUG
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "LegacyTimePeriod")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Not in day definition '" << kv.first << "'.";
|
2014-12-19 12:19:28 +01:00
|
|
|
#endif /* I2_DEBUG */
|
2013-04-15 12:48:12 +02:00
|
|
|
continue;
|
2013-06-24 15:01:23 +02:00
|
|
|
}
|
|
|
|
|
2014-12-19 12:19:28 +01:00
|
|
|
#ifdef I2_DEBUG
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "LegacyTimePeriod")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "In day definition '" << kv.first << "'.";
|
2014-12-19 12:19:28 +01:00
|
|
|
#endif /* I2_DEBUG */
|
2013-04-15 12:48:12 +02:00
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
ProcessTimeRanges(kv.second, &reference, segments);
|
2013-04-15 12:48:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "LegacyTimePeriod")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Legacy timeperiod update returned " << segments->GetLength() << " segments.";
|
2013-04-15 12:48:12 +02:00
|
|
|
|
|
|
|
return segments;
|
|
|
|
}
|