This repository was archived by the owner on Sep 7, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathlibtime.hpp
More file actions
116 lines (92 loc) · 3.25 KB
/
Copy pathlibtime.hpp
File metadata and controls
116 lines (92 loc) · 3.25 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// version 1.0
#pragma once
#include <cstdlib>
#include <ctime>
#ifdef _WIN32
#include <windows.h>
void usleep(__int64 usec) {
HANDLE timer;
LARGE_INTEGER ft;
ft.QuadPart = -(10*usec);
timer = CreateWaitableTimer(NULL, TRUE, NULL);
SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
WaitForSingleObject(timer, INFINITE);
CloseHandle(timer);
}
int clock_gettime(int, struct timespec *spec) //C-file part
{ __int64 wintime; GetSystemTimeAsFileTime((FILETIME*)&wintime);
wintime -=116444736000000000LL; //1jan1601 to 1jan1970
spec->tv_sec =wintime / 10000000LL; //seconds
spec->tv_nsec =wintime % 10000000LL*100; //nano-seconds
return 0;
}
#else
#include <unistd.h>
#include <sys/timerfd.h>
#endif
#include "libfd.hpp"
namespace Time {
enum ClockType {
SYSTEM_CLOCK,
TIMER_CLOCK
};
enum TimeFraction {
HOURS,
MINUTES,
SECONDS,
MILLISECONDS,
MICROSECONDS,
NANOSECONDS
};
int64_t to_nanoseconds(double value, TimeFraction frac) {
switch (frac) {
case HOURS: return value * 3600000000000LL;
case MINUTES: return value * 60000000000LL;
case SECONDS: return value * 1000000000LL;
case MILLISECONDS: return value * 1000000LL;
case MICROSECONDS: return value * 1000LL;
case NANOSECONDS: return static_cast<int64_t>(value);
default: return 0;
}
}
double from_nanoseconds(int64_t nanoseconds, TimeFraction frac) {
switch (frac) {
case HOURS: return nanoseconds / 3600000000000.0;
case MINUTES: return nanoseconds / 60000000000.0;
case SECONDS: return nanoseconds / 1000000000.0;
case MILLISECONDS: return nanoseconds / 1000000.0;
case MICROSECONDS: return nanoseconds / 1000.0;
case NANOSECONDS: return static_cast<double>(nanoseconds);
default: return 0.0;
}
}
double time_cast(double time, TimeFraction from, TimeFraction to) {
return from_nanoseconds(to_nanoseconds(time, from), to);
}
int64_t clock_raw(ClockType type) {
timespec ts;
clock_gettime(type, &ts);
return ts.tv_sec * 1'000'000'000 + ts.tv_nsec;
}
double clock(ClockType type, TimeFraction frac = MICROSECONDS) {
int64_t raw_time = clock_raw(type);
return time_cast(raw_time, NANOSECONDS, frac);
}
void sleep(double time, TimeFraction frac = MICROSECONDS) {
double sleep_time = time_cast(time, frac, MICROSECONDS);
usleep(sleep_time);
}
// FileDescriptor fd_timer(double interval, TimeFraction frac = MICROSECONDS, ClockType clock = SYSTEM_CLOCK) {
// FileDescriptor fd = timerfd_create(clock, 0);
// double time = time_cast(interval, frac, SECONDS);
// long time_sec = time;
// long time_nsec = time_cast(time - time_sec, SECONDS, NANOSECONDS);
// itimerspec tspec{0};
// tspec.it_value.tv_sec = time_sec;
// tspec.it_value.tv_nsec = time_nsec;
// tspec.it_interval.tv_sec = time_sec;
// tspec.it_interval.tv_nsec = time_nsec;
// timerfd_settime(fd, 0, &tspec, nullptr);
// return fd;
// }
}