-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchrono.h
More file actions
89 lines (75 loc) · 2.21 KB
/
chrono.h
File metadata and controls
89 lines (75 loc) · 2.21 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
#pragma once
#include <chrono>
#include <thread>
#include <stdio.h>
#include <inttypes.h>
namespace chrono
{
// epoch time in us
inline uint64_t Now()
{
return std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}
// elapsed time since start
inline uint64_t Current(uint64_t startTimeUs)
{
return Now() - startTimeUs;
}
// time to wait for presentation time
inline int64_t Wait(uint64_t startTimeUs, uint64_t presentationTimeUs)
{
return presentationTimeUs - Current(startTimeUs);
}
// convert microseconds to seconds
template <typename T>
inline double Seconds(T timeUs)
{
double t = static_cast<double>(timeUs);
return t / 1000000.0;
}
// convert microseconds to milliseconds
template <typename T>
inline double Milliseconds(T timeUs)
{
double t = static_cast<double>(timeUs);
return t / 1000.0;
}
// convert seconds to microseconds
inline uint64_t Microseconds(double seconds)
{
return static_cast<uint64_t>(seconds * 1000000.0);
}
inline void HoursMinutesSeconds(uint64_t timeUs, int64_t& hour, int64_t& min, int64_t& sec)
{
const int64_t secondsInHour = 3600;
const int64_t secondsInMinute = 60;
int64_t time = static_cast<int64_t>(Seconds(timeUs));
hour = time / secondsInHour;
time = time % secondsInHour;
min = time / secondsInMinute;
time = time % secondsInMinute;
sec = time;
}
inline std::string HoursMinutesSeconds(uint64_t timeUs)
{
int64_t hour = 0;
int64_t min = 0;
int64_t sec = 0;
HoursMinutesSeconds(timeUs, hour, min, sec);
char buf[BUFSIZ];
if (hour != 0)
{
snprintf(buf, sizeof(buf), "%" PRId64 "h%" PRId64 "m%" PRId64 "s", hour, min, sec);
}
else if (min != 0)
{
snprintf(buf, sizeof(buf), "%" PRId64 "m%" PRId64 "s", min, sec);
}
else
{
snprintf(buf, sizeof(buf), "%" PRId64 "s", sec);
}
return buf;
}
}