-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRealtimeClock.cpp
More file actions
43 lines (30 loc) · 768 Bytes
/
RealtimeClock.cpp
File metadata and controls
43 lines (30 loc) · 768 Bytes
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
// RealtimeClock.cpp
#include <time.h>
#include "RealtimeClock.h"
#ifdef __MACH__
#include <mach/mach_time.h>
#endif // #ifdef __MACH__
using namespace std;
double RealtimeClock::current_time() {
double ct;
#ifndef __MACH__
// Linux
struct timespec tp;
int result = clock_gettime(CLOCK_MONOTONIC, &tp);
if (result == 0) {
ct = tp.tv_sec + (1.0e-9 * tp.tv_nsec);
}
else {
// An error occurred while getting the time.
ct = 0.0;
}
#endif // #ifndef __MACH__
#ifdef __MACH__
// Darwin - Apple OS X
mach_timebase_info_data_t info;
mach_timebase_info(&info);
double conv_factor = (static_cast<double>(info.numer)) / (static_cast<double>(info.denom));
ct = mach_absolute_time() * conv_factor * 1.0e-9;
#endif // #ifdef __MACH__
return ct;
}