OpenDDS  Snapshot(2023/04/28-20:55)
TimeDuration.h
Go to the documentation of this file.
1 #ifndef OPENDDS_DCPS_TIMEDURATION_H
2 #define OPENDDS_DCPS_TIMEDURATION_H
3 
4 #include "PoolAllocator.h"
5 #include "SafeBool_T.h"
6 
7 #include <dds/DdsDcpsCoreC.h>
8 
9 #include <ace/Time_Value.h>
10 
11 #ifndef ACE_LACKS_PRAGMA_ONCE
12 # pragma once
13 #endif
14 
16 
17 namespace OpenDDS {
18 namespace DCPS {
19 
20 /**
21  * Represents a length of time and based on C++11 std::chrono::duration.
22  *
23  * This wraps an ACE_Time_Value, and is designed to work with TimePoint_T.
24  *
25  * See https://opendds.readthedocs.io/en/master/internal/dev_guidelines.html#time
26  * (or docs/internal/dev_guidelines.rst) for background and reasoning for this
27  * class.
28  */
29 class OpenDDS_Dcps_Export TimeDuration : public SafeBool_T<TimeDuration> {
30 public:
31  const static TimeDuration zero_value;
32  const static TimeDuration max_value;
33 
34  /**
35  * Return a TimeDuration equivalent to the given number of milliseconds.
36  */
37  static TimeDuration from_msec(const ACE_UINT64& ms);
38 
39  /**
40  * Return a TimeDuration from fractional seconds.
41  */
42  static TimeDuration from_double(double duration);
43 
44  /**
45  * Initialized to zero
46  */
47  TimeDuration();
48 
49  TimeDuration(const TimeDuration& other);
50 
51  /**
52  * Copy the ACE_Time_Value directly.
53  *
54  * \warning It will accept a ACE_Time_Value that represents a specific point
55  * in time, but it is wrong to do so. Use one of the TimePoint_T classes
56  * defined in TimeTypes.h that match the clock type.
57  */
58  explicit TimeDuration(const ACE_Time_Value& ace_time_value);
59 
60  /**
61  * Define the number of seconds and optionally microseconds, passed to
62  * ACE_Time_Value's constructor of the same signature.
63  */
64  explicit TimeDuration(time_t sec, suseconds_t usec = 0);
65 
66  /**
67  * Converts the DDS Duration into the equivalent value.
68  */
69  explicit TimeDuration(const DDS::Duration_t& dds_duration);
70 
71  const ACE_Time_Value& value() const;
72  void value(const ACE_Time_Value& ace_time_value);
73  bool is_zero() const;
74  bool is_max() const;
75  DDS::Duration_t to_dds_duration() const;
76 
77  bool boolean_test() const
78  {
79  return value_ != zero_value.value();
80  }
81 
82  /**
83  * Convert to a string in a humanized format:
84  * SECONDS.FRACTIONAL s
85  * if the time is less than a minute or just_sec is true, else:
86  * [[HOURS:]MINUTES:]SECONDS.FRACTIONAL
87  *
88  * decimal_places is the number of decimal places to round to in the
89  * fractional seconds.
90  */
91  String str(unsigned decimal_places = 3, bool just_sec = false) const;
92 
93  String sec_str(unsigned decimal_places = 3) const
94  {
95  return str(decimal_places, true);
96  }
97 
98  TimeDuration& operator+=(const TimeDuration& other);
99  TimeDuration& operator-=(const TimeDuration& other);
100  TimeDuration& operator=(const TimeDuration& other);
101  TimeDuration& operator=(const time_t& other);
102  TimeDuration& operator*=(double other);
103  TimeDuration& operator/=(double other);
104 
105 protected:
107 };
108 
115 OpenDDS_Dcps_Export double operator/(const TimeDuration& x, const TimeDuration& y);
116 OpenDDS_Dcps_Export bool operator<(const TimeDuration& x, const TimeDuration& y);
117 OpenDDS_Dcps_Export bool operator>(const TimeDuration& x, const TimeDuration& y);
122 
123 } // namespace DCPS
124 } // namespace OpenDDS
125 
127 
128 #ifdef __ACE_INLINE__
129 # include "TimeDuration.inl"
130 #endif
131 
132 #endif
const LogLevel::Value value
Definition: debug.cpp:61
std::string String
OpenDDS_Dcps_Export TimeDuration operator/(const TimeDuration &x, double y)
ACE_INLINE OpenDDS_Dcps_Export DDS::Duration_t operator-(const DDS::Time_t &t1, const DDS::Time_t &t2)
Value value_
#define OpenDDS_Dcps_Export
Definition: dcps_export.h:24
ACE_INLINE OpenDDS_Dcps_Export bool operator>(const DDS::Duration_t &t1, const DDS::Duration_t &t2)
Definition: Time_Helper.inl:65
const ACE_Time_Value & value() const
bool operator==(const DisjointSequence::OrderedRanges< T > &a, const DisjointSequence::OrderedRanges< T > &b)
ACE_INLINE OpenDDS_Dcps_Export bool operator<=(const DDS::Duration_t &t1, const DDS::Duration_t &t2)
Definition: Time_Helper.inl:55
String sec_str(unsigned decimal_places=3) const
Definition: TimeDuration.h:93
bool operator>=(const LogLevel &ll, LogLevel::Value value)
Definition: debug.h:61
SequenceNumber operator+(const SequenceNumber &lhs, int rhs)
static const TimeDuration zero_value
Definition: TimeDuration.h:31
bool operator!=(const GUID_t &lhs, const GUID_t &rhs)
Definition: GuidUtils.h:125
unsigned long long ACE_UINT64
SequenceNumber operator+=(SequenceNumber &lhs, int rhs)
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
OpenDDS_Dcps_Export TimeDuration operator*(double x, const TimeDuration &y)
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
bool operator<(const GUID_t &lhs, const GUID_t &rhs)
Definition: GuidUtils.h:80
static const TimeDuration max_value
Definition: TimeDuration.h:32