]> granicus.if.org Git - icinga2/commitdiff
Fix parsing of "day -X (last day of month)" in TimePeriod class
authorMichael Friedrich <michael.friedrich@icinga.com>
Tue, 9 Jul 2019 14:13:54 +0000 (16:13 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Tue, 9 Jul 2019 14:16:43 +0000 (16:16 +0200)
lib/icinga/legacytimeperiod.cpp
lib/icinga/legacytimeperiod.hpp

index 1275ae351dd8d03cbf5f51da47da5cc2d5207bf9..039fedc78be2359faf95278efc0afcc49f57adfa 100644 (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;
                        }
                }
 
index 2b9767b2f78436192ad0506c0c0e1c372fd7ea79..3f1a5cdb97c374955d41dc2db293ecc8335b491b 100644 (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);
 };
 
 }