diff options
author | Kevron Rees <kevron.m.rees@intel.com> | 2015-01-14 15:02:37 -0800 |
---|---|---|
committer | Kevron Rees <kevron.m.rees@intel.com> | 2015-01-26 13:15:56 -0800 |
commit | f66e7a8bf29b6fe290815084331b459bf47351a6 (patch) | |
tree | 0fad8264740ed3fb06e1bf164c5f524b2bd24750 | |
parent | 3ae1e967bf23b7fb41364308562608d4b391c5ac (diff) | |
download | automotive-message-broker-f66e7a8bf29b6fe290815084331b459bf47351a6.tar.gz |
Added Timestamp class
-rw-r--r-- | lib/asyncqueue.hpp | 2 | ||||
-rw-r--r-- | lib/timestamp.cpp | 40 | ||||
-rw-r--r-- | lib/timestamp.h | 20 |
3 files changed, 62 insertions, 0 deletions
diff --git a/lib/asyncqueue.hpp b/lib/asyncqueue.hpp index fe396d7e..88cd7109 100644 --- a/lib/asyncqueue.hpp +++ b/lib/asyncqueue.hpp @@ -64,7 +64,9 @@ public: } if(!mQueue.size()) + { throw std::runtime_error("nothing in queue"); + } auto itr = mQueue.begin(); diff --git a/lib/timestamp.cpp b/lib/timestamp.cpp index 112a7d1d..33e01823 100644 --- a/lib/timestamp.cpp +++ b/lib/timestamp.cpp @@ -6,9 +6,49 @@ double amb::currentTime() { + return Timestamp::instance()->currentTime(); +} + +amb::Timestamp* amb::Timestamp::mInstance = nullptr; + +amb::Timestamp::Timestamp() +{ + auto tm = std::chrono::system_clock::now(); + auto tm2 = std::chrono::steady_clock::now(); + + double eTime = (std::chrono::duration_cast<std::chrono::milliseconds>(tm.time_since_epoch()).count() / 1000.00); + double sTime = (std::chrono::duration_cast<std::chrono::milliseconds>(tm2.time_since_epoch()).count() / 1000.00); + + startTimeEpoch = eTime - sTime; +} + +double amb::Timestamp::currentTime() +{ auto tm = std::chrono::steady_clock::now(); double time = std::chrono::duration_cast<std::chrono::milliseconds>(tm.time_since_epoch()).count() / 1000.00; return time; } + +double amb::Timestamp::epochTime(double time) +{ + return startTimeEpoch + time; +} + +double amb::Timestamp::epochTime() +{ + auto tm = std::chrono::system_clock::now(); + + double time = std::chrono::duration_cast<std::chrono::milliseconds>(tm.time_since_epoch()).count() / 1000.00; + + return time; +} + +amb::Timestamp* amb::Timestamp::instance() +{ + if(!mInstance) + mInstance = new Timestamp(); + + return mInstance; +} diff --git a/lib/timestamp.h b/lib/timestamp.h index 745bc13b..5300488b 100644 --- a/lib/timestamp.h +++ b/lib/timestamp.h @@ -6,6 +6,26 @@ namespace amb { double currentTime(); +class Timestamp { +protected: + Timestamp(); + +public: + + double currentTime(); + + double epochTime(double time); + + double epochTime(); + +public: + static Timestamp *instance(); + +private: + double startTimeEpoch; + static Timestamp* mInstance; +}; + } #endif |