-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-printer.h
54 lines (45 loc) · 1.31 KB
/
time-printer.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
#ifndef TIMEPRINTER_H
#define TIMEPRINTER_H
#include <ostream>
#include <chrono>
namespace details
{
template <class T>
struct time_unit_string
{};
std::ostream& operator<<(std::ostream &os, time_unit_string<std::chrono::nanoseconds>)
{
return os << "ns";
}
std::ostream& operator<<(std::ostream &os, time_unit_string<std::chrono::microseconds>)
{
return os << "us";
}
std::ostream& operator<<(std::ostream &os, time_unit_string<std::chrono::milliseconds>)
{
return os << "ms";
}
std::ostream& operator<<(std::ostream &os, time_unit_string<std::chrono::seconds>)
{
return os << "s";
}
}
class time_printer_t
{
public:
template <class T, class R = std::chrono::milliseconds>
void print_to(std::ostream &os, const T &server_time, const T &client_time)
{
const auto &diff_time = server_time - client_time;
using namespace std::chrono;
os << "server "
<< duration_cast<R>(server_time.time_since_epoch()).count()
<< ", client "
<< duration_cast<R>(client_time.time_since_epoch()).count()
<< ", diff "
<< duration_cast<R>(diff_time).count() << ' '
<< details::time_unit_string<R>{}
<< std::endl;
}
};
#endif // TIMEPRINTER_H