]> granicus.if.org Git - icinga2/blob - test/icinga-legacytimeperiod.cpp
Merge pull request #5959 from Icinga/feature/api-pretty
[icinga2] / test / icinga-legacytimeperiod.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "icinga/legacytimeperiod.hpp"
21 #include <BoostTestTargetConfig.h>
22
23 using namespace icinga;
24
25 BOOST_AUTO_TEST_SUITE(icinga_legacytimeperiod);
26
27 struct GlobalTimezoneFixture
28 {
29         char *tz;
30
31         GlobalTimezoneFixture()
32         {
33                 tz = getenv("TZ");
34 #ifdef _WIN32
35                 _putenv_s("TZ", "UTC");
36 #else
37                 setenv("TZ", "", 1);
38 #endif
39                 tzset();
40         }
41
42         ~GlobalTimezoneFixture()
43         {
44 #ifdef _WIN32
45                 if (tz)
46                         _putenv_s("TZ", tz);
47                 else
48                         _putenv_s("TZ", "");
49 #else
50                 if (tz)
51                         setenv("TZ", tz, 1);
52                 else
53                         unsetenv("TZ");
54 #endif
55                 tzset();
56         }
57 };
58
59 BOOST_GLOBAL_FIXTURE(GlobalTimezoneFixture);
60
61 BOOST_AUTO_TEST_CASE(simple)
62 {
63         tm beg, end, ref;
64
65         // check parsing of "YYYY-MM-DD" specs
66         LegacyTimePeriod::ParseTimeSpec("2016-01-01", &beg, &end, &ref);
67         BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1451606400);
68         BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1451692800);
69
70         LegacyTimePeriod::ParseTimeSpec("2015-12-31", &beg, &end, &ref);
71         BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1451520000);
72         BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1451606400);
73
74         BOOST_CHECK_THROW(LegacyTimePeriod::ParseTimeSpec("2015-12-32", &beg, &end, &ref),
75             std::invalid_argument);
76
77         BOOST_CHECK_THROW(LegacyTimePeriod::ParseTimeSpec("2015-28-01", &beg, &end, &ref),
78             std::invalid_argument);
79
80         // check parsing of "day X" and "day -X" specs
81         ref.tm_year = 2016 - 1900;
82         ref.tm_mon = 1;
83         LegacyTimePeriod::ParseTimeSpec("day 2", &beg, &end, &ref);
84         BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1454371200); // 2016-02-02
85         BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1454457600); // 2016-02-03
86
87         ref.tm_year = 2018 - 1900;
88         ref.tm_mon = 11;
89         LegacyTimePeriod::ParseTimeSpec("day 31", &beg, &end, &ref);
90         BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1546214400); // 2018-12-31
91         BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1546300800); // 2019-01-01
92
93         ref.tm_year = 2012 - 1900;
94         ref.tm_mon = 6;
95         LegacyTimePeriod::ParseTimeSpec("day -1", &beg, &end, &ref);
96         BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1343692800); // 2012-07-31
97         BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1343779200); // 2012-08-01
98
99         ref.tm_year = 2016 - 1900; // leap year
100         ref.tm_mon = 1;
101         LegacyTimePeriod::ParseTimeSpec("day -1", &beg, &end, &ref);
102         BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1456704000); // 2016-02-29
103         BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1456790400); // 2016-03-01
104 }
105
106 BOOST_AUTO_TEST_SUITE_END()