2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-03-13 11:04:53 -04:00
|
|
|
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "icinga/timeperiod.hpp"
|
2018-01-18 07:50:38 -05:00
|
|
|
#include "icinga/timeperiod-ti.cpp"
|
2015-02-12 03:12:55 -05:00
|
|
|
#include "icinga/legacytimeperiod.hpp"
|
2015-08-15 14:28:05 -04:00
|
|
|
#include "base/configtype.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/objectlock.hpp"
|
2015-02-12 03:12:55 -05:00
|
|
|
#include "base/exception.hpp"
|
2014-10-19 08:21:12 -04:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/timer.hpp"
|
|
|
|
|
#include "base/utility.hpp"
|
2017-11-21 08:07:44 -05:00
|
|
|
#include <boost/thread/once.hpp>
|
2013-03-13 11:04:53 -04:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
|
|
REGISTER_TYPE(TimePeriod);
|
|
|
|
|
|
2013-03-18 06:02:18 -04:00
|
|
|
static Timer::Ptr l_UpdateTimer;
|
2013-03-13 11:04:53 -04:00
|
|
|
|
2015-08-20 11:18:48 -04:00
|
|
|
void TimePeriod::Start(bool runtimeCreated)
|
2013-03-13 11:04:53 -04:00
|
|
|
{
|
2015-08-20 11:18:48 -04:00
|
|
|
ObjectImpl<TimePeriod>::Start(runtimeCreated);
|
2013-03-13 11:04:53 -04:00
|
|
|
|
2017-11-21 08:07:44 -05:00
|
|
|
static boost::once_flag once = BOOST_ONCE_INIT;
|
|
|
|
|
|
2023-03-31 06:38:24 -04:00
|
|
|
boost::call_once(once, [] {
|
2023-03-21 05:30:15 -04:00
|
|
|
l_UpdateTimer = Timer::Create();
|
2017-11-21 08:07:44 -05:00
|
|
|
l_UpdateTimer->SetInterval(300);
|
2021-01-18 08:29:05 -05:00
|
|
|
l_UpdateTimer->OnTimerExpired.connect([](const Timer * const&) { UpdateTimerHandler(); });
|
2017-11-21 08:07:44 -05:00
|
|
|
l_UpdateTimer->Start();
|
|
|
|
|
});
|
|
|
|
|
|
2013-03-13 11:04:53 -04:00
|
|
|
/* Pre-fill the time period for the next 24 hours. */
|
|
|
|
|
double now = Utility::GetTime();
|
2013-04-16 04:12:53 -04:00
|
|
|
UpdateRegion(now, now + 24 * 3600, true);
|
2016-08-20 17:46:44 -04:00
|
|
|
#ifdef _DEBUG
|
2013-04-16 04:12:53 -04:00
|
|
|
Dump();
|
2016-08-20 17:46:44 -04:00
|
|
|
#endif /* _DEBUG */
|
2013-03-13 11:04:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TimePeriod::AddSegment(double begin, double end)
|
|
|
|
|
{
|
|
|
|
|
ASSERT(OwnsLock());
|
|
|
|
|
|
2014-10-19 11:52:17 -04:00
|
|
|
Log(LogDebug, "TimePeriod")
|
2017-12-19 09:50:05 -05:00
|
|
|
<< "Adding segment '" << Utility::FormatDateTime("%c", begin) << "' <-> '"
|
|
|
|
|
<< Utility::FormatDateTime("%c", end) << "' to TimePeriod '" << GetName() << "'";
|
2013-09-18 18:01:18 -04:00
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
if (GetValidBegin().IsEmpty() || begin < GetValidBegin())
|
|
|
|
|
SetValidBegin(begin);
|
2013-03-13 11:04:53 -04:00
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
if (GetValidEnd().IsEmpty() || end > GetValidEnd())
|
|
|
|
|
SetValidEnd(end);
|
2013-03-13 11:04:53 -04:00
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
Array::Ptr segments = GetSegments();
|
2013-03-13 11:04:53 -04:00
|
|
|
|
|
|
|
|
if (segments) {
|
|
|
|
|
/* Try to merge the new segment into an existing segment. */
|
|
|
|
|
ObjectLock dlock(segments);
|
2023-03-31 06:36:37 -04:00
|
|
|
for (Dictionary::Ptr segment : segments) {
|
2013-03-13 11:04:53 -04:00
|
|
|
if (segment->Get("begin") <= begin && segment->Get("end") >= end)
|
|
|
|
|
return; /* New segment is fully contained in this segment. */
|
|
|
|
|
|
2016-03-25 07:55:11 -04:00
|
|
|
if (segment->Get("begin") >= begin && segment->Get("end") <= end) {
|
|
|
|
|
segment->Set("begin", begin);
|
|
|
|
|
segment->Set("end", end); /* Extend an existing segment to both sides */
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (segment->Get("end") >= begin && segment->Get("end") <= end) {
|
|
|
|
|
segment->Set("end", end); /* Extend an existing segment to right. */
|
2013-03-13 11:04:53 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-05 06:30:02 -05:00
|
|
|
if (segment->Get("begin") >= begin && segment->Get("begin") <= end) {
|
2016-03-25 07:55:11 -04:00
|
|
|
segment->Set("begin", begin); /* Extend an existing segment to left. */
|
2013-03-13 11:04:53 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2016-03-25 07:55:11 -04:00
|
|
|
|
2013-03-13 11:04:53 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Create new segment if we weren't able to merge this into an existing segment. */
|
2018-01-11 05:17:38 -05:00
|
|
|
Dictionary::Ptr segment = new Dictionary({
|
|
|
|
|
{ "begin", begin },
|
|
|
|
|
{ "end", end }
|
|
|
|
|
});
|
2013-03-13 11:04:53 -04:00
|
|
|
|
|
|
|
|
if (!segments) {
|
2014-11-08 15:17:16 -05:00
|
|
|
segments = new Array();
|
2013-10-26 03:41:45 -04:00
|
|
|
SetSegments(segments);
|
2013-03-13 11:04:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
segments->Add(segment);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-15 04:54:06 -04:00
|
|
|
void TimePeriod::AddSegment(const Dictionary::Ptr& segment)
|
|
|
|
|
{
|
|
|
|
|
AddSegment(segment->Get("begin"), segment->Get("end"));
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-13 11:04:53 -04:00
|
|
|
void TimePeriod::RemoveSegment(double begin, double end)
|
|
|
|
|
{
|
|
|
|
|
ASSERT(OwnsLock());
|
|
|
|
|
|
2014-10-19 11:52:17 -04:00
|
|
|
Log(LogDebug, "TimePeriod")
|
2017-12-19 09:50:05 -05:00
|
|
|
<< "Removing segment '" << Utility::FormatDateTime("%c", begin) << "' <-> '"
|
|
|
|
|
<< Utility::FormatDateTime("%c", end) << "' from TimePeriod '" << GetName() << "'";
|
2013-09-18 18:01:18 -04:00
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
if (GetValidBegin().IsEmpty() || begin < GetValidBegin())
|
|
|
|
|
SetValidBegin(begin);
|
2013-03-13 11:04:53 -04:00
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
if (GetValidEnd().IsEmpty() || end > GetValidEnd())
|
|
|
|
|
SetValidEnd(end);
|
2013-03-13 11:04:53 -04:00
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
Array::Ptr segments = GetSegments();
|
2013-03-13 11:04:53 -04:00
|
|
|
|
|
|
|
|
if (!segments)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-11-08 15:17:16 -05:00
|
|
|
Array::Ptr newSegments = new Array();
|
2013-03-14 02:29:53 -04:00
|
|
|
|
2013-03-13 11:04:53 -04:00
|
|
|
/* Try to split or adjust an existing segment. */
|
|
|
|
|
ObjectLock dlock(segments);
|
2023-03-31 06:36:37 -04:00
|
|
|
for (Dictionary::Ptr segment : segments) {
|
2013-03-14 02:29:53 -04:00
|
|
|
/* Fully contained in the specified range? */
|
|
|
|
|
if (segment->Get("begin") >= begin && segment->Get("end") <= end)
|
2018-05-16 06:04:38 -04:00
|
|
|
// Don't add the old segment, because the segment is fully contained into our range
|
2013-03-14 02:29:53 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
/* Not overlapping at all? */
|
|
|
|
|
if (segment->Get("end") < begin || segment->Get("begin") > end) {
|
|
|
|
|
newSegments->Add(segment);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 07:55:11 -04:00
|
|
|
/* Cut between */
|
|
|
|
|
if (segment->Get("begin") < begin && segment->Get("end") > end) {
|
2018-01-11 05:17:38 -05:00
|
|
|
newSegments->Add(new Dictionary({
|
|
|
|
|
{ "begin", segment->Get("begin") },
|
|
|
|
|
{ "end", begin }
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
newSegments->Add(new Dictionary({
|
|
|
|
|
{ "begin", end },
|
|
|
|
|
{ "end", segment->Get("end") }
|
|
|
|
|
}));
|
2018-05-16 06:04:38 -04:00
|
|
|
// Don't add the old segment, because we have now two new segments and a gap between
|
|
|
|
|
continue;
|
2016-03-25 07:55:11 -04:00
|
|
|
}
|
|
|
|
|
|
2013-03-14 03:18:19 -04:00
|
|
|
/* Adjust the begin/end timestamps so as to not overlap with the specified range. */
|
2013-09-18 18:01:18 -04:00
|
|
|
if (segment->Get("begin") > begin && segment->Get("begin") < end)
|
2013-03-14 03:18:19 -04:00
|
|
|
segment->Set("begin", end);
|
2013-03-14 02:29:53 -04:00
|
|
|
|
2013-09-18 18:01:18 -04:00
|
|
|
if (segment->Get("end") > begin && segment->Get("end") < end)
|
2013-03-14 03:18:19 -04:00
|
|
|
segment->Set("end", begin);
|
|
|
|
|
|
|
|
|
|
newSegments->Add(segment);
|
2013-03-13 11:04:53 -04:00
|
|
|
}
|
|
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
SetSegments(newSegments);
|
2013-09-18 18:01:18 -04:00
|
|
|
|
2016-08-20 17:46:44 -04:00
|
|
|
#ifdef _DEBUG
|
2013-09-18 18:01:18 -04:00
|
|
|
Dump();
|
2016-08-20 17:46:44 -04:00
|
|
|
#endif /* _DEBUG */
|
2013-03-13 11:04:53 -04:00
|
|
|
}
|
|
|
|
|
|
2016-03-25 07:55:11 -04:00
|
|
|
void TimePeriod::RemoveSegment(const Dictionary::Ptr& segment)
|
|
|
|
|
{
|
|
|
|
|
RemoveSegment(segment->Get("begin"), segment->Get("end"));
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-13 11:04:53 -04:00
|
|
|
void TimePeriod::PurgeSegments(double end)
|
|
|
|
|
{
|
|
|
|
|
ASSERT(OwnsLock());
|
|
|
|
|
|
2014-10-19 11:52:17 -04:00
|
|
|
Log(LogDebug, "TimePeriod")
|
2017-12-19 09:50:05 -05:00
|
|
|
<< "Purging segments older than '" << Utility::FormatDateTime("%c", end)
|
|
|
|
|
<< "' from TimePeriod '" << GetName() << "'";
|
2013-09-18 18:01:18 -04:00
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
if (GetValidBegin().IsEmpty() || end < GetValidBegin())
|
2013-03-13 11:04:53 -04:00
|
|
|
return;
|
|
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
SetValidBegin(end);
|
2013-03-13 11:04:53 -04:00
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
Array::Ptr segments = GetSegments();
|
2013-03-13 11:04:53 -04:00
|
|
|
|
|
|
|
|
if (!segments)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-11-08 15:17:16 -05:00
|
|
|
Array::Ptr newSegments = new Array();
|
2013-03-13 11:04:53 -04:00
|
|
|
|
|
|
|
|
/* Remove old segments. */
|
|
|
|
|
ObjectLock dlock(segments);
|
2023-03-31 06:36:37 -04:00
|
|
|
for (Dictionary::Ptr segment : segments) {
|
2013-03-13 11:04:53 -04:00
|
|
|
if (segment->Get("end") >= end)
|
|
|
|
|
newSegments->Add(segment);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
SetSegments(newSegments);
|
2013-03-13 11:04:53 -04:00
|
|
|
}
|
|
|
|
|
|
2016-03-25 07:55:11 -04:00
|
|
|
void TimePeriod::Merge(const TimePeriod::Ptr& timeperiod, bool include)
|
|
|
|
|
{
|
|
|
|
|
Log(LogDebug, "TimePeriod")
|
2017-12-19 09:50:05 -05:00
|
|
|
<< "Merge TimePeriod '" << GetName() << "' with '" << timeperiod->GetName() << "' "
|
|
|
|
|
<< "Method: " << (include ? "include" : "exclude");
|
2016-03-25 07:55:11 -04:00
|
|
|
|
|
|
|
|
Array::Ptr segments = timeperiod->GetSegments();
|
|
|
|
|
|
|
|
|
|
if (segments) {
|
|
|
|
|
ObjectLock dlock(segments);
|
|
|
|
|
ObjectLock ilock(this);
|
2023-03-31 06:36:37 -04:00
|
|
|
for (Dictionary::Ptr segment : segments) {
|
2016-03-25 07:55:11 -04:00
|
|
|
include ? AddSegment(segment) : RemoveSegment(segment);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-16 04:12:53 -04:00
|
|
|
void TimePeriod::UpdateRegion(double begin, double end, bool clearExisting)
|
2013-03-13 11:04:53 -04:00
|
|
|
{
|
2018-06-19 04:30:34 -04:00
|
|
|
if (clearExisting) {
|
2019-05-01 05:49:07 -04:00
|
|
|
ObjectLock olock(this);
|
2018-06-19 04:30:34 -04:00
|
|
|
SetSegments(new Array());
|
|
|
|
|
} else {
|
2013-10-26 03:41:45 -04:00
|
|
|
if (begin < GetValidEnd())
|
|
|
|
|
begin = GetValidEnd();
|
2013-03-13 11:04:53 -04:00
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
if (end < GetValidEnd())
|
2013-04-16 04:12:53 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2013-03-13 11:04:53 -04:00
|
|
|
|
2017-11-30 02:19:58 -05:00
|
|
|
Array::Ptr segments = GetUpdate()->Invoke({ this, begin, end });
|
2013-03-15 04:54:06 -04:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
RemoveSegment(begin, end);
|
|
|
|
|
|
2013-03-25 15:47:02 -04:00
|
|
|
if (segments) {
|
|
|
|
|
ObjectLock dlock(segments);
|
2023-03-31 06:36:37 -04:00
|
|
|
for (Dictionary::Ptr segment : segments) {
|
2013-03-25 15:47:02 -04:00
|
|
|
AddSegment(segment);
|
|
|
|
|
}
|
2013-03-15 04:54:06 -04:00
|
|
|
}
|
|
|
|
|
}
|
2016-03-25 07:55:11 -04:00
|
|
|
|
|
|
|
|
bool preferInclude = GetPreferIncludes();
|
|
|
|
|
|
|
|
|
|
/* First handle the non preferred timeranges */
|
|
|
|
|
Array::Ptr timeranges = preferInclude ? GetExcludes() : GetIncludes();
|
|
|
|
|
|
|
|
|
|
if (timeranges) {
|
|
|
|
|
ObjectLock olock(timeranges);
|
2023-03-31 06:36:37 -04:00
|
|
|
for (String name : timeranges) {
|
2016-03-25 07:55:11 -04:00
|
|
|
const TimePeriod::Ptr timeperiod = TimePeriod::GetByName(name);
|
|
|
|
|
|
|
|
|
|
if (timeperiod)
|
|
|
|
|
Merge(timeperiod, !preferInclude);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Preferred timeranges must be handled at the end */
|
|
|
|
|
timeranges = preferInclude ? GetIncludes() : GetExcludes();
|
|
|
|
|
|
|
|
|
|
if (timeranges) {
|
|
|
|
|
ObjectLock olock(timeranges);
|
2023-03-31 06:36:37 -04:00
|
|
|
for (String name : timeranges) {
|
2016-03-25 07:55:11 -04:00
|
|
|
const TimePeriod::Ptr timeperiod = TimePeriod::GetByName(name);
|
|
|
|
|
|
|
|
|
|
if (timeperiod)
|
|
|
|
|
Merge(timeperiod, preferInclude);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-13 11:04:53 -04:00
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
bool TimePeriod::GetIsInside() const
|
2015-02-23 08:06:32 -05:00
|
|
|
{
|
|
|
|
|
return IsInside(Utility::GetTime());
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-13 11:04:53 -04:00
|
|
|
bool TimePeriod::IsInside(double ts) const
|
|
|
|
|
{
|
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
if (GetValidBegin().IsEmpty() || ts < GetValidBegin() || GetValidEnd().IsEmpty() || ts > GetValidEnd())
|
2013-03-13 11:04:53 -04:00
|
|
|
return true; /* Assume that all invalid regions are "inside". */
|
|
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
Array::Ptr segments = GetSegments();
|
2013-03-13 11:04:53 -04:00
|
|
|
|
|
|
|
|
if (segments) {
|
|
|
|
|
ObjectLock dlock(segments);
|
2023-03-31 06:36:37 -04:00
|
|
|
for (Dictionary::Ptr segment : segments) {
|
2024-08-01 10:13:10 -04:00
|
|
|
if (ts >= segment->Get("begin") && ts < segment->Get("end"))
|
2013-03-13 11:04:53 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double TimePeriod::FindNextTransition(double begin)
|
|
|
|
|
{
|
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
Array::Ptr segments = GetSegments();
|
2013-03-13 11:04:53 -04:00
|
|
|
|
|
|
|
|
double closestTransition = -1;
|
|
|
|
|
|
|
|
|
|
if (segments) {
|
|
|
|
|
ObjectLock dlock(segments);
|
2023-03-31 06:36:37 -04:00
|
|
|
for (Dictionary::Ptr segment : segments) {
|
2013-03-13 11:04:53 -04:00
|
|
|
if (segment->Get("begin") > begin && (segment->Get("begin") < closestTransition || closestTransition == -1))
|
|
|
|
|
closestTransition = segment->Get("begin");
|
|
|
|
|
|
|
|
|
|
if (segment->Get("end") > begin && (segment->Get("end") < closestTransition || closestTransition == -1))
|
|
|
|
|
closestTransition = segment->Get("end");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return closestTransition;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void TimePeriod::UpdateTimerHandler()
|
2013-03-13 11:04:53 -04:00
|
|
|
{
|
|
|
|
|
double now = Utility::GetTime();
|
|
|
|
|
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const TimePeriod::Ptr& tp : ConfigType::GetObjectsByType<TimePeriod>()) {
|
2016-08-23 00:59:52 -04:00
|
|
|
if (!tp->IsActive())
|
|
|
|
|
continue;
|
|
|
|
|
|
2013-03-17 15:19:29 -04:00
|
|
|
double valid_end;
|
|
|
|
|
|
2013-03-14 07:21:10 -04:00
|
|
|
{
|
|
|
|
|
ObjectLock olock(tp);
|
|
|
|
|
tp->PurgeSegments(now - 3600);
|
2013-03-17 15:19:29 -04:00
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
valid_end = tp->GetValidEnd();
|
2013-03-14 07:21:10 -04:00
|
|
|
}
|
2013-03-13 11:04:53 -04:00
|
|
|
|
2013-04-16 04:12:53 -04:00
|
|
|
tp->UpdateRegion(valid_end, now + 24 * 3600, false);
|
2016-08-20 17:46:44 -04:00
|
|
|
#ifdef _DEBUG
|
2013-04-16 04:12:53 -04:00
|
|
|
tp->Dump();
|
2016-08-20 17:46:44 -04:00
|
|
|
#endif /* _DEBUG */
|
2013-03-13 11:04:53 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void TimePeriod::Dump()
|
2013-04-16 04:12:53 -04:00
|
|
|
{
|
2019-05-01 05:49:07 -04:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
Array::Ptr segments = GetSegments();
|
2013-04-16 04:12:53 -04:00
|
|
|
|
2014-10-19 11:52:17 -04:00
|
|
|
Log(LogDebug, "TimePeriod")
|
2017-12-19 09:50:05 -05:00
|
|
|
<< "Dumping TimePeriod '" << GetName() << "'";
|
2014-10-19 11:52:17 -04:00
|
|
|
|
|
|
|
|
Log(LogDebug, "TimePeriod")
|
2017-12-19 09:50:05 -05:00
|
|
|
<< "Valid from '" << Utility::FormatDateTime("%c", GetValidBegin())
|
|
|
|
|
<< "' until '" << Utility::FormatDateTime("%c", GetValidEnd());
|
2013-04-16 04:12:53 -04:00
|
|
|
|
|
|
|
|
if (segments) {
|
|
|
|
|
ObjectLock dlock(segments);
|
2023-03-31 06:36:37 -04:00
|
|
|
for (Dictionary::Ptr segment : segments) {
|
2014-10-19 11:52:17 -04:00
|
|
|
Log(LogDebug, "TimePeriod")
|
2017-12-19 09:50:05 -05:00
|
|
|
<< "Segment: " << Utility::FormatDateTime("%c", segment->Get("begin")) << " <-> "
|
|
|
|
|
<< Utility::FormatDateTime("%c", segment->Get("end"));
|
2013-04-16 04:12:53 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-28 07:45:45 -04:00
|
|
|
Log(LogDebug, "TimePeriod", "---");
|
2013-04-16 04:12:53 -04:00
|
|
|
}
|
2015-02-12 03:12:55 -05:00
|
|
|
|
2018-01-11 01:08:09 -05:00
|
|
|
void TimePeriod::ValidateRanges(const Lazy<Dictionary::Ptr>& lvalue, const ValidationUtils& utils)
|
2015-02-12 03:12:55 -05:00
|
|
|
{
|
2025-11-14 10:38:41 -05:00
|
|
|
ObjectImpl::ValidateRanges(lvalue, utils);
|
|
|
|
|
|
2018-01-11 01:08:09 -05:00
|
|
|
if (!lvalue())
|
2015-02-12 03:12:55 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* create a fake time environment to validate the definitions */
|
2015-03-03 03:56:38 -05:00
|
|
|
time_t refts = Utility::GetTime();
|
|
|
|
|
tm reference = Utility::LocalTime(refts);
|
2015-02-12 03:12:55 -05:00
|
|
|
Array::Ptr segments = new Array();
|
|
|
|
|
|
2018-01-11 01:08:09 -05:00
|
|
|
ObjectLock olock(lvalue());
|
|
|
|
|
for (const Dictionary::Pair& kv : lvalue()) {
|
2015-02-12 03:12:55 -05:00
|
|
|
try {
|
2015-03-03 03:56:38 -05:00
|
|
|
tm begin_tm, end_tm;
|
|
|
|
|
int stride;
|
|
|
|
|
LegacyTimePeriod::ParseTimeRange(kv.first, &begin_tm, &end_tm, &stride, &reference);
|
2015-03-29 02:13:11 -04:00
|
|
|
} catch (const std::exception& ex) {
|
2017-11-30 12:09:38 -05:00
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "ranges" }, "Invalid time specification '" + kv.first + "': " + ex.what()));
|
2015-02-12 03:12:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
LegacyTimePeriod::ProcessTimeRanges(kv.second, &reference, segments);
|
2015-03-29 02:13:11 -04:00
|
|
|
} catch (const std::exception& ex) {
|
2017-11-30 12:09:38 -05:00
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "ranges" }, "Invalid time range definition '" + kv.second + "': " + ex.what()));
|
2015-02-12 03:12:55 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|