]> granicus.if.org Git - icinga2/commitdiff
TimePeriod: Fully support and test "day -X" notation 7302/head
authorMichael Friedrich <michael.friedrich@icinga.com>
Tue, 9 Jul 2019 14:21:07 +0000 (16:21 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Tue, 9 Jul 2019 14:21:07 +0000 (16:21 +0200)
Previously no tests would have detected if this really worked or not.

lib/icinga/legacytimeperiod.cpp
test/icinga-legacytimeperiod.cpp

index 039fedc78be2359faf95278efc0afcc49f57adfa..560a588ec62b29c527d057b9d899a94a665beadc 100644 (file)
@@ -181,6 +181,9 @@ void LegacyTimePeriod::ParseTimeSpec(const String& timespec, tm *begin, tm *end,
                        if (mday < 0) {
                                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;
@@ -200,6 +203,9 @@ void LegacyTimePeriod::ParseTimeSpec(const String& timespec, tm *begin, tm *end,
                        if (mday < 0) {
                                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);
 
index f27a5647748716150041e4949ef4fd9f9a071337..c7eb51b064f0359191f2162458a7ba86035f5000 100644 (file)
@@ -158,6 +158,26 @@ BOOST_AUTO_TEST_CASE(simple)
        BOOST_CHECK_EQUAL(begin, expectedBegin);
        BOOST_CHECK_EQUAL(end, expectedEnd);
 
+       //-----------------------------------------------------
+       // Third last day of the month
+       timestamp = "day -3";
+       tm_ref.tm_year = 2019 - 1900;
+       tm_ref.tm_mon = 7 - 1;
+
+       expectedBegin = boost::posix_time::ptime(boost::gregorian::date(2019, 7, 29), boost::posix_time::time_duration(0, 0, 0));
+
+       expectedEnd = boost::posix_time::ptime(boost::gregorian::date(2019, 7, 30), boost::posix_time::time_duration(0, 0, 0));
+
+       // Run Tests
+       LegacyTimePeriod::ParseTimeSpec(timestamp, &tm_beg, &tm_end, &tm_ref);
+
+       // 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);
+
        //-----------------------------------------------------
        // Leap year with the last day of the month
        timestamp = "day -1";