Skip to content

Commit 222db9e

Browse files
Time_Value & Floating-point std::chrono::duration
`ACE_Time_Value` currently doesn't accept a `std::chrono::duration` with `Rep` that's a floating point type because it's using the modulus operator to separate the microseconds (`duration % seconds(1)`) and trying to use modulus with floating points causes a compile error. This changes it to use subtraction from the whole number of seconds (`duration - sec`) and adds a test for it.
1 parent e330559 commit 222db9e

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

ACE/ace/Time_Value.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,11 @@ class ACE_Export ACE_Time_Value
118118
template< class Rep, class Period >
119119
void set (const std::chrono::duration<Rep, Period>& duration)
120120
{
121-
std::chrono::seconds const s {
122-
std::chrono::duration_cast<std::chrono::seconds> (duration)};
121+
using namespace std::chrono;
123122

124-
std::chrono::microseconds const usec {
125-
std::chrono::duration_cast<std::chrono::microseconds>(
126-
duration % std::chrono::seconds (1))};
127-
this->set (ACE_Utils::truncate_cast<time_t>(s.count ()), ACE_Utils::truncate_cast<suseconds_t>(usec.count ()));
123+
seconds const sec {duration_cast<seconds> (duration)};
124+
microseconds const usec {duration_cast<microseconds> (duration - sec)};
125+
this->set (ACE_Utils::truncate_cast<time_t> (sec.count ()), ACE_Utils::truncate_cast<suseconds_t> (usec.count ()));
128126
}
129127

130128
/// Converts from ACE_Time_Value format into milliseconds format.

ACE/tests/.gitignore

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@
9494
/Compiler_Features_33_Test
9595
/Compiler_Features_34_Test
9696
/Compiler_Features_35_Test
97+
/Compiler_Features_36_Test
98+
/Compiler_Features_37_Test
99+
/Compiler_Features_38_Test
100+
/Compiler_Features_39_Test
101+
/Compiler_Features_40_Test
102+
/Compiler_Features_41_Test
103+
/Compiler_Features_42_Test
97104
/Config_Test
98105
/Conn_Test
99106
/Date_Time_Test
@@ -110,6 +117,7 @@
110117
/FlReactor_Test
111118
/Framework_Component_Test
112119
/Future_Set_Test
120+
/Future_Stress_Test
113121
/Future_Test
114122
/Get_Opt_Test
115123
/Handle_Set_Test
@@ -152,6 +160,7 @@
152160
/MT_Reference_Counted_Event_Handler_Test
153161
/MT_Reference_Counted_Notify_Test
154162
/MT_SOCK_Test
163+
/Multicast_Interfaces_Test
155164
/Multicast_Test
156165
/Multicast_Test_IPV6
157166
/Multihomed_INET_Addr_Test
@@ -216,8 +225,9 @@
216225
/Signal_Test
217226
/Sigset_Ops_Test
218227
/Simple_Message_Block_Test
219-
/Singleton_Test
220228
/Singleton_Restart_Test
229+
/Singleton_Test
230+
/SOCK_Acceptor_Test
221231
/SOCK_Connector_Test
222232
/SOCK_Dgram_Bcast_Test
223233
/SOCK_Dgram_Test
@@ -256,8 +266,8 @@
256266
/Token_Strategy_Test
257267
/Tokens_Test
258268
/TP_Reactor_Test
259-
/TSS_Static_Test
260269
/TSS_Leak_Test
270+
/TSS_Static_Test
261271
/TSS_Test
262272
/Unbounded_Set_Test
263273
/Unbounded_Set_Test_Ex
@@ -273,8 +283,3 @@
273283
/XtAthenaReactor_Test
274284
/XtMotifReactor_Test
275285
/XtReactor_Test
276-
/Compiler_Features_36_Test
277-
/Compiler_Features_37_Test
278-
/Compiler_Features_38_Test
279-
/SOCK_Acceptor_Test
280-
Multicast_Interfaces_Test

ACE/tests/Chrono_Test.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,21 @@ test_assignments ()
144144
++errors;
145145
}
146146

147+
// ACE_Time_Value should accept floating-point-based durations.
148+
std::chrono::duration<double, std::ratio<(24*3600)>> const half_day {0.5};
149+
tv = ACE_Time_Value { half_day };
150+
if (tv.sec () != 3600*12 || tv.usec () != 0)
151+
{
152+
ACE_ERROR ((LM_ERROR,
153+
ACE_TEXT ("(%P|%t) unexpected value after converting ")
154+
ACE_TEXT ("duration<double, std::ratio<(24*3600)>> to an ACE_Time_Value. ")
155+
ACE_TEXT ("<sec=%d,usec=0> - got <sec=%d,usec=%d>\n"),
156+
3600*12, tv.sec (), tv.usec ()));
157+
++errors;
158+
}
159+
147160
// Two times half a day, 3 hours, 24 minutes, 54 seconds, 238 milliseconds,
148161
// 754 microseconds and 342 nanoseconds.
149-
std::chrono::duration<double, std::ratio<(24*3600)>> half_day {0.5};
150162
std::chrono::microseconds const usec {
151163
2 * (
152164
std::chrono::duration_cast<std::chrono::microseconds> (

0 commit comments

Comments
 (0)