-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeConversion.h
66 lines (49 loc) · 1.45 KB
/
timeConversion.h
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
//
// https://embeddedartistry.com/blog/2019/01/31/converting-between-timespec-stdchrono/
//
#include <sys/time.h>
#include <chrono>
#include <ctime>
/***
clock_gettime(CLOCK_REALTIME, timespec* ts)
struct timespec {
time_t tv_sec;
long tv_nsec;
}
int gettimeofday(struct timeval* tp, void* tzp);
struct timeval
{
time_t tv_sec;
suseconds_t tv_usec;
}
***/
namespace {
using namespace std::chrono; // for example brevity
constexpr nanoseconds timespecToDuration(timespec ts)
{
auto duration = seconds{ts.tv_sec} + nanoseconds{ts.tv_nsec};
return duration_cast<nanoseconds>(duration);
}
constexpr timespec durationToTimespec(nanoseconds dur)
{
auto secs = duration_cast<seconds>(dur);
dur -= secs;
return timespec{secs.count(), dur.count()};
}
constexpr time_point<system_clock, nanoseconds> timespecToTimePoint(timespec ts)
{
return time_point<system_clock, nanoseconds>{
duration_cast<system_clock::duration>(timespecToDuration(ts))};
}
constexpr timespec timepointToTimespec(time_point<system_clock, nanoseconds> tp)
{
auto secs = time_point_cast<seconds>(tp);
auto ns = time_point_cast<nanoseconds>(tp) - time_point_cast<nanoseconds>(secs);
return timespec{secs.time_since_epoch().count(), ns.count()};
}
constexpr microseconds timevalToDuration(timeval tv)
{
auto duration = seconds{tv.tv_sec} + microseconds{tv.tv_usec};
return duration_cast<microseconds>(duration);
}
} // namespace