1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
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. *
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. *
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 ******************************************************************************/
20 #include "icinga/legacytimeperiod.hpp"
21 #include <boost/test/unit_test.hpp>
23 using namespace icinga;
25 BOOST_AUTO_TEST_SUITE(icinga_legacytimeperiod);
27 struct GlobalTimezoneFixture
31 GlobalTimezoneFixture(void)
38 ~GlobalTimezoneFixture(void)
48 BOOST_GLOBAL_FIXTURE(GlobalTimezoneFixture);
50 BOOST_AUTO_TEST_CASE(simple)
55 // check parsing of "YYYY-MM-DD" specs
56 LegacyTimePeriod::ParseTimeSpec("2016-01-01", &beg, &end, &ref);
57 BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1451606400);
58 BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1451692800);
60 LegacyTimePeriod::ParseTimeSpec("2015-12-31", &beg, &end, &ref);
61 BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1451520000);
62 BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1451606400);
64 BOOST_CHECK_THROW(LegacyTimePeriod::ParseTimeSpec("2015-12-32", &beg, &end, &ref),
65 std::invalid_argument);
67 BOOST_CHECK_THROW(LegacyTimePeriod::ParseTimeSpec("2015-28-01", &beg, &end, &ref),
68 std::invalid_argument);
70 // check parsing of "day X" and "day -X" specs
71 ref.tm_year = 2016 - 1900;
73 LegacyTimePeriod::ParseTimeSpec("day 2", &beg, &end, &ref);
74 BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1454371200); // 2016-02-02
75 BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1454457600); // 2016-02-03
77 ref.tm_year = 2018 - 1900;
79 LegacyTimePeriod::ParseTimeSpec("day 31", &beg, &end, &ref);
80 BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1546214400); // 2018-12-31
81 BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1546300800); // 2019-01-01
83 ref.tm_year = 2012 - 1900;
85 LegacyTimePeriod::ParseTimeSpec("day -1", &beg, &end, &ref);
86 BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1343692800); // 2012-07-31
87 BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1343779200); // 2012-08-01
89 ref.tm_year = 2016 - 1900; // leap year
91 LegacyTimePeriod::ParseTimeSpec("day -1", &beg, &end, &ref);
92 BOOST_CHECK_EQUAL(mktime(&beg), (time_t) 1456704000); // 2016-02-29
93 BOOST_CHECK_EQUAL(mktime(&end), (time_t) 1456790400); // 2016-03-01
96 BOOST_AUTO_TEST_SUITE_END()