timestamp: fix currentTime() problem
[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 #ifndef HAVE_BCM_TIMEVAL
8
9 #define bcm_timeval timeval
10
11 #endif
12
13 namespace amb {
14
15 double currentTime();
16
17 /*!
18  * \brief The Timestamp class provides system time and monotonic time helper functions
19  * Timestamp is meant to be a singleton class.  Access through instance().
20  * \code
21  * double currentMonotonicTime = amb::Timestamp::instance()->currentTime();
22  * double epocTimeForMonotonicTime = amb::Timestamp::instance()->epochTime(currentMonotonicTime);
23  * \endcode
24  */
25 class Timestamp {
26 protected:
27         Timestamp();
28
29 public:
30
31         /*!
32          * \brief currentTime
33          * \return current monotonic (steady) time in seconds.
34          */
35         double currentTime();
36         double currentTime(double time);
37
38         /*!
39          * \brief epochTime
40          * \param time monotonic time usually from currentTime()
41          * \return number of seconds.milliseconds since unix epoch
42          */
43         double epochTime(double time);
44
45         /*!
46          * \brief epochTime
47          * \return current system time in seconds since unix epoch
48          */
49         double epochTime();
50
51     static double fromTimeval(const struct ::timeval& tv);
52     static struct ::timeval toTimeval(const double time);
53
54     static struct ::bcm_timeval toBcmTimeval(const double time);
55
56 public:
57         /*!
58          * \brief instance
59          * \return instance of Timestamp;
60          */
61         static Timestamp *instance();
62
63 private:
64         double startTimeEpoch;
65         static Timestamp* mInstance;
66 };
67
68 }
69
70 #endif