summaryrefslogtreecommitdiff
path: root/lib/timestamp.h
blob: d654d3f74ed36b077a5ff557c57c16ee2815474c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef _TIMESTAMP_H___
#define _TIMESTAMP_H___

#include <time.h>
#include <linux/can/bcm.h>

namespace amb {

double currentTime();

/*!
 * \brief The Timestamp class provides system time and monotonic time helper functions
 * Timestamp is meant to be a singleton class.  Access through instance().
 * \code
 * double currentMonotonicTime = amb::Timestamp::instance()->currentTime();
 * double epocTimeForMonotonicTime = amb::Timestamp::instance()->epochTime(currentMonotonicTime);
 * \endcode
 */
class Timestamp {
protected:
	Timestamp();

public:

	/*!
	 * \brief currentTime
	 * \return current monotonic (steady) time in seconds.
	 */
	double currentTime();
	double currentTime(double time);

	/*!
	 * \brief epochTime
	 * \param time monotonic time usually from currentTime()
	 * \return number of seconds.milliseconds since unix epoch
	 */
	double epochTime(double time);

	/*!
	 * \brief epochTime
	 * \return current system time in seconds since unix epoch
	 */
	double epochTime();

    static double fromTimeval(const struct ::timeval& tv);
    static struct ::timeval toTimeval(const double time);

    static struct ::bcm_timeval toBcmTimeval(const double time);

public:
	/*!
	 * \brief instance
	 * \return instance of Timestamp;
	 */
	static Timestamp *instance();

private:
	double startTimeEpoch;
	static Timestamp* mInstance;
};

}

#endif