On 04/06/2010 10:29 PM, Andrew Stitcher wrote:
On Tue, 2010-04-06 at 15:33 -0400, Alan Conway wrote:
I need to (de)serialize AbsTime timestamps. I can convert an AbsTime to a
uint64_t with uint64_t(Duration(abstime)) but I don't see how to convert back to
an AbsTime. I can just add a constructor but wanted to check with you if I'm
missing the intent.

The problem here is that fundamentally the class is designed not to show
it's epoch value. The fact you can wheedle it out by going via a
Duration is not intentional!

[BTW Duration&  AbsTime encapsulate an int64_t so using uint64_t is
incorrect. Both are allowed to be negative - Duration -ve indicates
before the time you asked about, AbsTime -ve indicates before the Epoch]

The external representation can't simply be the nanosec count in AbsTime
as different platforms have different Epochs. This is certainly true
between Windows and Unix.

I guess there needs to be an explicit way to convert a specific
date/time to an AbsTime so that you can use the existing differencing
operations as you can get a Duration out of the difference between the
AbsTime you're serialising and some fixed Epoch AbsTime. And equally you
can construct an AbsTime out of an Epoch AbsTime and a Duration.

Actually I suppose you could just declare a new static AbsTime as the
Epoch and make sure it represents the same time across all platforms.
Then use it for serialisation. That would avoid needing to do the
general Date->AbsTime thing.

This has rambled a bit, get back to me if it's not clear, I could add an
Epoch AbsTime for you if it's not clear how to do it fairly easily.


Here's a patch to add an EPOCH constant to Time.h, Steve can you check that I've got the windows part right?
diff --git a/qpid/cpp/include/qpid/sys/Time.h b/qpid/cpp/include/qpid/sys/Time.h
index ad05b0d..eb1701c 100644
--- a/qpid/cpp/include/qpid/sys/Time.h
+++ b/qpid/cpp/include/qpid/sys/Time.h
@@ -88,7 +88,9 @@ class AbsTime {
     TimePrivate timepoint;
 
 public:
-    QPID_COMMON_EXTERN inline AbsTime() {}
+    static const AbsTime EPOCH; // The Unix epoch: 1970-01-01T00:00:00
+    
+    QPID_COMMON_EXTERN inline AbsTime() : timepoint() {}
     QPID_COMMON_EXTERN AbsTime(const AbsTime& time0, const Duration& duration);
     // Default assignment operation fine
     // Default copy constructor fine
diff --git a/qpid/cpp/src/qpid/sys/posix/Time.cpp b/qpid/cpp/src/qpid/sys/posix/Time.cpp
index 0734abd..e094742 100644
--- a/qpid/cpp/src/qpid/sys/posix/Time.cpp
+++ b/qpid/cpp/src/qpid/sys/posix/Time.cpp
@@ -35,6 +35,8 @@ int64_t max_abstime() { return std::numeric_limits<int64_t>::max(); }
 namespace qpid {
 namespace sys {
 
+const AbsTime AbsTime::EPOCH;
+
 AbsTime::AbsTime(const AbsTime& t, const Duration& d) :
     timepoint(d == Duration::max() ? max_abstime() : t.timepoint+d.nanosecs)
 {}
diff --git a/qpid/cpp/src/qpid/sys/windows/Time.cpp b/qpid/cpp/src/qpid/sys/windows/Time.cpp
index 1d7b94e..0cb302f 100644
--- a/qpid/cpp/src/qpid/sys/windows/Time.cpp
+++ b/qpid/cpp/src/qpid/sys/windows/Time.cpp
@@ -30,6 +30,8 @@ using namespace boost::posix_time;
 namespace qpid {
 namespace sys {
 
+const AbsTime AbsTime::EPOCH(boost::posix_time::from_time_t(0));
+
 AbsTime::AbsTime(const AbsTime& t, const Duration& d) {
     if (d == Duration::max()) {
         timepoint = ptime(max_date_time);

---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:[email protected]

Reply via email to