-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchrono.h
75 lines (63 loc) · 2.2 KB
/
chrono.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
67
68
69
70
71
72
73
74
#ifndef COOL_CHRONO_H_
#define COOL_CHRONO_H_
#include <cool/ratio.h>
#include <array>
#include <chrono>
#include <ctime>
#include <ostream>
///////////////////////////////////////////////////////////////////////////////
// Stream inserters operator for things in std::chrono
//
// std::chrono::duration
//
// Usage:
// using cool::chrono::duration::operator<<;
// std::cout << std::chrono::hours{2} << std::endl; // outputs "2 hours"
//
//
// std::chrono::system_clock
//
// Usage:
// using cool::chrono::system_clock::operator<<;
// std::cout << std::chrono::system_clock{} << std::endl; // outputs "Thu, 12 Aug 1965 12:59:00 -0500"
//
// now() - returns a '\0'-terminated array containing the formatted system_clock
///////////////////////////////////////////////////////////////////////////////
namespace cool { namespace chrono {
namespace duration {
template<typename Rep, intmax_t N, intmax_t D>
std::ostream& operator<<(std::ostream& os, std::chrono::duration<Rep, std::ratio<N, D>> const& that)
{
os << that.count() << ' ';
if (N == 3600 && D == 1)
os << "hours";
else if (N == 60 && D == 1)
os << "minutes";
else if (N == 1 && D == 1)
os << "seconds";
else
{
using cool::ratio::operator<<;
os << std::ratio<N, D>{} << "seconds";
}
return os;
}
} // duration namespace
namespace system_clock
{
inline std::array<char, sizeof "Thu, 12 Aug 1965 12:59:00 -0500"> now()
{
std::chrono::system_clock::time_point tp{std::chrono::system_clock::now()};
std::time_t timeT{std::chrono::system_clock::to_time_t(tp)};
struct tm tm;
localtime_r(&timeT, &tm);
std::array<char, sizeof "Thu, 12 Aug 1965 12:59:00 -0500"> formatted;
strftime(formatted.data(), formatted.size(), "%a, %d %b %Y %T %z", &tm);
return formatted;
}
inline
std::ostream& operator<<(std::ostream& os, std::chrono::system_clock)
{ return os << cool::chrono::system_clock::now().data(); }
} // system_clock namespace
} /* chrono namespace */ } /* cool namespace */
#endif /* COOL_CHRONO_H_ */