1 /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
2 * Use, modification and distribution is subject to the
3 * Boost Software License, Version 1.0. (See accompanying
4 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
5
6 */
7
8 #include "boost/date_time/posix_time/posix_time.hpp"
9 #include "boost/date_time/microsec_time_clock.hpp"
10 #include "../testfrmwk.hpp"
11 #if defined(BOOST_HAS_FTIME)
12 #include <windows.h>
13 #endif
14
15 void
sync_to_next_second()16 sync_to_next_second()
17 {
18 using namespace boost::posix_time;
19
20 ptime t_prev;
21 ptime t_now = second_clock::local_time();
22
23 // Wait the next seconds
24 do
25 {
26 t_prev = t_now;
27 t_now = second_clock::local_time();
28 } while (t_now.time_of_day().seconds() == t_prev.time_of_day().seconds());
29
30 // Wait 300ms in order to avoid seconds of second_clock > microsec_clock.
31 t_now = microsec_clock::local_time();
32 t_prev = t_now;
33 do
34 {
35 t_now = microsec_clock::local_time();
36 } while (t_now - t_prev < milliseconds(300));
37
38 }
39
40
41 int
main()42 main()
43 {
44 #ifdef BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK
45
46 using namespace boost::posix_time;
47
48 std::cout << "Check local time of microsec_clock against second_clock" << std::endl;
49
50 ptime last = microsec_clock::local_time();
51 int max = 30;
52 for (int i=0; i<max; i++)
53 {
54 // Some systems loop too fast so "last is less" tests fail due to
55 // 'last' & 't2' being equal. These calls slow it down enough to
56 // make 'last' & 't2' different. Moreover, we must wait the next
57 // second to avoid a change in hour, minute or second field
58 // between acquisition of t1 and t2.
59 sync_to_next_second();
60
61 ptime t1 = second_clock::local_time();
62 std::cout << t1 << std::endl;
63
64 ptime t2 = microsec_clock::local_time();
65 std::cout << t2 << std::endl;
66
67 check("check equality of hours "
68 "between second_clock and microsec_clock timestamps",
69 t1.time_of_day().hours() == t2.time_of_day().hours());
70
71 check("check equality of minutes "
72 "between second_clock and microsec_clock timestamps",
73 t1.time_of_day().minutes() == t2.time_of_day().minutes());
74
75 check("check equality of seconds "
76 "between second_clock and microsec_clock timestamps",
77 t1.time_of_day().seconds() == t2.time_of_day().seconds());
78
79 check("check equality of date"
80 "between second_clock and microsec_clock timestamps",
81 t1.date() == t2.date());
82
83 if( !check("check that previous microsec_clock timestamp "
84 "is less than the current", last < t2) ) {
85 std::cout << last << " < " << t2 << std::endl;
86 }
87
88 last = t2;
89 }
90
91
92 std::cout << "Check universal time of microsec_clock against second_clock" << std::endl;
93 max = 10;
94 last = microsec_clock::universal_time();
95 for (int i=0; i<max; i++)
96 {
97 // Some systems loop too fast so "last is less" tests fail due to
98 // 'last' & 't2' being equal. These calls slow it down enough to
99 // make 'last' & 't2' different. Moreover, we must wait the next
100 // second to avoid a change in hour, minute or second field
101 // between acquisition of t1 and t2.
102 sync_to_next_second();
103
104 ptime t1 = second_clock::universal_time();
105 std::cout << t1 << std::endl;
106
107 ptime t2 = microsec_clock::universal_time();
108 std::cout << t2 << std::endl;
109
110 check("check equality of hours "
111 "between second_clock and microsec_clock timestamps",
112 t1.time_of_day().hours() == t2.time_of_day().hours());
113
114 check("check equality of minutes "
115 "between second_clock and microsec_clock timestamps",
116 t1.time_of_day().minutes() == t2.time_of_day().minutes());
117
118 check("check equality of seconds "
119 "between second_clock and microsec_clock timestamps",
120 t1.time_of_day().seconds() == t2.time_of_day().seconds());
121
122 check("check equality of date"
123 "between second_clock and microsec_clock timestamps",
124 t1.date() == t2.date());
125
126 if( !check("check that previous microsec_clock timestamp "
127 "is less than the current", last < t2) ) {
128 std::cout << last << " < " << t2 << std::endl;
129 }
130
131 last = t2;
132 }
133
134 #else
135 check("Get time of day micro second clock not supported due to inadequate compiler/platform", false);
136 #endif
137 return printTestStats();
138
139 }
140