MYNT EYE D SDK  1.7.1
http://www.myntai.com/mynteye/depth
times.h
1 // Copyright 2018 Slightech Co., Ltd. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #ifndef MYNTEYE_UTIL_TIMES_H_
15 #define MYNTEYE_UTIL_TIMES_H_
16 #pragma once
17 
18 #include <chrono>
19 #include <cmath>
20 #include <cstdint>
21 #include <ctime>
22 #include <iomanip>
23 #include <ratio>
24 #include <sstream>
25 #include <string>
26 
27 #include "mynteyed/stubs/global.h"
28 
29 MYNTEYE_BEGIN_NAMESPACE
30 
31 namespace times {
32 
33 using clock = std::chrono::system_clock;
34 
35 // template<class Rep, class Period = std::ratio<1>>
36 // using duration = std::chrono::duration<Rep, Period>;
37 
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>>;
45 
46 // to
47 
48 template <typename Duration>
49 Duration to_duration(const std::int64_t& t) {
50  return Duration{t};
51 }
52 
53 template <typename Duration>
54 clock::time_point to_time_point(const Duration& d) {
55  return clock::time_point(d);
56 }
57 
58 template <typename Duration>
59 clock::time_point to_time_point(const std::int64_t& t) {
60  return clock::time_point(Duration{t});
61 }
62 
63 inline clock::time_point to_time_point(std::tm* tm) {
64  return clock::from_time_t(std::mktime(tm));
65 }
66 
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);
70 }
71 
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);
75 }
76 
77 // cast
78 
79 template <typename FromDuration, typename ToDuration>
80 ToDuration cast(const FromDuration& d) {
81  return std::chrono::duration_cast<ToDuration>(d);
82 }
83 
84 template <typename Duration>
85 Duration cast(const clock::duration& d) {
86  return cast<clock::duration, Duration>(d);
87 }
88 
89 template <typename FromDuration, typename ToDuration>
90 std::int64_t cast(const std::int64_t& t) {
91  return cast<FromDuration, ToDuration>(FromDuration{t}).count();
92 }
93 
94 template <typename Duration>
95 clock::time_point cast(const clock::time_point& d) {
96  // C++17, floor
97  return std::chrono::time_point_cast<Duration>(d);
98 }
99 
100 template <typename Duration>
101 clock::duration cast_mod(const clock::time_point& t) {
102  return t - cast<Duration>(t);
103 }
104 
105 // count
106 
107 template <typename FromDuration, typename ToDuration>
108 std::int64_t count(const FromDuration& d) {
109  return cast<FromDuration, ToDuration>(d).count();
110 }
111 
112 template <typename Duration>
113 std::int64_t count(const clock::duration& d) {
114  return cast<Duration>(d).count();
115 }
116 
117 // day
118 
119 inline std::tm* day_beg(std::tm* tm) {
120  tm->tm_hour = 0;
121  tm->tm_min = 0;
122  tm->tm_sec = 0;
123  return tm;
124 }
125 
126 inline std::tm* day_end(std::tm* tm) {
127  tm->tm_hour = 23;
128  tm->tm_min = 59;
129  tm->tm_sec = 59;
130  return tm;
131 }
132 
133 inline clock::time_point day_beg(const clock::time_point& t) {
134  return cast<days>(t);
135 }
136 
137 inline clock::time_point day_end(const clock::time_point& t) {
138  return day_beg(t) + days(1) - clock::duration(1);
139 }
140 
141 inline clock::duration day_time(const clock::time_point& t) {
142  return cast_mod<days>(t);
143 }
144 
145 // between
146 
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);
151 }
152 
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));
156 }
157 
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));
161 }
162 
163 // epoch
164 
165 inline clock::time_point epoch() {
166  return clock::time_point(clock::duration{0});
167 }
168 
169 template <typename Duration>
170 std::int64_t since_epoch(const clock::time_point& t) {
171  return count<Duration>(t.time_since_epoch());
172 }
173 
174 // now
175 
176 inline clock::time_point now() {
177  return clock::now();
178 }
179 
180 template <typename Duration>
181 std::int64_t now() {
182  return since_epoch<Duration>(now());
183 }
184 
185 // string
186 
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)
192  char foo[20];
193  strftime(foo, sizeof(foo), fmt, tm);
194  ss << foo;
195 #else
196  ss << std::put_time(tm, fmt);
197 #endif
198  if (precision > 0) {
199  if (precision > 6)
200  precision = 6;
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));
205  }
206  return ss.str();
207 }
208 
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);
213 }
214 
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);
219 }
220 
221 } // namespace times
222 
223 MYNTEYE_END_NAMESPACE
224 
225 #endif // MYNTEYE_UTIL_TIMES_H_