-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.h
399 lines (371 loc) · 11.3 KB
/
time.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// -*-coding: mule-utf-8-unix; fill-column: 58; -*-
/**
* @file
* The implementation of std::put_time (it's absent in
* GCC) in the Bare C++ style.
*
* This file (originally) was a part of public
* https://github.com/lodyagin/types repository.
*
* @author Sergei Lodyagin
* @copyright Copyright (c) 2014, Sergei Lodyagin
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that
* the following conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <ostream>
#include <cstdint>
#include <utility>
#include <chrono>
#include <ratio>
#include <ctime>
#include <iomanip>
#include <tuple>
//#include "types/string.h"
#ifndef COHORS_TYPES_TIME_H_
#define COHORS_TYPES_TIME_H_
namespace times {
namespace howard_hinnant {
//! Returns number of days since civil 1970-01-01. Negative values indicate
//! days prior to 1970-01-01.
//! Preconditions: y-m-d represents a date in the civil (Gregorian) calendar
//! m is in [1, 12]
//! d is in [1, last_day_of_month(y, m)]
//! y is "approximately" in
//! [numeric_limits<Int>::min()/366, numeric_limits<Int>::max()/366]
//! Exact range of validity is:
//! [civil_from_days(numeric_limits<Int>::min()),
//! civil_from_days(numeric_limits<Int>::max()-719468)]
//! Howard Hinnant
//! chrono-Compatible Low-Level Date Algorithms
//! http://home.roadrunner.com/~hinnant/date_algorithms.html
template <class Int>
// constexpr c++14
Int
days_from_civil(Int y, unsigned m, unsigned d) noexcept
{
static_assert(std::numeric_limits<unsigned>::digits >= 18,
"This algorithm has not been ported to a 16 bit unsigned integer");
static_assert(std::numeric_limits<Int>::digits >= 20,
"This algorithm has not been ported to a 16 bit signed integer");
y -= m <= 2;
const Int era = (y >= 0 ? y : y-399) / 400;
const unsigned yoe = static_cast<unsigned>(y - era * 400); // [0, 399]
const unsigned doy = (153*(m + (m > 2 ? -3 : 9)) + 2)/5 + d-1; // [0, 365]
const unsigned doe = yoe * 365 + yoe/4 - yoe/100 + doy; // [0, 146096]
return era * 146097 + static_cast<Int>(doe) - 719468;
}
//! Returns year/month/day triple in civil calendar
//! Preconditions: z is number of days since 1970-01-01 and is in the range:
//! [numeric_limits<Int>::min(), numeric_limits<Int>::max()-719468].
//! Howard Hinnant
//! chrono-Compatible Low-Level Date Algorithms
//! http://home.roadrunner.com/~hinnant/date_algorithms.html
template <class Int>
//constexpr (c++14)
std::tuple<Int, unsigned, unsigned>
civil_from_days(Int z) noexcept
{
static_assert(std::numeric_limits<unsigned>::digits >= 18,
"This algorithm has not been ported to a 16 bit unsigned integer");
static_assert(std::numeric_limits<Int>::digits >= 20,
"This algorithm has not been ported to a 16 bit signed integer");
z += 719468;
const Int era = (z >= 0 ? z : z - 146096) / 146097;
const unsigned doe = static_cast<unsigned>(z - era * 146097); // [0, 146096]
const unsigned yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365; // [0, 399]
const Int y = static_cast<Int>(yoe) + era * 400;
const unsigned doy = doe - (365*yoe + yoe/4 - yoe/100); // [0, 365]
const unsigned mp = (5*doy + 2)/153; // [0, 11]
const unsigned d = doy - (153*mp+2)/5 + 1; // [1, 31]
const unsigned m = mp + (mp < 10 ? 3 : -9); // [1, 12]
return std::tuple<Int, unsigned, unsigned>(y + (m <= 2), m, d);
}
//! Returns day of week in civil calendar [0, 6] -> [Sun, Sat]
//! Preconditions: z is number of days since 1970-01-01 and is in the range:
//! [numeric_limits<Int>::min(), numeric_limits<Int>::max()-4].
//! Howard Hinnant
//! chrono-Compatible Low-Level Date Algorithms
//! http://home.roadrunner.com/~hinnant/date_algorithms.html
template <class Int>
// constexpr c++14
unsigned weekday_from_days(Int z) noexcept
{
return static_cast<unsigned>(z >= -4 ? (z+4) % 7 : (z+5) % 7 + 6);
}
//! Howard Hinnant
//! chrono-Compatible Low-Level Date Algorithms
//! http://home.roadrunner.com/~hinnant/date_algorithms.html
template <class To, class Rep, class Period>
To round_down(const std::chrono::duration<Rep, Period>& d)
{
To t = std::chrono::duration_cast<To>(d);
if (t > d)
--t;
return t;
}
} // howard_hinnant
//! Converts std::chrono::time_point<system_clock> to
//! std::tm in UTC. Great thanks to Howard Hinnant,
//! http://stackoverflow.com/a/16784256/1326885
//! FIXME: leap seconds
//! (convert by the table at
//! http://en.wikipedia.org/wiki/Leap_seconds)
template <class Duration, class Clock>
std::tm make_utc_tm(
std::chrono::time_point<Clock, Duration> tp
)
{
using namespace std;
using namespace std::chrono;
using days = duration<
int,
typename std::ratio_multiply
<hours::period, std::ratio<24>>::type
>;
// t is time duration since 1970-01-01
Duration t = tp.time_since_epoch();
// d is days since 1970-01-01
days d = howard_hinnant::round_down<days>(t);
// t is now time duration since midnight of day d
t -= d;
// break d down into year/month/day
int year;
unsigned month;
unsigned day;
std::tie(year, month, day) =
howard_hinnant::civil_from_days(d.count());
// start filling in the tm with calendar info
#ifdef _WIN32
std::tm tm = {0};
#else
std::tm tm = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
#endif
tm.tm_year = year - 1900;
tm.tm_mon = month - 1;
tm.tm_mday = day;
tm.tm_wday = howard_hinnant::weekday_from_days(d.count());
tm.tm_yday = d.count() - howard_hinnant::days_from_civil(year, 1, 1);
// Fill in the time
tm.tm_hour = duration_cast<hours>(t).count();
t -= hours(tm.tm_hour);
tm.tm_min = duration_cast<minutes>(t).count();
t -= minutes(tm.tm_min);
tm.tm_sec = duration_cast<seconds>(t).count();
return tm;
}
#if 1
//! Log the current time when output to a stream
template<class CharT, class Clock, size_t slen>
class timestamp_t
{
public:
timestamp_t(const CharT(&fmt)[slen])
: format(fmt)
{}
const strings::basic_constexpr_string<CharT> format;
};
template<class Clock, size_t slen>
timestamp_t<char, Clock, slen> timestamp(
const char(&s)[slen]
)
{
return timestamp_t<char, Clock, slen>(s);
}
template<class Clock, size_t slen>
timestamp_t<wchar_t, Clock, slen> timestamp(
const wchar_t(&s)[slen]
)
{
return timestamp_t<wchar_t, Clock, slen>(s);
}
#endif
//! seconds from the last midnight
template <
class Clock,
class SDur,
class TDur = std::chrono::duration<typename SDur::rep>
// 1 sec per
>
TDur seconds_since_midnight
(std::chrono::time_point<Clock, SDur> time_point)
{
return std::chrono::duration_cast<TDur>
(time_point.time_since_epoch())
% std::chrono::hours(24);
}
template<
class CharT,
class Clock,
class Duration = typename Clock::duration//,
// uint8_t N = 0
>
struct put_time_t
{
using time_point = std::chrono::time_point<Clock, Duration>;
template<size_t N>
put_time_t(const time_point& p, const CharT(&f)[N], int frac)
: point(p), format(f), fraction(frac)
{}
#if 1
put_time_t(
const time_point& p,
strings::constexpr_string f,
int frac
)
: point(p), format(f.c_str()), fraction(frac)
{}
#endif
const time_point point;
const CharT* format;
int fraction;
};
template<
class CharT,
class Clock,
class Duration = typename Clock::duration,
uint8_t N = 0
>
put_time_t<CharT, Clock, Duration>
put_time(
const std::chrono::time_point<Clock, Duration>& time,
const CharT(&format)[N],
int fraction
)
{
return ::times::put_time_t<CharT, Clock, Duration>
(time, format, fraction);
}
template<
class CharT,
class Clock,
class Duration = typename Clock::duration
>
put_time_t<CharT, Clock, Duration>
put_time(
const std::chrono::time_point<Clock, Duration>& time,
const strings::basic_constexpr_string<CharT> format
)
{
return ::times::put_time_t<CharT, Clock, Duration>
(time, format);
}
template<
class CharT,
class Clock,
class Duration = typename Clock::duration,
uint8_t N = 0
>
void put_time(
std::basic_ios<CharT>& out,
const put_time_t<CharT, Clock, Duration>& t
)
{
const std::tm tmp = ::times::make_utc_tm(t.point);
using iterator = std::ostreambuf_iterator<CharT>;
using time_put = std::time_put<CharT, iterator>;
const time_put& tp= std::use_facet<time_put>(out.getloc());
const iterator end = tp.put(
iterator(out.rdbuf()),
out,
out.fill(),
&tmp,
t.format,
t.format
+ std::char_traits<CharT>::length(t.format)
);
if (end.failed())
{
out.setstate(std::ios_base::badbit);
return;
}
if (t.fraction <= 0)
return;
using rep = typename Duration::rep;
//using ns_duration = std::chrono::duration<rep, std::nano>;
using frac_time_point = std::chrono::time_point<Clock, Duration>;
using s_duration = std::chrono::duration<rep, std::ratio<1>>;
const rep fraction = (frac_time_point{t.point} - std::chrono::time_point_cast<s_duration>(t.point)).count();
using num_put = std::num_put<CharT, iterator>;
auto it2 = iterator(out.rdbuf());
*it2++ = '.';
out.width(t.fraction);
const auto& np = std::use_facet<num_put>(out.getloc());
const iterator end2 = np.put(
it2,
out,
'0',
fraction
);
if (end2.failed())
out.setstate(std::ios_base::badbit);
}
template<
class CharT,
class Clock,
size_t slen
>
void put_time(
std::basic_ios<CharT>& out,
const ::times::timestamp_t<CharT, Clock, slen>& ts
)
{
put_time(out, put_time(Clock::now(), ts.format));
}
template<
class CharT,
class Clock,
class Duration = typename Clock::duration,
uint8_t N = 0
>
std::basic_ostream<CharT>& operator<<(
std::basic_ostream<CharT>& out,
const ::times::put_time_t<CharT, Clock, Duration>& t
)
{
::times::put_time(out, t);
return out;
}
template<
class CharT,
class Clock,
size_t slen
>
std::basic_ostream<CharT>& operator<<(
std::basic_ostream<CharT>& out,
const ::times::timestamp_t<CharT, Clock, slen>& ts
)
{
::times::put_time(out, ts);
return out;
}
} // times
#endif