Fix parsing of "day -X (last day of month)" in TimePeriod class

This commit is contained in:
Michael Friedrich 2019-07-09 16:13:54 +02:00
parent 7665143afa
commit 0dc87668d6
2 changed files with 27 additions and 6 deletions

View File

@ -113,6 +113,13 @@ int LegacyTimePeriod::MonthFromString(const String& monthdef)
return -1;
}
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();
}
void LegacyTimePeriod::ParseTimeSpec(const String& timespec, tm *begin, tm *end, tm *reference)
{
/* Let mktime() figure out whether we're in DST or not. */
@ -170,10 +177,14 @@ void LegacyTimePeriod::ParseTimeSpec(const String& timespec, tm *begin, tm *end,
begin->tm_min = 0;
begin->tm_sec = 0;
/* day -1: Negative days are relative to the next month. */
/* day -X: Negative days are relative to the next month. */
if (mday < 0) {
begin->tm_mday = mday * -1 - 1;
begin->tm_mon++;
boost::gregorian::date d(GetEndOfMonthDay(reference->tm_year + 1900, mon + 1)); //TODO: Refactor this mess into full Boost.DateTime
*begin = boost::gregorian::to_tm(d);
begin->tm_hour = 0;
begin->tm_min = 0;
begin->tm_sec = 0;
}
}
@ -185,10 +196,17 @@ void LegacyTimePeriod::ParseTimeSpec(const String& timespec, tm *begin, tm *end,
end->tm_min = 0;
end->tm_sec = 0;
/* day -1: Negative days are relative to the next month. */
/* day -X: Negative days are relative to the next month. */
if (mday < 0) {
end->tm_mday = mday * -1 - 1;
end->tm_mon++;
boost::gregorian::date d(GetEndOfMonthDay(reference->tm_year + 1900, mon + 1)); //TODO: Refactor this mess into full Boost.DateTime
// 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;
}
}

View File

@ -6,6 +6,7 @@
#include "icinga/i2-icinga.hpp"
#include "icinga/timeperiod.hpp"
#include "base/dictionary.hpp"
#include <boost/date_time/gregorian/gregorian.hpp>
namespace icinga
{
@ -35,6 +36,8 @@ public:
private:
LegacyTimePeriod();
static boost::gregorian::date GetEndOfMonthDay(int year, int month);
};
}