Skip to content

Commit

Permalink
Merge pull request #7302 from Icinga/bugfix/tests-tps
Browse files Browse the repository at this point in the history
Rewrite tests for Timeperiods with Boost.DateTime and fix "day -X" specification
  • Loading branch information
Michael Friedrich committed Jul 10, 2019
2 parents 0712589 + 88e5d8c commit b83e66a
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 89 deletions.
36 changes: 30 additions & 6 deletions lib/icinga/legacytimeperiod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -170,10 +177,17 @@ void LegacyTimePeriod::ParseTimeSpec(const String& timespec, tm *begin, tm *end,
begin->tm_min = 0;
begin->tm_sec = 0;

/* 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

//Depending on the number, we need to substract specific days (counting starts at 0).
d = d - boost::gregorian::days(mday * -1 - 1);

*begin = boost::gregorian::to_tm(d);
begin->tm_hour = 0;
begin->tm_min = 0;
begin->tm_sec = 0;
}
}

Expand All @@ -185,10 +199,20 @@ void LegacyTimePeriod::ParseTimeSpec(const String& timespec, tm *begin, tm *end,
end->tm_min = 0;
end->tm_sec = 0;

/* 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

//Depending on the number, we need to substract specific days (counting starts at 0).
d = d - boost::gregorian::days(mday * -1 - 1);

// 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;
}
}

Expand Down
3 changes: 3 additions & 0 deletions lib/icinga/legacytimeperiod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -35,6 +36,8 @@ class LegacyTimePeriod

private:
LegacyTimePeriod();

static boost::gregorian::date GetEndOfMonthDay(int year, int month);
};

}
Expand Down
Loading

0 comments on commit b83e66a

Please sign in to comment.