1 // Copyright (C) 2015-2019 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 #include <config.h>
8
9 #include <util/boost_time_utils.h>
10
11 #include <string.h>
12
13 #include <gtest/gtest.h>
14
15 using namespace std;
16 using namespace isc::util;
17 using namespace boost::posix_time;
18 using namespace boost::gregorian;
19
20 /// Check the ptimeToText() function returns a numeric month.
21 /// Note durationToText() is called by ptimeToText() so is tested too.
22
23 // The Posix time epoch is 1970
TEST(BoostTimeUtilsTest,epoch)24 TEST(BoostTimeUtilsTest, epoch) {
25 time_t tepoch = 0;
26 ptime pepoch = from_time_t(tepoch);
27
28 // We're going to loop through precision values starting with 0 through
29 // the max supported precision. Each pass should after the first, should
30 // add an additional level of precision: secs, secs/10, secs/100,
31 // secs/1000 and so on. The initial string has no fraction seconds.
32 std::string expected("1970-01-01 00:00:00");
33 std::string sepoch;
34 for (int precision = 0; precision <= MAX_FSECS_PRECISION; ++precision) {
35 if (precision == 1) {
36 // Adding fractional seconds so we need append a decimal point.
37 expected.push_back('.');
38 }
39
40 if (precision >= 1) {
41 // Adding an additional level of precision, append a zero.
42 expected.push_back('0');
43 }
44
45 // Now let's see if we get the correct precision in the text.
46 sepoch = ptimeToText(pepoch, precision);
47 EXPECT_EQ(expected, sepoch) << " test precision:" << precision;
48 }
49
50 // Expected string should have same precision as default, so
51 // test the default.
52 sepoch = ptimeToText(pepoch);
53 EXPECT_EQ(expected, sepoch);
54
55 // Now test a requested precision beyond default. We should
56 // get the default precision.
57 sepoch = ptimeToText(pepoch, MAX_FSECS_PRECISION + 1);
58 EXPECT_EQ(expected, sepoch);
59
60 }
61
62 // The 2015 Bastille day
TEST(BoostTimeUtilsTest,bastilleDay)63 TEST(BoostTimeUtilsTest, bastilleDay) {
64 time_duration tdbast =
65 hours(12) + minutes(13) + seconds(14) + milliseconds(500);
66 ptime pbast(date(2015, Jul, 14), tdbast);
67
68 // We're going to loop through precision values starting with 0 through
69 // the max supported precision. Each pass should after the first, should
70 // add an additional level of precision: secs, secs/10, secs/100,
71 // secs/1000 and so on. The initial string has no fraction seconds.
72 std::string expected("2015-07-14 12:13:14");
73 std::string sbast;
74 for (int precision = 0; precision <= MAX_FSECS_PRECISION; ++precision) {
75 if (precision == 1) {
76 // Adding fractional seconds so we need append a decimal point
77 // and the digit 5 (i.e. 500 ms = .5 secs).
78 expected.push_back('.');
79 expected.push_back('5');
80 } else if (precision > 1) {
81 // Adding an additional level of precision, append a zero.
82 expected.push_back('0');
83 }
84
85 // Now let's see if we get the correct precision in the text.
86 sbast = ptimeToText(pbast, precision);
87 EXPECT_EQ(expected, sbast) << " test precision:" << precision;
88 }
89
90 // Expected string should have same precision as default, so
91 // test the default.
92 sbast = ptimeToText(pbast);
93 EXPECT_EQ(expected, sbast);
94
95 // Now test a requested precision beyond default. We should
96 // get the default precision.
97 sbast = ptimeToText(pbast, MAX_FSECS_PRECISION + 1);
98 EXPECT_EQ(expected, sbast);
99 }
100