]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | /* Copyright (c) 2002,2003 CrystalClear Software, Inc. |
92f5a8d4 | 2 | * Use, modification and distribution is subject to the |
7c673cae FG |
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 | ||
92f5a8d4 TL |
15 | void |
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 | ||
7c673cae | 41 | int |
92f5a8d4 | 42 | main() |
7c673cae FG |
43 | { |
44 | #ifdef BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK | |
45 | ||
46 | using namespace boost::posix_time; | |
47 | ||
92f5a8d4 | 48 | std::cout << "Check local time of microsec_clock against second_clock" << std::endl; |
7c673cae | 49 | |
92f5a8d4 | 50 | ptime last = microsec_clock::local_time(); |
1e59de90 | 51 | int max = 5; |
92f5a8d4 | 52 | for (int i=0; i<max; i++) |
7c673cae | 53 | { |
92f5a8d4 TL |
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(); | |
7c673cae FG |
60 | |
61 | ptime t1 = second_clock::local_time(); | |
f67539c2 | 62 | std::cout << t1 << std::endl; |
7c673cae FG |
63 | |
64 | ptime t2 = microsec_clock::local_time(); | |
f67539c2 | 65 | std::cout << t2 << std::endl; |
92f5a8d4 | 66 | |
1e59de90 TL |
67 | // sometimes on very slow systems (github actions) a long time |
68 | // can pass between syncing to the next second + 300ms (see previous | |
69 | // function) and asking for t2; therefore if the fractional seconds | |
70 | // of t2 are less than 300ms we skip that test loop assuming it | |
71 | // took too long | |
72 | time_duration t2_tod = t2.time_of_day(); | |
73 | if (t2_tod.fractional_seconds() < (t2_tod.ticks_per_second() * 3) / 10) { | |
74 | std::cout << "SKIP :: we had a significant host processing delay" << std::endl; | |
75 | continue; | |
76 | } | |
77 | ||
92f5a8d4 TL |
78 | check("check equality of hours " |
79 | "between second_clock and microsec_clock timestamps", | |
80 | t1.time_of_day().hours() == t2.time_of_day().hours()); | |
81 | ||
82 | check("check equality of minutes " | |
83 | "between second_clock and microsec_clock timestamps", | |
7c673cae | 84 | t1.time_of_day().minutes() == t2.time_of_day().minutes()); |
92f5a8d4 TL |
85 | |
86 | check("check equality of seconds " | |
87 | "between second_clock and microsec_clock timestamps", | |
88 | t1.time_of_day().seconds() == t2.time_of_day().seconds()); | |
89 | ||
90 | check("check equality of date" | |
91 | "between second_clock and microsec_clock timestamps", | |
92 | t1.date() == t2.date()); | |
93 | ||
94 | if( !check("check that previous microsec_clock timestamp " | |
95 | "is less than the current", last < t2) ) { | |
f67539c2 | 96 | std::cout << last << " < " << t2 << std::endl; |
7c673cae | 97 | } |
7c673cae | 98 | |
92f5a8d4 | 99 | last = t2; |
7c673cae FG |
100 | } |
101 | ||
102 | ||
92f5a8d4 | 103 | std::cout << "Check universal time of microsec_clock against second_clock" << std::endl; |
1e59de90 | 104 | max = 5; |
7c673cae | 105 | last = microsec_clock::universal_time(); |
92f5a8d4 | 106 | for (int i=0; i<max; i++) |
7c673cae | 107 | { |
92f5a8d4 TL |
108 | // Some systems loop too fast so "last is less" tests fail due to |
109 | // 'last' & 't2' being equal. These calls slow it down enough to | |
110 | // make 'last' & 't2' different. Moreover, we must wait the next | |
111 | // second to avoid a change in hour, minute or second field | |
112 | // between acquisition of t1 and t2. | |
113 | sync_to_next_second(); | |
7c673cae FG |
114 | |
115 | ptime t1 = second_clock::universal_time(); | |
f67539c2 | 116 | std::cout << t1 << std::endl; |
7c673cae FG |
117 | |
118 | ptime t2 = microsec_clock::universal_time(); | |
f67539c2 | 119 | std::cout << t2 << std::endl; |
92f5a8d4 | 120 | |
1e59de90 TL |
121 | // sometimes on very slow systems (github actions) a long time |
122 | // can pass between syncing to the next second + 300ms (see previous | |
123 | // function) and asking for t2; therefore if the fractional seconds | |
124 | // of t2 are less than 300ms we skip that test loop assuming it | |
125 | // took too long | |
126 | time_duration t2_tod = t2.time_of_day(); | |
127 | if (t2_tod.fractional_seconds() < (t2_tod.ticks_per_second() * 3) / 10) { | |
128 | std::cout << "SKIP :: we had a significant host processing delay" << std::endl; | |
129 | continue; | |
130 | } | |
131 | ||
92f5a8d4 TL |
132 | check("check equality of hours " |
133 | "between second_clock and microsec_clock timestamps", | |
134 | t1.time_of_day().hours() == t2.time_of_day().hours()); | |
135 | ||
136 | check("check equality of minutes " | |
137 | "between second_clock and microsec_clock timestamps", | |
7c673cae | 138 | t1.time_of_day().minutes() == t2.time_of_day().minutes()); |
92f5a8d4 TL |
139 | |
140 | check("check equality of seconds " | |
141 | "between second_clock and microsec_clock timestamps", | |
142 | t1.time_of_day().seconds() == t2.time_of_day().seconds()); | |
143 | ||
144 | check("check equality of date" | |
145 | "between second_clock and microsec_clock timestamps", | |
146 | t1.date() == t2.date()); | |
147 | ||
148 | if( !check("check that previous microsec_clock timestamp " | |
149 | "is less than the current", last < t2) ) { | |
f67539c2 | 150 | std::cout << last << " < " << t2 << std::endl; |
7c673cae | 151 | } |
7c673cae | 152 | |
92f5a8d4 | 153 | last = t2; |
7c673cae FG |
154 | } |
155 | ||
156 | #else | |
157 | check("Get time of day micro second clock not supported due to inadequate compiler/platform", false); | |
158 | #endif | |
159 | return printTestStats(); | |
160 | ||
161 | } |