Merge pull request #67 from tripzero/trip
[profile/ivi/automotive-message-broker.git] / lib / timestamp.h
1 #ifndef _TIMESTAMP_H___
2 #define _TIMESTAMP_H___
3
4 #include <time.h>
5 #include <linux/can/bcm.h>
6
7 namespace amb {
8
9 double currentTime();
10
11 /*!
12  * \brief The Timestamp class provides system time and monotonic time helper functions
13  * Timestamp is meant to be a singleton class.  Access through instance().
14  * \code
15  * double currentMonotonicTime = amb::Timestamp::instance()->currentTime();
16  * double epocTimeForMonotonicTime = amb::Timestamp::instance()->epochTime(currentMonotonicTime);
17  * \endcode
18  */
19 class Timestamp {
20 protected:
21         Timestamp();
22
23 public:
24
25         /*!
26          * \brief currentTime
27          * \return current monotonic (steady) time in seconds.
28          */
29         double currentTime();
30         double currentTime(double time);
31
32         /*!
33          * \brief epochTime
34          * \param time monotonic time usually from currentTime()
35          * \return number of seconds.milliseconds since unix epoch
36          */
37         double epochTime(double time);
38
39         /*!
40          * \brief epochTime
41          * \return current system time in seconds since unix epoch
42          */
43         double epochTime();
44
45     static double fromTimeval(const struct ::timeval& tv);
46     static struct ::timeval toTimeval(const double time);
47
48     static struct ::bcm_timeval toBcmTimeval(const double time);
49
50 public:
51         /*!
52          * \brief instance
53          * \return instance of Timestamp;
54          */
55         static Timestamp *instance();
56
57 private:
58         double startTimeEpoch;
59         static Timestamp* mInstance;
60 };
61
62 }
63
64 #endif