summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--qpid/cpp/src/qpid/broker/Timer.cpp2
-rw-r--r--qpid/cpp/src/qpid/sys/Time.h61
-rw-r--r--qpid/cpp/src/qpid/sys/posix/Time.cpp2
-rw-r--r--qpid/cpp/src/tests/amqp_0_10/serialize.cpp10
4 files changed, 48 insertions, 27 deletions
diff --git a/qpid/cpp/src/qpid/broker/Timer.cpp b/qpid/cpp/src/qpid/broker/Timer.cpp
index 33261dad6e..9005a7cd6e 100644
--- a/qpid/cpp/src/qpid/broker/Timer.cpp
+++ b/qpid/cpp/src/qpid/broker/Timer.cpp
@@ -36,7 +36,7 @@ TimerTask::TimerTask(AbsTime _time) :
TimerTask::~TimerTask(){}
-void TimerTask::reset() { time.reset(AbsTime::now(), duration); }
+void TimerTask::reset() { time = AbsTime(AbsTime::now(), duration); }
Timer::Timer() : active(false)
{
diff --git a/qpid/cpp/src/qpid/sys/Time.h b/qpid/cpp/src/qpid/sys/Time.h
index 860fc49097..6501cd0806 100644
--- a/qpid/cpp/src/qpid/sys/Time.h
+++ b/qpid/cpp/src/qpid/sys/Time.h
@@ -31,39 +31,75 @@ namespace sys {
class Duration;
-/** Times in nanoseconds */
+/** Class to represent an instant in time:
+ * The time resolution is in nanosecs, and this is held with 64 bits
+ * giving a total time span from about 25 million years ago to 25 million
+ * years hence. As an aside the internal time can sensibly be negative
+ * meaning before the epoch (probably 1/1/1970 although this class doesn't
+ * care).
+ *
+ * The AbsTime class is a value class and so you don't need to add any accessors
+ * to its internal state. If you think you want to replace its value,i
+ * You need to construct a new AbsTime and assign it, viz:
+ *
+ * AbsTime when = AbsTime::now();
+ * ...
+ * when = AbsTime(when, 2*TIME_SEC); // Advance timer 2 secs
+ *
+ * If for some reason you need access to the internal nanosec value you need
+ * to convert the AbsTime to a Duration and use its conversion to int64_t, viz:
+ *
+ * AbsTime now = AbsTime::now();
+ *
+ * int64_t ns = Duration(now);
+ *
+ * However note that the nanosecond value that is returned here is not defined to be
+ * anything in particular and could vary from platform to platform.
+ *
+ * There are some sensible operations that are currently missing from AbsTime, but
+ * nearly all that's needed can be done with a mixture of AbsTimes and Durations.
+ *
+ * For example, convenience operators to add a Duration and AbsTime returning an AbsTime
+ * would fit here (although you can already perform the operation with one of the AbsTime
+ * constructors). However trying to add 2 AbsTimes doesn't make sense.
+ */
class AbsTime {
static int64_t max() { return std::numeric_limits<int64_t>::max(); }
int64_t time_ns;
- friend class Duration;
+ friend class Duration;
- public:
+public:
inline AbsTime() {}
inline AbsTime(const AbsTime& time0, const Duration& duration);
- // Default asignment operation fine
+ // Default assignment operation fine
// Default copy constructor fine
- inline void reset(const AbsTime& time0, const Duration& duration);
static AbsTime now();
inline static AbsTime FarFuture();
- int64_t timeValue() const { return time_ns; }
bool operator==(const AbsTime& t) const { return t.time_ns == time_ns; }
template <class S> void serialize(S& s) { s(time_ns); }
- friend bool operator<(const AbsTime& a, const AbsTime& b);
- friend bool operator>(const AbsTime& a, const AbsTime& b);
+ friend bool operator<(const AbsTime& a, const AbsTime& b);
+ friend bool operator>(const AbsTime& a, const AbsTime& b);
+ friend std::ostream& operator << (std::ostream&, const AbsTime&);
};
std::ostream& operator << (std::ostream&, const AbsTime&);
+/** Class to represent the duration between instants of time:
+ * As AbsTime this class also uses nanosecs for its time
+ * resolution. For the most part a duration can be dealt with like a
+ * 64 bit integer, and indeed there is an implicit conversion which
+ * makes this quite conveient.
+ */
class Duration {
static int64_t max() { return std::numeric_limits<int64_t>::max(); }
int64_t nanosecs;
- friend class AbsTime;
+ friend class AbsTime;
- public:
+public:
inline Duration(int64_t time0);
inline explicit Duration(const AbsTime& time0);
inline explicit Duration(const AbsTime& start, const AbsTime& finish);
@@ -76,11 +112,6 @@ AbsTime::AbsTime(const AbsTime& t, const Duration& d) :
time_ns(d == Duration::max() ? max() : t.time_ns+d.nanosecs)
{}
-void AbsTime::reset(const AbsTime& t, const Duration& d)
-{
- time_ns = d == Duration::max() ? max() : t.time_ns+d.nanosecs;
-}
-
AbsTime AbsTime::FarFuture() { AbsTime ff; ff.time_ns = max(); return ff;}
inline AbsTime now() { return AbsTime::now(); }
diff --git a/qpid/cpp/src/qpid/sys/posix/Time.cpp b/qpid/cpp/src/qpid/sys/posix/Time.cpp
index b2b7a81130..09627cdc6b 100644
--- a/qpid/cpp/src/qpid/sys/posix/Time.cpp
+++ b/qpid/cpp/src/qpid/sys/posix/Time.cpp
@@ -62,7 +62,7 @@ std::ostream& operator<<(std::ostream& o, const AbsTime& t) {
"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"
};
struct tm * timeinfo;
- time_t rawtime(t.timeValue()/TIME_SEC);
+ time_t rawtime(t.time_ns/TIME_SEC);
timeinfo = localtime (&rawtime);
char time_string[100];
sprintf ( time_string,
diff --git a/qpid/cpp/src/tests/amqp_0_10/serialize.cpp b/qpid/cpp/src/tests/amqp_0_10/serialize.cpp
index 7db9c8db5a..a8f66df215 100644
--- a/qpid/cpp/src/tests/amqp_0_10/serialize.cpp
+++ b/qpid/cpp/src/tests/amqp_0_10/serialize.cpp
@@ -52,16 +52,6 @@ std::ostream& operator<<(std::ostream& out, const array<T,N>& a) {
}
} // boost
-namespace qpid {
-namespace sys {
-
-std::ostream& operator<<(std::ostream& out, const AbsTime& t) {
- return out << t.timeValue();
-}
-}
-
-} // qpid
-
QPID_AUTO_TEST_SUITE(SerializeTestSuite)
using namespace std;