LegacyTimePeriod: support specifying seconds

This commit is contained in:
Alexander A. Klimov 2019-08-22 18:50:37 +02:00
parent 8c739fe33b
commit 28f708bb7c
2 changed files with 68 additions and 73 deletions

View File

@ -337,6 +337,28 @@ bool LegacyTimePeriod::IsInDayDefinition(const String& daydef, tm *reference)
return IsInTimeRange(&begin, &end, stride, reference);
}
static inline
void ProcessTimeRaw(const String& in, tm *reference, tm *out)
{
*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]);
}
void LegacyTimePeriod::ProcessTimeRangeRaw(const String& timerange, tm *reference, tm *begin, tm *end)
{
std::vector<String> times = timerange.Split("-");
@ -344,25 +366,8 @@ void LegacyTimePeriod::ProcessTimeRangeRaw(const String& timerange, tm *referenc
if (times.size() != 2)
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid timerange: " + timerange));
std::vector<String> hd1 = times[0].Split(":");
if (hd1.size() != 2)
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid time specification: " + times[0]));
std::vector<String> hd2 = times[1].Split(":");
if (hd2.size() != 2)
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid time specification: " + times[1]));
*begin = *reference;
begin->tm_sec = 0;
begin->tm_min = Convert::ToLong(hd1[1]);
begin->tm_hour = Convert::ToLong(hd1[0]);
*end = *reference;
end->tm_sec = 0;
end->tm_min = Convert::ToLong(hd2[1]);
end->tm_hour = Convert::ToLong(hd2[0]);
ProcessTimeRaw(times[0], reference, begin);
ProcessTimeRaw(times[1], reference, end);
if (begin->tm_hour * 3600 + begin->tm_min * 60 + begin->tm_sec >=
end->tm_hour * 3600 + end->tm_min * 60 + end->tm_sec)

View File

@ -199,6 +199,38 @@ BOOST_AUTO_TEST_CASE(simple)
BOOST_CHECK_EQUAL(end, expectedEnd);
}
struct DateTime
{
struct {
int Year, Month, Day;
} Date;
struct {
int Hour, Minute, Second;
} Time;
};
static inline
void AdvancedHelper(const char *timestamp, DateTime from, DateTime to)
{
using boost::gregorian::date;
using boost::posix_time::ptime;
using boost::posix_time::ptime_from_tm;
using boost::posix_time::time_duration;
tm tm_beg, tm_end, tm_ref;
tm_ref.tm_year = from.Date.Year - 1900;
tm_ref.tm_mon = from.Date.Month - 1;
tm_ref.tm_mday = from.Date.Day;
// Run test
LegacyTimePeriod::ProcessTimeRangeRaw(timestamp, &tm_ref, &tm_beg, &tm_end);
// Compare times
BOOST_CHECK_EQUAL(ptime_from_tm(tm_beg), ptime(date(from.Date.Year, from.Date.Month, from.Date.Day), time_duration(from.Time.Hour, from.Time.Minute, from.Time.Second)));
BOOST_CHECK_EQUAL(ptime_from_tm(tm_end), ptime(date(to.Date.Year, to.Date.Month, to.Date.Day), time_duration(to.Time.Hour, to.Time.Minute, to.Time.Second)));
}
BOOST_AUTO_TEST_CASE(advanced)
{
tm tm_beg, tm_end, tm_ref;
@ -211,68 +243,26 @@ BOOST_AUTO_TEST_CASE(advanced)
//-----------------------------------------------------
// 2019-05-06 where Icinga celebrates 10 years #monitoringlove
// 2019-05-06 22:00:00 - 2019-05-07 06:00:00
timestamp = "22:00-06:00";
tm_ref.tm_year = 2019 - 1900;
tm_ref.tm_mon = 5 - 1;
tm_ref.tm_mday = 6;
expectedBegin = boost::posix_time::ptime(boost::gregorian::date(2019, 5, 6), boost::posix_time::time_duration(22, 0, 0));
expectedEnd = boost::posix_time::ptime(boost::gregorian::date(2019, 5, 7), boost::posix_time::time_duration(6, 0, 0));
// Run test
LegacyTimePeriod::ProcessTimeRangeRaw(timestamp, &tm_ref, &tm_beg, &tm_end);
// Compare times
begin = boost::posix_time::ptime_from_tm(tm_beg);
end = boost::posix_time::ptime_from_tm(tm_end);
BOOST_CHECK_EQUAL(begin, expectedBegin);
BOOST_CHECK_EQUAL(end, expectedEnd);
AdvancedHelper("22:00-06:00", {{2019, 5, 6}, {22, 0, 0}}, {{2019, 5, 7}, {6, 0, 0}});
AdvancedHelper("22:00:01-06:00", {{2019, 5, 6}, {22, 0, 1}}, {{2019, 5, 7}, {6, 0, 0}});
AdvancedHelper("22:00-06:00:02", {{2019, 5, 6}, {22, 0, 0}}, {{2019, 5, 7}, {6, 0, 2}});
AdvancedHelper("22:00:03-06:00:04", {{2019, 5, 6}, {22, 0, 3}}, {{2019, 5, 7}, {6, 0, 4}});
//-----------------------------------------------------
// 2019-05-06 Icinga is unleashed.
// 09:00:00 - 17:00:00
timestamp = "09:00-17:00";
tm_ref.tm_year = 2009 - 1900;
tm_ref.tm_mon = 5 - 1;
tm_ref.tm_mday = 6;
expectedBegin = boost::posix_time::ptime(boost::gregorian::date(2009, 5, 6), boost::posix_time::time_duration(9, 0, 0));
expectedEnd = boost::posix_time::ptime(boost::gregorian::date(2009, 5, 6), boost::posix_time::time_duration(17, 0, 0));
// Run test
LegacyTimePeriod::ProcessTimeRangeRaw(timestamp, &tm_ref, &tm_beg, &tm_end);
// Compare times
begin = boost::posix_time::ptime_from_tm(tm_beg);
end = boost::posix_time::ptime_from_tm(tm_end);
BOOST_CHECK_EQUAL(begin, expectedBegin);
BOOST_CHECK_EQUAL(end, expectedEnd);
AdvancedHelper("09:00-17:00", {{2009, 5, 6}, {9, 0, 0}}, {{2009, 5, 6}, {17, 0, 0}});
AdvancedHelper("09:00:01-17:00", {{2009, 5, 6}, {9, 0, 1}}, {{2009, 5, 6}, {17, 0, 0}});
AdvancedHelper("09:00-17:00:02", {{2009, 5, 6}, {9, 0, 0}}, {{2009, 5, 6}, {17, 0, 2}});
AdvancedHelper("09:00:03-17:00:04", {{2009, 5, 6}, {9, 0, 3}}, {{2009, 5, 6}, {17, 0, 4}});
//-----------------------------------------------------
// At our first Icinga Camp in SFO 2014 at GitHub HQ, we partied all night long with an overflow.
// 2014-09-24 09:00:00 - 2014-09-25 06:00:00
timestamp = "09:00-30:00";
tm_ref.tm_year = 2014 - 1900;
tm_ref.tm_mon = 9 - 1;
tm_ref.tm_mday = 24;
expectedBegin = boost::posix_time::ptime(boost::gregorian::date(2014, 9, 24), boost::posix_time::time_duration(9, 0, 0));
expectedEnd = boost::posix_time::ptime(boost::gregorian::date(2014, 9, 25), boost::posix_time::time_duration(6, 0, 0));
// Run test
LegacyTimePeriod::ProcessTimeRangeRaw(timestamp, &tm_ref, &tm_beg, &tm_end);
// Compare times
begin = boost::posix_time::ptime_from_tm(tm_beg);
end = boost::posix_time::ptime_from_tm(tm_end);
BOOST_CHECK_EQUAL(begin, expectedBegin);
BOOST_CHECK_EQUAL(end, expectedEnd);
AdvancedHelper("09:00-30:00", {{2014, 9, 24}, {9, 0, 0}}, {{2014, 9, 25}, {6, 0, 0}});
AdvancedHelper("09:00:01-30:00", {{2014, 9, 24}, {9, 0, 1}}, {{2014, 9, 25}, {6, 0, 0}});
AdvancedHelper("09:00-30:00:02", {{2014, 9, 24}, {9, 0, 0}}, {{2014, 9, 25}, {6, 0, 2}});
AdvancedHelper("09:00:03-30:00:04", {{2014, 9, 24}, {9, 0, 3}}, {{2014, 9, 25}, {6, 0, 4}});
}
BOOST_AUTO_TEST_SUITE_END()