14 #ifndef MYNTEYE_UTIL_TIMES_H_ 15 #define MYNTEYE_UTIL_TIMES_H_ 27 #include "mynteyed/stubs/global.h" 29 MYNTEYE_BEGIN_NAMESPACE
33 using clock = std::chrono::system_clock;
38 using nanoseconds = std::chrono::nanoseconds;
39 using microseconds = std::chrono::microseconds;
40 using milliseconds = std::chrono::milliseconds;
41 using seconds = std::chrono::seconds;
42 using minutes = std::chrono::minutes;
43 using hours = std::chrono::hours;
44 using days = std::chrono::duration<std::int64_t, std::ratio<86400>>;
48 template <
typename Duration>
49 Duration to_duration(
const std::int64_t& t) {
53 template <
typename Duration>
54 clock::time_point to_time_point(
const Duration& d) {
55 return clock::time_point(d);
58 template <
typename Duration>
59 clock::time_point to_time_point(
const std::int64_t& t) {
60 return clock::time_point(Duration{t});
63 inline clock::time_point to_time_point(std::tm* tm) {
64 return clock::from_time_t(std::mktime(tm));
67 inline struct std::tm* to_local_tm(
const clock::time_point& t) {
68 auto t_c = clock::to_time_t(t);
69 return std::localtime(&t_c);
72 inline struct std::tm* to_utc_tm(
const clock::time_point& t) {
73 auto t_c = clock::to_time_t(t);
74 return std::gmtime(&t_c);
79 template <
typename FromDuration,
typename ToDuration>
80 ToDuration cast(
const FromDuration& d) {
81 return std::chrono::duration_cast<ToDuration>(d);
84 template <
typename Duration>
85 Duration cast(
const clock::duration& d) {
86 return cast<clock::duration, Duration>(d);
89 template <
typename FromDuration,
typename ToDuration>
90 std::int64_t cast(
const std::int64_t& t) {
91 return cast<FromDuration, ToDuration>(FromDuration{t}).count();
94 template <
typename Duration>
95 clock::time_point cast(
const clock::time_point& d) {
97 return std::chrono::time_point_cast<Duration>(d);
100 template <
typename Duration>
101 clock::duration cast_mod(
const clock::time_point& t) {
102 return t - cast<Duration>(t);
107 template <
typename FromDuration,
typename ToDuration>
108 std::int64_t count(
const FromDuration& d) {
109 return cast<FromDuration, ToDuration>(d).count();
112 template <
typename Duration>
113 std::int64_t count(
const clock::duration& d) {
114 return cast<Duration>(d).count();
119 inline std::tm* day_beg(std::tm* tm) {
126 inline std::tm* day_end(std::tm* tm) {
133 inline clock::time_point day_beg(
const clock::time_point& t) {
134 return cast<days>(t);
137 inline clock::time_point day_end(
const clock::time_point& t) {
138 return day_beg(t) + days(1) - clock::duration(1);
141 inline clock::duration day_time(
const clock::time_point& t) {
142 return cast_mod<days>(t);
147 template <
typename Duration>
148 std::int64_t between(
149 const clock::time_point& t1,
const clock::time_point& t2) {
150 return count<Duration>(t2 - t1);
153 inline std::int64_t between_days(
154 const clock::time_point& t1,
const clock::time_point& t2) {
155 return between<days>(day_beg(t1), day_beg(t2));
158 template <
typename Duration>
159 std::int64_t between_days(
const std::int64_t& t1,
const std::int64_t& t2) {
160 return between_days(to_time_point<Duration>(t1), to_time_point<Duration>(t2));
165 inline clock::time_point epoch() {
166 return clock::time_point(clock::duration{0});
169 template <
typename Duration>
170 std::int64_t since_epoch(
const clock::time_point& t) {
171 return count<Duration>(t.time_since_epoch());
176 inline clock::time_point now() {
180 template <
typename Duration>
182 return since_epoch<Duration>(now());
187 inline std::string to_string(
188 const clock::time_point& t,
const std::tm* tm,
189 const char* fmt =
"%F %T", std::int32_t precision = 6) {
190 std::stringstream ss;
191 #if defined(MYNTEYE_OS_ANDROID) || defined(MYNTEYE_OS_LINUX) 193 strftime(foo,
sizeof(foo), fmt, tm);
196 ss << std::put_time(tm, fmt);
201 ss <<
'.' << std::setfill(
'0') << std::setw(precision)
202 <<
static_cast<std::int32_t
>(
203 count<microseconds>(cast_mod<seconds>(t)) /
204 std::pow(10, 6 - precision));
209 inline std::string to_local_string(
210 const clock::time_point& t,
const char* fmt =
"%F %T",
211 const std::int32_t& precision = 6) {
212 return to_string(t, to_local_tm(t), fmt, precision);
215 inline std::string to_utc_string(
216 const clock::time_point& t,
const char* fmt =
"%F %T",
217 const std::int32_t& precision = 6) {
218 return to_string(t, to_utc_tm(t), fmt, precision);
223 MYNTEYE_END_NAMESPACE
225 #endif // MYNTEYE_UTIL_TIMES_H_