OpenDDS  Snapshot(2023/04/28-20:55)
TimeDuration.cpp
Go to the documentation of this file.
1 #include <DCPS/DdsDcps_pch.h> // Only the _pch include should start with DCPS/
2 
3 #include "TimeDuration.h"
4 
5 #include "SafetyProfileStreams.h"
6 
7 #include <cmath>
8 #include <cstdio>
9 #ifdef ACE_HAS_CPP11
10 # include <limits>
11 #else
12 # include <ace/Numeric_Limits.h>
13 #endif /* ACE_HAS_CPP11*/
14 
16 
17 namespace OpenDDS {
18 namespace DCPS {
19 
20 const TimeDuration TimeDuration::zero_value(0, 0);
21 const TimeDuration TimeDuration::max_value(
22 #ifdef ACE_HAS_CPP11
23  std::numeric_limits<time_t>::max(),
24 #else
26 #endif
27  ACE_ONE_SECOND_IN_USECS - 1);
28 
29 namespace {
30  time_t usec_to_rounded_frac(
31  suseconds_t value, unsigned decimal_places, time_t& carry)
32  {
33  const double frac = static_cast<double>(value) / ACE_ONE_SECOND_IN_USECS;
34  const double denominator = std::pow(10.0, static_cast<double>(decimal_places));
35  const double numerator = std::floor(frac * denominator + 0.5);
36  if (numerator == denominator) {
37  carry = 1;
38  return 0;
39  }
40  carry = 0;
41  return static_cast<time_t>(numerator);
42  }
43 
44  String to_zero_pad_str(time_t value, unsigned len = 2)
45  {
46  const String nopad = to_dds_string(value);
47  if (len > nopad.size()) {
48  return String(len - nopad.size(), '0') + nopad;
49  }
50  return nopad;
51  }
52 }
53 
54 String TimeDuration::str(unsigned decimal_places, bool just_sec) const
55 {
56  String rv;
57  time_t sec = value().sec();
58  suseconds_t usec = value().usec();
59  bool negative = false;
60  if (sec < 0) {
61  negative = true;
62  sec = -sec;
63  }
64  if (usec < 0) {
65  negative = true;
66  usec = -usec;
67  }
68  if (negative) {
69  rv += "-";
70  }
71  time_t carry;
72  const time_t numerator = usec_to_rounded_frac(usec, decimal_places, carry);
73  const time_t seconds_total = sec + carry;
74  const time_t minutes_total = seconds_total / 60;
75  just_sec = just_sec || minutes_total == 0;
76  if (just_sec) {
77  rv += to_dds_string(seconds_total);
78  } else {
79  const time_t seconds = seconds_total % 60;
80  const time_t minutes = minutes_total % 60;
81  const time_t hours = minutes_total / 60;
82  if (hours > 0) {
83  rv += to_dds_string(hours) + ":" + to_zero_pad_str(minutes);
84  } else {
85  rv += to_dds_string(minutes);
86  }
87  rv += ":" + to_zero_pad_str(seconds);
88  }
89  if (decimal_places > 0) {
90  rv += "." + to_zero_pad_str(numerator, decimal_places);
91  }
92  if (just_sec) {
93  rv += " s";
94  }
95  return rv;
96 }
97 
98 }
99 }
100 
102 
103 #if !defined (__ACE_INLINE__)
104 # include "TimeDuration.inl"
105 #endif /* __ACE_INLINE__ */
const LogLevel::Value value
Definition: debug.cpp:61
std::string String
String to_dds_string(unsigned short to_convert)
const ACE_Time_Value & value() const
time_t sec(void) const
static const TimeDuration zero_value
Definition: TimeDuration.h:31
suseconds_t usec(void) const
String str(unsigned decimal_places=3, bool just_sec=false) const
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
static const TimeDuration max_value
Definition: TimeDuration.h:32