1 // Copyright (C) 2015-2020 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/chrono_time_utils.h>
10
11 #include <string.h>
12 #include <time.h>
13
14 #include <gtest/gtest.h>
15
16 using namespace std;
17 using namespace std::chrono;
18 using namespace isc::util;
19
20 /// Check the clockToText() function returns a numeric month.
TEST(ChronoTimeUtilsTest,epoch)21 TEST(ChronoTimeUtilsTest, epoch) {
22 // The system clock is a wall clock using the local time zone so
23 // the epoch is zero only at some places or of course if the
24 // system is in UTC...
25 struct tm epoch;
26 memset(&epoch, 0, sizeof(epoch));
27 epoch.tm_year = 70;
28 epoch.tm_mday = 1;
29 epoch.tm_isdst = -1;
30 time_t tepoch = mktime(&epoch);
31 system_clock::time_point pepoch = system_clock::from_time_t(tepoch);
32
33 // We're going to loop through precision values starting with 0 through
34 // the max supported precision. Each pass should after the first, should
35 // add an additional level of precision: secs, secs/10, secs/100,
36 // secs/1000 and so on. The initial string has no fraction seconds.
37 std::string expected("1970-01-01 00:00:00");
38 std::string sepoch;
39 for (int precision = 0; precision <= MAX_FSECS_PRECISION; ++precision) {
40 if (precision == 1) {
41 // Adding fractional seconds so we need append a decimal point.
42 expected.push_back('.');
43 }
44
45 if (precision >= 1) {
46 // Adding an additional level of precision, append a zero.
47 expected.push_back('0');
48 }
49
50 // Now let's see if we get the correct precision in the text.
51 sepoch = clockToText(pepoch, precision);
52 EXPECT_EQ(expected, sepoch) << " test precision:" << precision;
53 }
54
55 // Expected string should have same precision as default, so
56 // test the default.
57 sepoch = clockToText(pepoch);
58 EXPECT_EQ(expected, sepoch);
59
60 // Now test a requested precision beyond default. We should
61 // get the default precision.
62 sepoch = clockToText(pepoch, MAX_FSECS_PRECISION + 1);
63 EXPECT_EQ(expected, sepoch);
64
65 }
66
67 /// Check the durationToText() works as expected.
68 /// Note durationToText() is not called by clockToText().
TEST(ChronoTimeUtilsTest,duration)69 TEST(ChronoTimeUtilsTest, duration) {
70 system_clock::duration p123 = hours(1) + minutes(2) + seconds(3);
71
72 // We're going to loop through precision values starting with 0 through
73 // the max supported precision. Each pass should after the first, should
74 // add an additional level of precision: secs, secs/10, secs/100,
75 // secs/1000 and so on. The initial string has no fraction seconds.
76 std::string expected("01:02:03");
77 std::string s123;
78 for (int precision = 0; precision <= MAX_FSECS_PRECISION; ++precision) {
79 if (precision == 1) {
80 // Adding fractional seconds so we need append a decimal point.
81 expected.push_back('.');
82 }
83
84 if (precision >= 1) {
85 // Adding an additional level of precision, append a zero.
86 expected.push_back('0');
87 }
88
89 // Now let's see if we get the correct precision in the text.
90 s123 = durationToText(p123, precision);
91 EXPECT_EQ(expected, s123) << " test precision:" << precision;
92 }
93
94 // Expected string should have same precision as default, so
95 // test the default.
96 s123 = durationToText(p123);
97 EXPECT_EQ(expected, s123);
98
99 // Now test a requested precision beyond default. We should
100 // get the default precision.
101 s123 = durationToText(p123, MAX_FSECS_PRECISION + 1);
102 EXPECT_EQ(expected, s123);
103 }
104
105 // The 2015 Bastille day
TEST(ChronoTimeUtilsTest,bastilleDay)106 TEST(ChronoTimeUtilsTest, bastilleDay) {
107 struct tm tm;
108 tm.tm_year = 2015 - 1900;
109 tm.tm_mon = 7 - 1;
110 tm.tm_mday = 14;
111 tm.tm_hour = 12;
112 tm.tm_min = 13;
113 tm.tm_sec = 14;
114 tm.tm_isdst = -1;
115 time_t tbast = mktime(&tm);
116 system_clock::time_point tpbast = system_clock::from_time_t(tbast);
117 tpbast += milliseconds(500);
118
119 // We're going to loop through precision values starting with 0 through
120 // the max supported precision. Each pass should after the first, should
121 // add an additional level of precision: secs, secs/10, secs/100,
122 // secs/1000 and so on. The initial string has no fraction seconds.
123 std::string expected("2015-07-14 12:13:14");
124 std::string sbast;
125 for (int precision = 0; precision <= MAX_FSECS_PRECISION; ++precision) {
126 if (precision == 1) {
127 // Adding fractional seconds so we need append a decimal point
128 // and the digit 5 (i.e. 500 ms = .5 secs).
129 expected.push_back('.');
130 expected.push_back('5');
131 } else if (precision > 1) {
132 // Adding an additional level of precision, append a zero.
133 expected.push_back('0');
134 }
135
136 // Now let's see if we get the correct precision in the text.
137 sbast = clockToText(tpbast, precision);
138 EXPECT_EQ(expected, sbast) << " test precision:" << precision;
139 }
140
141 // Expected string should have same precision as default, so
142 // test the default.
143 sbast = clockToText(tpbast);
144 EXPECT_EQ(expected, sbast);
145
146 // Now test a requested precision beyond default. We should
147 // get the default precision.
148 sbast = clockToText(tpbast, MAX_FSECS_PRECISION + 1);
149 EXPECT_EQ(expected, sbast);
150 }
151
152 // Try steady clock duration.
TEST(ChronoTimeUtilsTest,steadyClock)153 TEST(ChronoTimeUtilsTest, steadyClock) {
154 steady_clock::duration p12345 = hours(1) + minutes(2) + seconds(3) +
155 milliseconds(4) + microseconds(5);
156 std::string expected("01:02:03.004005");
157 std::string s12345 = durationToText(p12345, 6);
158 EXPECT_EQ(expected, s12345);
159 }
160