summaryrefslogtreecommitdiff
path: root/lib/timestamp.cpp
blob: 9c25902d002e534908332fd01e99e4bfd5732da0 (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
65
66
67
68
69
70
71
72
73
74
75
#include "timestamp.h"

#include <math.h>
#include <time.h>
#include <iostream>
#include <chrono>

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::currentTime(double time)
{
    return time - startTimeEpoch;
}

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;
}

double amb::Timestamp::fromTimeval(const struct ::timeval &tv)
{
    return tv.tv_sec*1.0 + tv.tv_usec*1e-6;
}

struct ::timeval amb::Timestamp::toTimeval(const double time)
{
    return { (__time_t) time, (__suseconds_t)fmod(time*1e6, 1e6) };
}

struct ::bcm_timeval amb::Timestamp::toBcmTimeval(const double time)
{
    return { (long) time, (long)fmod(time*1e6, 1e6) };
}