1*11be35a1SLionel Sambuc // Copyright 2010 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc // may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc // without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include "utils/datetime.hpp"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc extern "C" {
32*11be35a1SLionel Sambuc #include <time.h>
33*11be35a1SLionel Sambuc #include <unistd.h>
34*11be35a1SLionel Sambuc }
35*11be35a1SLionel Sambuc
36*11be35a1SLionel Sambuc #include <sstream>
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc #include <atf-c++.hpp>
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc namespace datetime = utils::datetime;
41*11be35a1SLionel Sambuc
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(delta__defaults);
ATF_TEST_CASE_BODY(delta__defaults)44*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(delta__defaults)
45*11be35a1SLionel Sambuc {
46*11be35a1SLionel Sambuc const datetime::delta delta;
47*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, delta.seconds);
48*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, delta.useconds);
49*11be35a1SLionel Sambuc }
50*11be35a1SLionel Sambuc
51*11be35a1SLionel Sambuc
52*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(delta__overrides);
ATF_TEST_CASE_BODY(delta__overrides)53*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(delta__overrides)
54*11be35a1SLionel Sambuc {
55*11be35a1SLionel Sambuc const datetime::delta delta(1, 2);
56*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, delta.seconds);
57*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(2, delta.useconds);
58*11be35a1SLionel Sambuc }
59*11be35a1SLionel Sambuc
60*11be35a1SLionel Sambuc
61*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(delta__from_microseconds);
ATF_TEST_CASE_BODY(delta__from_microseconds)62*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(delta__from_microseconds)
63*11be35a1SLionel Sambuc {
64*11be35a1SLionel Sambuc {
65*11be35a1SLionel Sambuc const datetime::delta delta = datetime::delta::from_microseconds(0);
66*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, delta.seconds);
67*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, delta.useconds);
68*11be35a1SLionel Sambuc }
69*11be35a1SLionel Sambuc {
70*11be35a1SLionel Sambuc const datetime::delta delta = datetime::delta::from_microseconds(
71*11be35a1SLionel Sambuc 999999);
72*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, delta.seconds);
73*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(999999, delta.useconds);
74*11be35a1SLionel Sambuc }
75*11be35a1SLionel Sambuc {
76*11be35a1SLionel Sambuc const datetime::delta delta = datetime::delta::from_microseconds(
77*11be35a1SLionel Sambuc 1000000);
78*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1, delta.seconds);
79*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, delta.useconds);
80*11be35a1SLionel Sambuc }
81*11be35a1SLionel Sambuc {
82*11be35a1SLionel Sambuc const datetime::delta delta = datetime::delta::from_microseconds(
83*11be35a1SLionel Sambuc 10576293);
84*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(10, delta.seconds);
85*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(576293, delta.useconds);
86*11be35a1SLionel Sambuc }
87*11be35a1SLionel Sambuc {
88*11be35a1SLionel Sambuc const datetime::delta delta = datetime::delta::from_microseconds(
89*11be35a1SLionel Sambuc 123456789123456);
90*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(123456789, delta.seconds);
91*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(123456, delta.useconds);
92*11be35a1SLionel Sambuc }
93*11be35a1SLionel Sambuc }
94*11be35a1SLionel Sambuc
95*11be35a1SLionel Sambuc
96*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(delta__to_microseconds);
ATF_TEST_CASE_BODY(delta__to_microseconds)97*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(delta__to_microseconds)
98*11be35a1SLionel Sambuc {
99*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(0, datetime::delta(0, 0).to_microseconds());
100*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(999999, datetime::delta(0, 999999).to_microseconds());
101*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1000000, datetime::delta(1, 0).to_microseconds());
102*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(10576293, datetime::delta(10, 576293).to_microseconds());
103*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(11576293, datetime::delta(10, 1576293).to_microseconds());
104*11be35a1SLionel Sambuc }
105*11be35a1SLionel Sambuc
106*11be35a1SLionel Sambuc
107*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(delta__equals);
ATF_TEST_CASE_BODY(delta__equals)108*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(delta__equals)
109*11be35a1SLionel Sambuc {
110*11be35a1SLionel Sambuc ATF_REQUIRE(datetime::delta() == datetime::delta());
111*11be35a1SLionel Sambuc ATF_REQUIRE(datetime::delta() == datetime::delta(0, 0));
112*11be35a1SLionel Sambuc ATF_REQUIRE(datetime::delta(1, 2) == datetime::delta(1, 2));
113*11be35a1SLionel Sambuc
114*11be35a1SLionel Sambuc ATF_REQUIRE(!(datetime::delta() == datetime::delta(0, 1)));
115*11be35a1SLionel Sambuc ATF_REQUIRE(!(datetime::delta() == datetime::delta(1, 0)));
116*11be35a1SLionel Sambuc ATF_REQUIRE(!(datetime::delta(1, 2) == datetime::delta(2, 1)));
117*11be35a1SLionel Sambuc }
118*11be35a1SLionel Sambuc
119*11be35a1SLionel Sambuc
120*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(delta__differs);
ATF_TEST_CASE_BODY(delta__differs)121*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(delta__differs)
122*11be35a1SLionel Sambuc {
123*11be35a1SLionel Sambuc ATF_REQUIRE(!(datetime::delta() != datetime::delta()));
124*11be35a1SLionel Sambuc ATF_REQUIRE(!(datetime::delta() != datetime::delta(0, 0)));
125*11be35a1SLionel Sambuc ATF_REQUIRE(!(datetime::delta(1, 2) != datetime::delta(1, 2)));
126*11be35a1SLionel Sambuc
127*11be35a1SLionel Sambuc ATF_REQUIRE(datetime::delta() != datetime::delta(0, 1));
128*11be35a1SLionel Sambuc ATF_REQUIRE(datetime::delta() != datetime::delta(1, 0));
129*11be35a1SLionel Sambuc ATF_REQUIRE(datetime::delta(1, 2) != datetime::delta(2, 1));
130*11be35a1SLionel Sambuc }
131*11be35a1SLionel Sambuc
132*11be35a1SLionel Sambuc
133*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(delta__addition);
ATF_TEST_CASE_BODY(delta__addition)134*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(delta__addition)
135*11be35a1SLionel Sambuc {
136*11be35a1SLionel Sambuc using datetime::delta;
137*11be35a1SLionel Sambuc
138*11be35a1SLionel Sambuc ATF_REQUIRE(delta() == delta() + delta());
139*11be35a1SLionel Sambuc ATF_REQUIRE(delta(0, 10) == delta() + delta(0, 10));
140*11be35a1SLionel Sambuc ATF_REQUIRE(delta(10, 0) == delta(10, 0) + delta());
141*11be35a1SLionel Sambuc
142*11be35a1SLionel Sambuc ATF_REQUIRE(delta(1, 234567) == delta(0, 1234567) + delta());
143*11be35a1SLionel Sambuc ATF_REQUIRE(delta(12, 34) == delta(10, 20) + delta(2, 14));
144*11be35a1SLionel Sambuc }
145*11be35a1SLionel Sambuc
146*11be35a1SLionel Sambuc
147*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(delta__addition_and_set);
ATF_TEST_CASE_BODY(delta__addition_and_set)148*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(delta__addition_and_set)
149*11be35a1SLionel Sambuc {
150*11be35a1SLionel Sambuc using datetime::delta;
151*11be35a1SLionel Sambuc
152*11be35a1SLionel Sambuc {
153*11be35a1SLionel Sambuc delta d;
154*11be35a1SLionel Sambuc d += delta(3, 5);
155*11be35a1SLionel Sambuc ATF_REQUIRE(delta(3, 5) == d);
156*11be35a1SLionel Sambuc }
157*11be35a1SLionel Sambuc {
158*11be35a1SLionel Sambuc delta d(1, 2);
159*11be35a1SLionel Sambuc d += delta(3, 5);
160*11be35a1SLionel Sambuc ATF_REQUIRE(delta(4, 7) == d);
161*11be35a1SLionel Sambuc }
162*11be35a1SLionel Sambuc {
163*11be35a1SLionel Sambuc delta d(1, 2);
164*11be35a1SLionel Sambuc ATF_REQUIRE(delta(4, 7) == (d += delta(3, 5)));
165*11be35a1SLionel Sambuc }
166*11be35a1SLionel Sambuc }
167*11be35a1SLionel Sambuc
168*11be35a1SLionel Sambuc
169*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(delta__output);
ATF_TEST_CASE_BODY(delta__output)170*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(delta__output)
171*11be35a1SLionel Sambuc {
172*11be35a1SLionel Sambuc {
173*11be35a1SLionel Sambuc std::ostringstream str;
174*11be35a1SLionel Sambuc str << datetime::delta(15, 8791);
175*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("15008791us", str.str());
176*11be35a1SLionel Sambuc }
177*11be35a1SLionel Sambuc {
178*11be35a1SLionel Sambuc std::ostringstream str;
179*11be35a1SLionel Sambuc str << datetime::delta(12345678, 0);
180*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("12345678000000us", str.str());
181*11be35a1SLionel Sambuc }
182*11be35a1SLionel Sambuc }
183*11be35a1SLionel Sambuc
184*11be35a1SLionel Sambuc
185*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(timestamp__copy);
ATF_TEST_CASE_BODY(timestamp__copy)186*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(timestamp__copy)
187*11be35a1SLionel Sambuc {
188*11be35a1SLionel Sambuc const datetime::timestamp ts1 = datetime::timestamp::from_values(
189*11be35a1SLionel Sambuc 2011, 2, 16, 19, 15, 30, 0);
190*11be35a1SLionel Sambuc {
191*11be35a1SLionel Sambuc const datetime::timestamp ts2 = ts1;
192*11be35a1SLionel Sambuc const datetime::timestamp ts3 = datetime::timestamp::from_values(
193*11be35a1SLionel Sambuc 2012, 2, 16, 19, 15, 30, 0);
194*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("2011", ts1.strftime("%Y"));
195*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("2011", ts2.strftime("%Y"));
196*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("2012", ts3.strftime("%Y"));
197*11be35a1SLionel Sambuc }
198*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("2011", ts1.strftime("%Y"));
199*11be35a1SLionel Sambuc }
200*11be35a1SLionel Sambuc
201*11be35a1SLionel Sambuc
202*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(timestamp__from_microseconds);
ATF_TEST_CASE_BODY(timestamp__from_microseconds)203*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(timestamp__from_microseconds)
204*11be35a1SLionel Sambuc {
205*11be35a1SLionel Sambuc const datetime::timestamp ts = datetime::timestamp::from_microseconds(
206*11be35a1SLionel Sambuc 1328829351987654LL);
207*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("2012-02-09 23:15:51", ts.strftime("%Y-%m-%d %H:%M:%S"));
208*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1328829351987654LL, ts.to_microseconds());
209*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1328829351, ts.to_seconds());
210*11be35a1SLionel Sambuc }
211*11be35a1SLionel Sambuc
212*11be35a1SLionel Sambuc
213*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(timestamp__now__mock);
ATF_TEST_CASE_BODY(timestamp__now__mock)214*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(timestamp__now__mock)
215*11be35a1SLionel Sambuc {
216*11be35a1SLionel Sambuc datetime::set_mock_now(2011, 2, 21, 18, 5, 10, 0);
217*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("2011-02-21 18:05:10",
218*11be35a1SLionel Sambuc datetime::timestamp::now().strftime("%Y-%m-%d %H:%M:%S"));
219*11be35a1SLionel Sambuc
220*11be35a1SLionel Sambuc datetime::set_mock_now(2012, 3, 22, 19, 6, 11, 54321);
221*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("2012-03-22 19:06:11",
222*11be35a1SLionel Sambuc datetime::timestamp::now().strftime("%Y-%m-%d %H:%M:%S"));
223*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("2012-03-22 19:06:11",
224*11be35a1SLionel Sambuc datetime::timestamp::now().strftime("%Y-%m-%d %H:%M:%S"));
225*11be35a1SLionel Sambuc }
226*11be35a1SLionel Sambuc
227*11be35a1SLionel Sambuc
228*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(timestamp__now__real);
ATF_TEST_CASE_BODY(timestamp__now__real)229*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(timestamp__now__real)
230*11be35a1SLionel Sambuc {
231*11be35a1SLionel Sambuc // This test is might fail if we happen to run at the crossing of one
232*11be35a1SLionel Sambuc // day to the other and the two measures we pick of the current time
233*11be35a1SLionel Sambuc // differ. This is so unlikely that I haven't bothered to do this in any
234*11be35a1SLionel Sambuc // other way.
235*11be35a1SLionel Sambuc
236*11be35a1SLionel Sambuc const time_t just_before = ::time(NULL);
237*11be35a1SLionel Sambuc const datetime::timestamp now = datetime::timestamp::now();
238*11be35a1SLionel Sambuc
239*11be35a1SLionel Sambuc ::tm data;
240*11be35a1SLionel Sambuc char buf[1024];
241*11be35a1SLionel Sambuc ATF_REQUIRE(::gmtime_r(&just_before, &data) != 0);
242*11be35a1SLionel Sambuc ATF_REQUIRE(::strftime(buf, sizeof(buf), "%Y-%m-%d", &data) != 0);
243*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(buf, now.strftime("%Y-%m-%d"));
244*11be35a1SLionel Sambuc
245*11be35a1SLionel Sambuc ATF_REQUIRE(now.strftime("%Z") == "GMT" || now.strftime("%Z") == "UTC");
246*11be35a1SLionel Sambuc }
247*11be35a1SLionel Sambuc
248*11be35a1SLionel Sambuc
249*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(timestamp__now__granularity);
ATF_TEST_CASE_BODY(timestamp__now__granularity)250*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(timestamp__now__granularity)
251*11be35a1SLionel Sambuc {
252*11be35a1SLionel Sambuc const datetime::timestamp first = datetime::timestamp::now();
253*11be35a1SLionel Sambuc ::usleep(1);
254*11be35a1SLionel Sambuc const datetime::timestamp second = datetime::timestamp::now();
255*11be35a1SLionel Sambuc ATF_REQUIRE(first.to_microseconds() != second.to_microseconds());
256*11be35a1SLionel Sambuc }
257*11be35a1SLionel Sambuc
258*11be35a1SLionel Sambuc
259*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(timestamp__strftime);
ATF_TEST_CASE_BODY(timestamp__strftime)260*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(timestamp__strftime)
261*11be35a1SLionel Sambuc {
262*11be35a1SLionel Sambuc const datetime::timestamp ts1 = datetime::timestamp::from_values(
263*11be35a1SLionel Sambuc 2010, 12, 10, 8, 45, 50, 0);
264*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("2010-12-10", ts1.strftime("%Y-%m-%d"));
265*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("08:45:50", ts1.strftime("%H:%M:%S"));
266*11be35a1SLionel Sambuc
267*11be35a1SLionel Sambuc const datetime::timestamp ts2 = datetime::timestamp::from_values(
268*11be35a1SLionel Sambuc 2011, 2, 16, 19, 15, 30, 0);
269*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("2011-02-16T19:15:30", ts2.strftime("%Y-%m-%dT%H:%M:%S"));
270*11be35a1SLionel Sambuc }
271*11be35a1SLionel Sambuc
272*11be35a1SLionel Sambuc
273*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(timestamp__to_microseconds);
ATF_TEST_CASE_BODY(timestamp__to_microseconds)274*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(timestamp__to_microseconds)
275*11be35a1SLionel Sambuc {
276*11be35a1SLionel Sambuc const datetime::timestamp ts1 = datetime::timestamp::from_values(
277*11be35a1SLionel Sambuc 2010, 12, 10, 8, 45, 50, 123456);
278*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1291970750123456LL, ts1.to_microseconds());
279*11be35a1SLionel Sambuc }
280*11be35a1SLionel Sambuc
281*11be35a1SLionel Sambuc
282*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(timestamp__to_seconds);
ATF_TEST_CASE_BODY(timestamp__to_seconds)283*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(timestamp__to_seconds)
284*11be35a1SLionel Sambuc {
285*11be35a1SLionel Sambuc const datetime::timestamp ts1 = datetime::timestamp::from_values(
286*11be35a1SLionel Sambuc 2010, 12, 10, 8, 45, 50, 123456);
287*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1291970750, ts1.to_seconds());
288*11be35a1SLionel Sambuc }
289*11be35a1SLionel Sambuc
290*11be35a1SLionel Sambuc
291*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(timestamp__leap_second);
ATF_TEST_CASE_BODY(timestamp__leap_second)292*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(timestamp__leap_second)
293*11be35a1SLionel Sambuc {
294*11be35a1SLionel Sambuc // This is actually a test for from_values(), which is the function that
295*11be35a1SLionel Sambuc // includes assertions to validate the input parameters.
296*11be35a1SLionel Sambuc const datetime::timestamp ts1 = datetime::timestamp::from_values(
297*11be35a1SLionel Sambuc 2012, 6, 30, 23, 59, 60, 543);
298*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(1341100800, ts1.to_seconds());
299*11be35a1SLionel Sambuc }
300*11be35a1SLionel Sambuc
301*11be35a1SLionel Sambuc
302*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(timestamp__equals);
ATF_TEST_CASE_BODY(timestamp__equals)303*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(timestamp__equals)
304*11be35a1SLionel Sambuc {
305*11be35a1SLionel Sambuc ATF_REQUIRE(datetime::timestamp::from_microseconds(1291970750123456LL) ==
306*11be35a1SLionel Sambuc datetime::timestamp::from_microseconds(1291970750123456LL));
307*11be35a1SLionel Sambuc }
308*11be35a1SLionel Sambuc
309*11be35a1SLionel Sambuc
310*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(timestamp__differs);
ATF_TEST_CASE_BODY(timestamp__differs)311*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(timestamp__differs)
312*11be35a1SLionel Sambuc {
313*11be35a1SLionel Sambuc ATF_REQUIRE(datetime::timestamp::from_microseconds(1291970750123456LL) !=
314*11be35a1SLionel Sambuc datetime::timestamp::from_microseconds(1291970750123455LL));
315*11be35a1SLionel Sambuc }
316*11be35a1SLionel Sambuc
317*11be35a1SLionel Sambuc
318*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(timestamp__subtraction);
ATF_TEST_CASE_BODY(timestamp__subtraction)319*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(timestamp__subtraction)
320*11be35a1SLionel Sambuc {
321*11be35a1SLionel Sambuc const datetime::timestamp ts1 = datetime::timestamp::from_microseconds(
322*11be35a1SLionel Sambuc 1291970750123456LL);
323*11be35a1SLionel Sambuc const datetime::timestamp ts2 = datetime::timestamp::from_microseconds(
324*11be35a1SLionel Sambuc 1291970750123468LL);
325*11be35a1SLionel Sambuc const datetime::timestamp ts3 = datetime::timestamp::from_microseconds(
326*11be35a1SLionel Sambuc 1291970850123456LL);
327*11be35a1SLionel Sambuc
328*11be35a1SLionel Sambuc ATF_REQUIRE(datetime::delta(0, 0) == ts1 - ts1);
329*11be35a1SLionel Sambuc ATF_REQUIRE(datetime::delta(0, 12) == ts2 - ts1);
330*11be35a1SLionel Sambuc ATF_REQUIRE(datetime::delta(100, 0) == ts3 - ts1);
331*11be35a1SLionel Sambuc ATF_REQUIRE(datetime::delta(99, 999988) == ts3 - ts2);
332*11be35a1SLionel Sambuc }
333*11be35a1SLionel Sambuc
334*11be35a1SLionel Sambuc
335*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(timestamp__output);
ATF_TEST_CASE_BODY(timestamp__output)336*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(timestamp__output)
337*11be35a1SLionel Sambuc {
338*11be35a1SLionel Sambuc {
339*11be35a1SLionel Sambuc std::ostringstream str;
340*11be35a1SLionel Sambuc str << datetime::timestamp::from_microseconds(1291970750123456LL);
341*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("1291970750123456us", str.str());
342*11be35a1SLionel Sambuc }
343*11be35a1SLionel Sambuc {
344*11be35a1SLionel Sambuc std::ostringstream str;
345*11be35a1SLionel Sambuc str << datetime::timestamp::from_microseconds(1028309798759812LL);
346*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("1028309798759812us", str.str());
347*11be35a1SLionel Sambuc }
348*11be35a1SLionel Sambuc }
349*11be35a1SLionel Sambuc
350*11be35a1SLionel Sambuc
ATF_INIT_TEST_CASES(tcs)351*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
352*11be35a1SLionel Sambuc {
353*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, delta__defaults);
354*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, delta__overrides);
355*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, delta__from_microseconds);
356*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, delta__to_microseconds);
357*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, delta__equals);
358*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, delta__differs);
359*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, delta__addition);
360*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, delta__addition_and_set);
361*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, delta__output);
362*11be35a1SLionel Sambuc
363*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, timestamp__copy);
364*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, timestamp__from_microseconds);
365*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, timestamp__now__mock);
366*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, timestamp__now__real);
367*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, timestamp__now__granularity);
368*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, timestamp__strftime);
369*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, timestamp__to_microseconds);
370*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, timestamp__to_seconds);
371*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, timestamp__leap_second);
372*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, timestamp__equals);
373*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, timestamp__differs);
374*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, timestamp__subtraction);
375*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, timestamp__output);
376*11be35a1SLionel Sambuc }
377