1 /******************************************************************************
3 * Copyright (C) 2012-2018 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 <BoostTestTargetConfig.h>
23 using namespace icinga;
25 BOOST_AUTO_TEST_SUITE(icinga_legacytimeperiod);
27 struct GlobalTimezoneFixture
31 GlobalTimezoneFixture()
35 _putenv_s("TZ", "UTC");
42 ~GlobalTimezoneFixture()
59 BOOST_GLOBAL_FIXTURE(GlobalTimezoneFixture);
61 BOOST_AUTO_TEST_CASE(simple)
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);
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);
74 BOOST_CHECK_THROW(LegacyTimePeriod::ParseTimeSpec("2015-12-32", &beg, &end, &ref),
75 std::invalid_argument);
77 BOOST_CHECK_THROW(LegacyTimePeriod::ParseTimeSpec("2015-28-01", &beg, &end, &ref),
78 std::invalid_argument);
80 // check parsing of "day X" and "day -X" specs
81 ref.tm_year = 2016 - 1900;
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
87 ref.tm_year = 2018 - 1900;
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
93 ref.tm_year = 2012 - 1900;
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
99 ref.tm_year = 2016 - 1900; // leap year
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
106 BOOST_AUTO_TEST_SUITE_END()