Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_conversions.hpp
Go to the documentation of this file.
1#pragma once
7#ifndef _TIME_SHIELD_TIME_CONVERSIONS_HPP_INCLUDED
8#define _TIME_SHIELD_TIME_CONVERSIONS_HPP_INCLUDED
9
10#include "enums.hpp"
11#include "validation.hpp"
12#include "time_utils.hpp"
13#include "time_zone_struct.hpp"
14#include <cmath>
15#include <ctime>
16
17namespace time_shield {
18
23 template<class T = int>
24 constexpr const T ns_of_sec(const fts_t& ts) noexcept {
25 fts_t temp;
26 return static_cast<T>(std::round(std::modf(ts, &temp) * static_cast<fts_t>(NS_PER_SEC)));
27 }
28
33 template<class T = int>
34 constexpr const T us_of_sec(const fts_t& ts) noexcept {
35 fts_t temp;
36 return static_cast<T>(std::round(std::modf(ts, &temp) * static_cast<fts_t>(US_PER_SEC)));
37 }
38
43 template<class T = int>
44 constexpr const T ms_of_sec(const fts_t& ts) noexcept {
45 fts_t temp;
46 return static_cast<T>(std::round(std::modf(ts, &temp) * static_cast<fts_t>(MS_PER_SEC)));
47 }
48
53 template<class T = int>
54 constexpr const T ms_of_ts(const ts_ms_t& ts) noexcept {
55 return ts % MS_PER_SEC;
56 }
57
58# ifndef TIME_SHIELD_CPP17
59 template<class T>
60 constexpr const ts_ms_t sec_to_ms_impl(const T& t, std::true_type) noexcept {
61 return static_cast<ts_ms_t>(std::round(t * static_cast<T>(MS_PER_SEC)));
62 }
63
64 template<class T>
65 constexpr const ts_ms_t sec_to_ms_impl(const T& t, std::false_type) noexcept {
66 return static_cast<ts_ms_t>(t) * static_cast<ts_ms_t>(MS_PER_SEC);
67 }
68# endif
69
75 template<class T1 = ts_ms_t, class T2>
76 constexpr const T1 sec_to_ms(const T2& ts) noexcept {
77# ifdef TIME_SHIELD_CPP17
78 if constexpr(std::is_same_v<T2, double>) {
79 return static_cast<T1>(std::round(ts * static_cast<T2>(MS_PER_SEC)));
80 } else
81 if constexpr(std::is_same_v<T2, float>) {
82 return static_cast<T1>(std::round(ts * static_cast<T2>(MS_PER_SEC)));
83 } else {
84 return static_cast<T1>(ts) * static_cast<T1>(MS_PER_SEC);
85 }
86# else
87 return sec_to_ms_impl(ts, std::is_same<T2, double>());
88# endif
89 }
90
94 constexpr const ts_ms_t fsec_to_ms(const fts_t& ts) noexcept {
95 return static_cast<ts_ms_t>(std::round(ts * static_cast<fts_t>(MS_PER_SEC)));
96 }
97
103 template<class T1 = ts_t, class T2 = ts_ms_t>
104 constexpr const T1 ms_to_sec(const T2& ts_ms) noexcept {
105 return static_cast<T1>(ts_ms) / static_cast<T1>(MS_PER_SEC);
106 }
107
112 template<class T = ts_ms_t>
113 constexpr const fts_t ms_to_fsec(const T& ts_ms) noexcept {
114 return static_cast<fts_t>(ts_ms) / static_cast<fts_t>(MS_PER_SEC);
115 }
116
117//------------------------------------------------------------------------------
118
123 template<class T = year_t>
124 constexpr const T get_unix_year(const ts_t& ts) noexcept {
125 // 9223372029693630000 - значение на момент 292277024400 от 2000 года
126 // Такое значение приводит к неправильному вычислению умножения n_400_years * SEC_PER_400_YEARS
127 // Поэтому пришлось снизить до 9223371890843040000
128 constexpr int64_t BIAS_292277022000 = 9223371890843040000LL;
129 constexpr int64_t BIAS_2000 = 946684800LL;
130
131 int64_t y = MAX_YEAR;
132 int64_t secs = -((ts - BIAS_2000) - BIAS_292277022000);
133
134 const int64_t n_400_years = secs / SEC_PER_400_YEARS;
135 secs -= n_400_years * SEC_PER_400_YEARS;
136 y -= n_400_years * 400;
137
138 const int64_t n_100_years = secs / SEC_PER_100_YEARS;
139 secs -= n_100_years * SEC_PER_100_YEARS;
140 y -= n_100_years * 100;
141
142 const int64_t n_4_years = secs / SEC_PER_4_YEARS;
143 secs -= n_4_years * SEC_PER_4_YEARS;
144 y -= n_4_years * 4;
145
146 const int64_t n_1_years = secs / SEC_PER_YEAR;
147 secs -= n_1_years * SEC_PER_YEAR;
148 y -= n_1_years;
149
150 y = secs == 0 ? y : y - 1;
151 return y - UNIX_EPOCH;
152 }
153
156 template<class T = year_t>
157 constexpr const T unix_year(const ts_t& ts) noexcept {
158 return get_unix_year(ts);
159 }
160
163 template<class T = year_t>
164 constexpr const T to_unix_year(const ts_t& ts) noexcept {
165 return get_unix_year(ts);
166 }
167
168//------------------------------------------------------------------------------
169
174 template<class T = int>
175 TIME_SHIELD_CONSTEXPR inline const T hour24_to_12(const T& hour) noexcept {
176 if (hour == 0 || hour > 12) return 12;
177 return hour;
178 }
179
182 template<class T = int>
183 TIME_SHIELD_CONSTEXPR inline const T h24_to_h12(const T& hour) noexcept {
184 return hour24_to_12(hour);
185 }
186
187//------------------------------------------------------------------------------
188
198 template<class T1, class T2 = ts_t>
199 T1 to_date_time(const T2& ts) {
200 // 9223372029693630000 - значение на момент 292277024400 от 2000 года
201 // Такое значение приводит к неправильному вычислению умножения n_400_years * SEC_PER_400_YEARS
202 // Поэтому пришлось снизить до 9223371890843040000
203 constexpr int64_t BIAS_292277022000 = 9223371890843040000LL;
204 constexpr int64_t BIAS_2000 = 946684800LL;
205
206 int64_t y = MAX_YEAR;
207 uint64_t secs = -((ts - BIAS_2000) - BIAS_292277022000);
208
209 const uint64_t n_400_years = secs / SEC_PER_400_YEARS;
210 secs -= n_400_years * SEC_PER_400_YEARS;
211 y -= n_400_years * 400;
212
213 const uint64_t n_100_years = secs / SEC_PER_100_YEARS;
214 secs -= n_100_years * SEC_PER_100_YEARS;
215 y -= n_100_years * 100;
216
217 const uint64_t n_4_years = secs / SEC_PER_4_YEARS;
218 secs -= n_4_years * SEC_PER_4_YEARS;
219 y -= n_4_years * 4;
220
221 const uint64_t n_1_years = secs / SEC_PER_YEAR;
222 secs -= n_1_years * SEC_PER_YEAR;
223 y -= n_1_years;
224
225 T1 date_time;
226
227 if (secs == 0) {
228 date_time.year = y;
229 date_time.mon = 1;
230 date_time.day = 1;
231 return date_time;
232 }
233
234 date_time.year = y - 1;
235 const bool is_leap_year = is_leap_year_date(date_time.year);
236 secs = is_leap_year ? SEC_PER_LEAP_YEAR - secs : SEC_PER_YEAR - secs;
237 const int days = secs / SEC_PER_DAY;
238
239 constexpr int JAN_AND_FEB_DAY_LEAP_YEAR = 60 - 1;
240 constexpr int TABLE_MONTH_OF_YEAR[] = {
241 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 31 январь
242 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // 28 февраль
243 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, // 31 март
244 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, // 30 апрель
245 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
246 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
247 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
248 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
249 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
250 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
251 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
252 12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
253 };
254 constexpr int TABLE_DAY_OF_YEAR[] = {
255 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, // 31 январь
256 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, // 28 февраль
257 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, // 31 март
258 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, // 30 апрель
259 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,
260 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,
261 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,
262 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,
263 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,
264 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,
265 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,
266 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,
267 };
268
269 if (is_leap_year) {
270 const int prev_days = days - 1;
271 date_time.day = days == JAN_AND_FEB_DAY_LEAP_YEAR ? (TABLE_DAY_OF_YEAR[prev_days] + 1) :
272 (days > JAN_AND_FEB_DAY_LEAP_YEAR ? TABLE_DAY_OF_YEAR[prev_days] : TABLE_DAY_OF_YEAR[days]);
273 date_time.mon = days >= JAN_AND_FEB_DAY_LEAP_YEAR ? TABLE_MONTH_OF_YEAR[prev_days] : TABLE_MONTH_OF_YEAR[days];
274 } else {
275 date_time.day = TABLE_DAY_OF_YEAR[days];
276 date_time.mon = TABLE_MONTH_OF_YEAR[days];
277 }
278
279 ts_t day_secs = secs % SEC_PER_DAY;
280 date_time.hour = day_secs / SEC_PER_HOUR;
281 ts_t min_secs = day_secs - date_time.hour * SEC_PER_HOUR;
282 date_time.min = min_secs / SEC_PER_MIN;
283 date_time.sec = min_secs - date_time.min * SEC_PER_MIN;
284# ifdef TIME_SHIELD_CPP17
285 if constexpr (std::is_floating_point<T2>::value) {
286 date_time.ms = static_cast<int>(std::round(std::fmod(static_cast<double>(ts), static_cast<double>(MS_PER_SEC))));
287 } else date_time.ms = 0;
288# else
289 if (std::is_floating_point<T2>::value) {
290 date_time.ms = static_cast<int>(std::round(std::fmod(static_cast<double>(ts), static_cast<double>(MS_PER_SEC))));
291 } else date_time.ms = 0;
292# endif
293 return date_time;
294 }
295
298 template<class T1, class T2 = ts_t>
299 T1 to_dt(const T2& ts) {
300 return to_date_time(ts);
301 }
302
303//------------------------------------------------------------------------------
304
309 template<class T>
310 inline T to_date_time_ms(const ts_ms_t& ts) {
312 date_time.ms = ms_of_ts(ts); // Extract and set the ms component
313 return date_time;
314 }
315
318 template<class T>
319 inline T to_dt_ms(const ts_ms_t& ts) {
320 return to_date_time_ms<T>(ts);
321 }
322
323//------------------------------------------------------------------------------
324
340 template<class T1 = year_t, class T2 = int>
342 const T1& year,
343 const T2& month,
344 const T2& day,
345 const T2& hour = 0,
346 const T2& min = 0,
347 const T2& sec = 0) {
348
349 if (day >= UNIX_EPOCH && year <= 31) {
350 return to_timestamp((T1)day, month, (T2)year, hour, min, sec);
351 }
352 if (!is_valid_date_time(year, month, day, hour, min, sec)) {
353 throw std::invalid_argument("Invalid date-time combination");
354 }
355
356 int64_t secs = 0;
357 uint64_t years = (static_cast<int64_t>(MAX_YEAR) - year);
358
359 const int64_t n_400_years = years / 400;
360 secs += n_400_years * SEC_PER_400_YEARS;
361 years -= n_400_years * 400;
362
363 const int64_t n_100_years = years / 100;
364 secs += n_100_years * SEC_PER_100_YEARS;
365 years -= n_100_years * 100;
366
367 const int64_t n_4_years = years / 4;
368 secs += n_4_years * SEC_PER_4_YEARS;
369 years -= n_4_years * 4;
370
371 secs += years * SEC_PER_YEAR;
372
373 // 9223372029693630000 - значение на момент 292277024400 от 2000 года
374 // Такое значение приводит к неправильному вычислению умножения n_400_years * SEC_PER_400_YEARS
375 // Поэтому пришлось снизить до 9223371890843040000
376 constexpr int64_t BIAS_292277022000 = 9223371890843040000LL;
377 constexpr int64_t BIAS_2000 = 946684800LL;
378
379 secs = BIAS_292277022000 - secs;
380 secs += BIAS_2000;
381
382 if (month == 1 && day == 1 &&
383 hour == 0 && min == 0 &&
384 sec == 0) {
385 return secs;
386 }
387
388 constexpr int lmos[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
389 constexpr int mos[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
390
391 secs += (is_leap_year_date(year) ? (lmos[month - 1] + day - 1) : (mos[month - 1] + day - 1)) * SEC_PER_DAY;
392 secs += SEC_PER_HOUR * hour + SEC_PER_MIN * min + sec;
393 return secs;
394 }
395
398 template<class T1 = year_t, class T2 = int>
400 const T1& year,
401 const T2& month,
402 const T2& day,
403 const T2& hour = 0,
404 const T2& min = 0,
405 const T2& sec = 0) {
406 return to_timestamp<T1, T2>(year, month, day, hour, min, sec);
407 }
408
411 template<class T1 = year_t, class T2 = int>
413 const T1& year,
414 const T2& month,
415 const T2& day,
416 const T2& hour = 0,
417 const T2& min = 0,
418 const T2& sec = 0) {
419 return to_timestamp<T1, T2>(year, month, day, hour, min, sec);
420 }
421
424 template<class T1 = year_t, class T2 = int>
426 const T1& year,
427 const T2& month,
428 const T2& day,
429 const T2& hour = 0,
430 const T2& min = 0,
431 const T2& sec = 0) {
432 return to_timestamp<T1, T2>(year, month, day, hour, min, sec);
433 }
434
437 template<class T1 = year_t, class T2 = int>
439 const T1& year,
440 const T2& month,
441 const T2& day,
442 const T2& hour = 0,
443 const T2& min = 0,
444 const T2& sec = 0) {
445 return to_timestamp<T1, T2>(year, month, day, hour, min, sec);
446 }
447
450 template<class T1 = year_t, class T2 = int>
452 const T1& year,
453 const T2& month,
454 const T2& day,
455 const T2& hour = 0,
456 const T2& min = 0,
457 const T2& sec = 0) {
458 return to_timestamp<T1, T2>(year, month, day, hour, min, sec);
459 }
460
461//------------------------------------------------------------------------------
462
472 template<class T>
474 const T& date_time) {
475 return to_timestamp(
476 date_time.year,
477 date_time.mon,
478 date_time.day,
479 date_time.hour,
480 date_time.min,
481 date_time.sec);
482 }
483
486 template<class T>
488 const T& date_time) {
489 return dt_to_timestamp(date_time);
490 }
491
494 template<class T>
496 const T& date_time) {
497 return dt_to_timestamp(date_time);
498 }
499
502 template<class T>
504 const T& date_time) {
505 return dt_to_timestamp(date_time);
506 }
507
510 template<class T>
512 const T& date_time) {
513 return dt_to_timestamp(date_time);
514 }
515
516//------------------------------------------------------------------------------
517
527 const std::tm *timeinfo) {
528 return to_timestamp(
529 timeinfo->tm_year + 1900,
530 timeinfo->tm_mon + 1,
531 timeinfo->tm_mday,
532 timeinfo->tm_hour,
533 timeinfo->tm_min,
534 timeinfo->tm_sec);
535 }
536
540 const std::tm *timeinfo) {
541 return tm_to_timestamp(timeinfo);
542 }
543
547 const std::tm *timeinfo) {
548 return tm_to_timestamp(timeinfo);
549 }
550
554 const std::tm *timeinfo) {
555 return tm_to_timestamp(timeinfo);
556 }
557
561 const std::tm *timeinfo) {
562 return tm_to_timestamp(timeinfo);
563 }
564
568 const std::tm *timeinfo) {
569 return tm_to_timestamp(timeinfo);
570 }
571
572//------------------------------------------------------------------------------
573
590 template<class T1 = year_t, class T2 = int>
592 const T1& year,
593 const T2& month,
594 const T2& day,
595 const T2& hour = 0,
596 const T2& min = 0,
597 const T2& sec = 0,
598 const T2& ms = 0) {
599 return sec_to_ms(to_timestamp<T1, T2>(year, month, day, hour, min, sec)) + ms;
600 }
601
604 template<class T1 = year_t, class T2 = int>
606 const T1& year,
607 const T2& month,
608 const T2& day,
609 const T2& hour = 0,
610 const T2& min = 0,
611 const T2& sec = 0,
612 const T2& ms = 0) {
613 return to_timestamp_ms(year, month, day, hour, min, sec, ms);
614 }
615
618 template<class T1 = year_t, class T2 = int>
620 const T1& year,
621 const T2& month,
622 const T2& day,
623 const T2& hour = 0,
624 const T2& min = 0,
625 const T2& sec = 0,
626 const T2& ms = 0) {
627 return to_timestamp_ms(year, month, day, hour, min, sec, ms);
628 }
629
632 template<class T1 = year_t, class T2 = int>
634 const T1& year,
635 const T2& month,
636 const T2& day,
637 const T2& hour = 0,
638 const T2& min = 0,
639 const T2& sec = 0,
640 const T2& ms = 0) {
641 return to_timestamp_ms(year, month, day, hour, min, sec, ms);
642 }
643
644//------------------------------------------------------------------------------
645
655 template<class T>
657 const T& date_time) {
658 return sec_to_ms(dt_to_timestamp(date_time)) + date_time.ms;
659 }
660
663 template<class T>
665 const T& date_time) {
666 return dt_to_timestamp_ms(date_time);
667 }
668
671 template<class T>
673 const T& date_time) {
674 return dt_to_timestamp_ms(date_time);
675 }
676
679 template<class T>
681 const T& date_time) {
682 return dt_to_timestamp_ms(date_time);
683 }
684
687 template<class T>
689 const T& date_time) {
690 return dt_to_timestamp_ms(date_time);
691 }
692
693//------------------------------------------------------------------------------
694
703 const std::tm *timeinfo) {
704 return sec_to_ms(tm_to_timestamp(timeinfo));
705 }
706
710 const std::tm *timeinfo) {
711 return tm_to_timestamp_ms(timeinfo);
712 }
713
717 const std::tm *timeinfo) {
718 return tm_to_timestamp_ms(timeinfo);
719 }
720
724 const std::tm *timeinfo) {
725 return tm_to_timestamp_ms(timeinfo);
726 }
727
731 const std::tm *timeinfo) {
732 return tm_to_timestamp_ms(timeinfo);
733 }
734
735//------------------------------------------------------------------------------
736
755 template<class T1 = year_t, class T2 = int, class T3 = int>
757 const T1& year,
758 const T2& month,
759 const T2& day,
760 const T2& hour = 0,
761 const T2& min = 0,
762 const T2& sec = 0,
763 const T3& ms = 0) {
764 return static_cast<fts_t>(to_timestamp(year, month, day, hour, min, sec)) +
765 static_cast<fts_t>(ms)/static_cast<fts_t>(MS_PER_SEC);
766 }
767
770 template<class T1 = year_t, class T2 = int, class T3 = int>
772 const T1& year,
773 const T2& month,
774 const T2& day,
775 const T2& hour = 0,
776 const T2& min = 0,
777 const T2& sec = 0,
778 const T3& ms = 0) {
779 return to_ftimestamp(year, month, day, hour, min, sec, ms);
780 }
781
784 template<class T1 = year_t, class T2 = int, class T3 = int>
786 const T1& year,
787 const T2& month,
788 const T2& day,
789 const T2& hour = 0,
790 const T2& min = 0,
791 const T2& sec = 0,
792 const T3& ms = 0) {
793 return to_ftimestamp(year, month, day, hour, min, sec, ms);
794 }
795
798 template<class T1 = year_t, class T2 = int, class T3 = int>
800 const T1& year,
801 const T2& month,
802 const T2& day,
803 const T2& hour = 0,
804 const T2& min = 0,
805 const T2& sec = 0,
806 const T3& ms = 0) {
807 return to_ftimestamp(year, month, day, hour, min, sec, ms);
808 }
809
810//------------------------------------------------------------------------------
811
822 template<class T>
824 const T& date_time) {
825 return static_cast<fts_t>(to_timestamp(date_time)) +
826 static_cast<fts_t>(date_time.ms)/static_cast<fts_t>(MS_PER_SEC);
827 }
828
831 template<class T>
833 const T& date_time) {
834 return dt_to_ftimestamp(date_time);
835 }
836
839 template<class T>
841 const T& date_time) {
842 return dt_to_ftimestamp(date_time);
843 }
844
847 template<class T>
849 const T& date_time) {
850 return dt_to_ftimestamp(date_time);
851 }
852
855 template<class T>
857 const T& date_time) {
858 return dt_to_ftimestamp(date_time);
859 }
860
861//------------------------------------------------------------------------------
862
873 const std::tm* timeinfo) {
874 return static_cast<fts_t>(tm_to_timestamp(timeinfo));
875 }
876
880 const std::tm* timeinfo) {
881 return tm_to_ftimestamp(timeinfo);
882 }
883
887 const std::tm* timeinfo) {
888 return tm_to_ftimestamp(timeinfo);
889 }
890
894 const std::tm* timeinfo) {
895 return tm_to_ftimestamp(timeinfo);
896 }
897
901 const std::tm* timeinfo) {
902 return tm_to_ftimestamp(timeinfo);
903 }
904
905//------------------------------------------------------------------------------
906
914 template<class T = uday_t>
915 constexpr const T get_unix_day(const ts_t& ts = ts()) noexcept {
916 return ts / SEC_PER_DAY;
917 }
918
921 template<class T = uday_t>
922 constexpr const T unix_day(const ts_t& ts = ts()) noexcept {
923 return get_unix_day(ts);
924 }
925
928 template<class T = uday_t>
929 constexpr const T uday(const ts_t& ts = ts()) noexcept {
930 return get_unix_day(ts);
931 }
932
933//------------------------------------------------------------------------------
934
943 template<class T = int>
944 constexpr const T get_days_difference(const ts_t& start, const ts_t& stop) noexcept {
945 return (stop - start) / SEC_PER_DAY;
946 }
947
950 template<class T = int>
951 constexpr const T get_days(const ts_t& start, const ts_t& stop) noexcept {
952 return get_days_difference(start, stop);
953 }
954
957 template<class T = int>
958 constexpr const T days(const ts_t& start, const ts_t& stop) noexcept {
959 return get_days_difference(start, stop);
960 }
961
962//------------------------------------------------------------------------------
963
971 template<class T = uday_t>
972 constexpr const T get_unix_day_ms(const ts_ms_t& t_ms = ts_ms()) noexcept {
973 return get_unix_day(ms_to_sec(t_ms));
974 }
975
978 template<class T = uday_t>
979 constexpr const T unix_day_ms(const ts_ms_t& t_ms = ts_ms()) noexcept {
980 return get_unix_day_ms(t_ms);
981 }
982
985 template<class T = uday_t>
986 constexpr const T uday_ms(const ts_ms_t& t_ms = ts_ms()) noexcept {
987 return get_unix_day_ms(t_ms);
988 }
989
990//------------------------------------------------------------------------------
991
999 template<class T = ts_t>
1000 constexpr const T unix_day_to_timestamp(const uday_t& unix_day) noexcept {
1001 return unix_day * SEC_PER_DAY;
1002 }
1003
1006 template<class T = ts_t>
1007 constexpr const T unix_day_to_ts(const uday_t& unix_day) noexcept {
1009 }
1010
1013 template<class T = ts_t>
1014 constexpr const T uday_to_ts(const uday_t& unix_day) noexcept {
1016 }
1017
1018//------------------------------------------------------------------------------
1019
1027 template<class T = int64_t>
1028 constexpr const T get_unix_min(const ts_t& ts = ts()) {
1029 return ts / SEC_PER_MIN;
1030 }
1031
1034 template<class T = int64_t>
1035 constexpr const T unix_min(const ts_t& ts = ts()) {
1036 return get_unix_min(ts);
1037 }
1038
1041 template<class T = int64_t>
1042 constexpr const T to_unix_min(const ts_t& ts = ts()) {
1043 return get_unix_min(ts);
1044 }
1045
1048 template<class T = int64_t>
1049 constexpr const T umin(const ts_t& ts = ts()) {
1050 return get_unix_min(ts);
1051 }
1052
1053//------------------------------------------------------------------------------
1054
1062 template<class T = int>
1063 constexpr const T sec_of_day(const ts_t& ts = ts()) {
1064 return ts % SEC_PER_DAY;
1065 }
1066
1074 template<class T = int>
1075 constexpr const T sec_of_day_ms(const ts_ms_t& ts_ms) noexcept {
1076 return sec_of_day(ms_to_sec(ts_ms));
1077 }
1078
1089 template<class T1 = int, class T2 = int>
1090 constexpr const T1 sec_of_day(
1091 const T2& hour,
1092 const T2& min,
1093 const T2& sec) noexcept {
1094 return hour * SEC_PER_HOUR + min * SEC_PER_MIN + sec;
1095 }
1096
1104 template<class T = int>
1105 constexpr const T sec_of_min(const ts_t& ts = ts()) {
1106 return (ts % SEC_PER_MIN);
1107 }
1108
1116 template<class T = int>
1117 constexpr const T sec_of_hour(const ts_t& ts = ts()) {
1118 return (ts % SEC_PER_HOUR);
1119 }
1120
1121//------------------------------------------------------------------------------
1122
1130 template<class T = year_t>
1131 TIME_SHIELD_CONSTEXPR inline const T get_year(const ts_t& ts = ts()) {
1132 return get_unix_year(ts) + UNIX_EPOCH;
1133 }
1134
1137 template<class T = year_t>
1138 TIME_SHIELD_CONSTEXPR inline const T year(const ts_t& ts = ts()) {
1139 return get_year(ts);
1140 }
1141
1144 template<class T = year_t>
1145 TIME_SHIELD_CONSTEXPR inline const T to_year(const ts_t& ts = ts()) {
1146 return get_year(ts);
1147 }
1148
1149//------------------------------------------------------------------------------
1150
1158 template<class T = year_t>
1160 return get_year(ms_to_sec(ts_ms));
1161 }
1162
1165 template<class T = year_t>
1166 TIME_SHIELD_CONSTEXPR inline const T year_ms(const ts_ms_t& ts_ms = ts_ms()) {
1167 return get_year_ms(ts_ms);
1168 }
1169
1172 template<class T = year_t>
1174 return get_year_ms(ts_ms);
1175 }
1176
1177//------------------------------------------------------------------------------
1178
1186 TIME_SHIELD_CONSTEXPR inline const ts_t start_of_year(const ts_t& ts) noexcept {
1187 constexpr ts_t BIAS_2100 = 4102444800;
1188 if (ts < BIAS_2100) {
1189 constexpr ts_t SEC_PER_YEAR_X2 = SEC_PER_YEAR * 2;
1190 ts_t year_start_ts = ts % SEC_PER_4_YEARS;
1191 if (year_start_ts < SEC_PER_YEAR) {
1192 return ts - year_start_ts;
1193 } else
1194 if (year_start_ts < SEC_PER_YEAR_X2) {
1195 return ts + SEC_PER_YEAR - year_start_ts;
1196 } else
1197 if (year_start_ts < (SEC_PER_YEAR_X2 + SEC_PER_LEAP_YEAR)) {
1198 return ts + SEC_PER_YEAR_X2 - year_start_ts;
1199 }
1200 return ts + (SEC_PER_YEAR_X2 + SEC_PER_LEAP_YEAR) - year_start_ts;
1201 }
1202
1203 constexpr ts_t BIAS_2000 = 946684800;
1204 ts_t secs = ts - BIAS_2000;
1205
1206 ts_t offset_y400 = secs % SEC_PER_400_YEARS;
1207 ts_t start_ts = secs - offset_y400 + BIAS_2000;
1208 secs = offset_y400;
1209
1210 if (secs >= SEC_PER_FIRST_100_YEARS) {
1212 start_ts += SEC_PER_FIRST_100_YEARS;
1213 while (secs >= SEC_PER_100_YEARS) {
1214 secs -= SEC_PER_100_YEARS;
1215 start_ts += SEC_PER_100_YEARS;
1216 }
1217
1218 constexpr ts_t SEC_PER_4_YEARS_V2 = 4 * SEC_PER_YEAR;
1219 if (secs >= SEC_PER_4_YEARS_V2) {
1220 secs -= SEC_PER_4_YEARS_V2;
1221 start_ts += SEC_PER_4_YEARS_V2;
1222 } else {
1223 start_ts += secs - secs % SEC_PER_YEAR;
1224 return start_ts;
1225 }
1226 }
1227
1228 ts_t offset_4y = secs % SEC_PER_4_YEARS;
1229 start_ts += secs - offset_4y;
1230 secs = offset_4y;
1231
1232 if (secs >= SEC_PER_LEAP_YEAR) {
1233 secs -= SEC_PER_LEAP_YEAR;
1234 start_ts += SEC_PER_LEAP_YEAR;
1235 start_ts += secs - secs % SEC_PER_YEAR;
1236 }
1237 return start_ts;
1238 }
1239
1242 TIME_SHIELD_CONSTEXPR inline const ts_t year_start(const ts_t& ts = ts()) {
1243 return start_of_year(ts);
1244 }
1245
1248 TIME_SHIELD_CONSTEXPR inline const ts_t year_begin(const ts_t& ts = ts()) {
1249 return start_of_year(ts);
1250 }
1251
1252//------------------------------------------------------------------------------
1253
1263 }
1264
1268 return start_of_year_ms(ts_ms);
1269 }
1270
1274 return start_of_year_ms(ts_ms);
1275 }
1276
1277//------------------------------------------------------------------------------
1278
1286 template<class T = year_t>
1288 if (year < 2100) {
1289 const ts_t year_diff = year >= UNIX_EPOCH ? year - UNIX_EPOCH : UNIX_EPOCH - year;
1290 const ts_t year_start_ts = (year_diff / 4) * SEC_PER_4_YEARS;
1291 const ts_t year_remainder = year_diff % 4;
1292 constexpr ts_t SEC_PER_YEAR_X2 = 2 * SEC_PER_YEAR;
1293 constexpr ts_t SEC_PER_YEAR_V2 = SEC_PER_YEAR_X2 + SEC_PER_LEAP_YEAR;
1294 switch (year_remainder) {
1295 case 0: return year_start_ts;
1296 case 1: return year_start_ts + SEC_PER_YEAR;
1297 case 2: return year_start_ts + SEC_PER_YEAR_X2;
1298 default: return year_start_ts + SEC_PER_YEAR_V2;
1299 };
1300 return year_start_ts + SEC_PER_YEAR_V2;
1301 }
1302 return to_timestamp(year, 1, 1);
1303 }
1304
1307 template<class T = year_t>
1309 return start_of_year_date(year);
1310 }
1311
1314 template<class T = year_t>
1316 return start_of_year_date(year);
1317 }
1318
1319//------------------------------------------------------------------------------
1320
1328 template<class T = year_t>
1332
1335 template<class T = year_t>
1339
1342 template<class T = year_t>
1346
1347//------------------------------------------------------------------------------
1348
1356 constexpr ts_t BIAS_2100 = 4102444800;
1357 if (ts < BIAS_2100) {
1358 constexpr ts_t SEC_PER_YEAR_X2 = SEC_PER_YEAR * 2;
1359 constexpr ts_t SEC_PER_YEAR_X3 = SEC_PER_YEAR * 3;
1360 constexpr ts_t SEC_PER_YEAR_X3_V2 = SEC_PER_YEAR_X2 + SEC_PER_LEAP_YEAR;
1361 ts_t year_end_ts = ts % SEC_PER_4_YEARS;
1362 if (year_end_ts < SEC_PER_YEAR) {
1363 return ts + SEC_PER_YEAR - year_end_ts - 1;
1364 } else
1365 if (year_end_ts < SEC_PER_YEAR_X2) {
1366 return ts + SEC_PER_YEAR_X2 - year_end_ts - 1;
1367 } else
1368 if (year_end_ts < SEC_PER_YEAR_X3_V2) {
1369 return ts + SEC_PER_YEAR_X3_V2 - year_end_ts - 1;
1370 }
1371 return ts + (SEC_PER_YEAR_X3 + SEC_PER_LEAP_YEAR) - year_end_ts - 1;
1372 }
1373
1374 constexpr ts_t BIAS_2000 = 946684800;
1375 ts_t secs = ts - BIAS_2000;
1376
1377 ts_t offset_y400 = secs % SEC_PER_400_YEARS;
1378 ts_t end_ts = secs - offset_y400 + BIAS_2000;
1379 secs = offset_y400;
1380
1381 if (secs >= SEC_PER_FIRST_100_YEARS) {
1383 end_ts += SEC_PER_FIRST_100_YEARS;
1384 while (secs >= SEC_PER_100_YEARS) {
1385 secs -= SEC_PER_100_YEARS;
1386 end_ts += SEC_PER_100_YEARS;
1387 }
1388
1389 constexpr ts_t SEC_PER_4_YEARS_V2 = 4 * SEC_PER_YEAR;
1390 if (secs >= SEC_PER_4_YEARS_V2) {
1391 secs -= SEC_PER_4_YEARS_V2;
1392 end_ts += SEC_PER_4_YEARS_V2;
1393 } else {
1394 end_ts += secs - secs % SEC_PER_YEAR;
1395 return end_ts + SEC_PER_YEAR - 1;
1396 }
1397 }
1398
1399 ts_t offset_4y = secs % SEC_PER_4_YEARS;
1400 end_ts += secs - offset_4y;
1401 secs = offset_4y;
1402
1403 if (secs >= SEC_PER_LEAP_YEAR) {
1404 secs -= SEC_PER_LEAP_YEAR;
1405 end_ts += SEC_PER_LEAP_YEAR;
1406 end_ts += secs - secs % SEC_PER_YEAR;
1407 end_ts += SEC_PER_YEAR;
1408 } else {
1409 end_ts += SEC_PER_LEAP_YEAR;
1410 }
1411 return end_ts - 1;
1412 }
1413
1416 TIME_SHIELD_CONSTEXPR inline const ts_t year_end(const ts_t& ts = ts()) {
1417 return end_of_year(ts);
1418 }
1419
1420//------------------------------------------------------------------------------
1421
1428 template<class T = year_t>
1432
1436 return end_of_year_ms(ts_ms);
1437 }
1438
1439//------------------------------------------------------------------------------
1440
1447 template<class T = int>
1448 inline const T day_of_year(const ts_t& ts = ts()) {
1449 return ((ts - start_of_year(ts)) / SEC_PER_DAY) + 1;
1450 }
1451
1452//------------------------------------------------------------------------------
1453
1460 template<class T = Month>
1461 TIME_SHIELD_CONSTEXPR inline const T month_of_year(const ts_t& ts) noexcept {
1462 constexpr int JAN_AND_FEB_DAY_LEAP_YEAR = 60;
1463 constexpr int TABLE_MONTH_OF_YEAR[] = {
1464 0,
1465 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 31 январь
1466 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // 28 февраль
1467 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, // 31 март
1468 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, // 30 апрель
1469 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
1470 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
1471 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
1472 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
1473 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
1474 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
1475 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
1476 12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
1477 };
1478 const size_t dy = day_of_year(ts);
1479 return static_cast<T>((is_leap_year(ts) && dy >= JAN_AND_FEB_DAY_LEAP_YEAR) ? TABLE_MONTH_OF_YEAR[dy - 1] : TABLE_MONTH_OF_YEAR[dy]);
1480 }
1481
1482//------------------------------------------------------------------------------
1483
1490 template<class T = int>
1492 constexpr int JAN_AND_FEB_DAY_LEAP_YEAR = 60;
1493 // таблица для обычного года, не високосного
1494 constexpr int TABLE_DAY_OF_YEAR[] = {
1495 0,
1496 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, // 31 январь
1497 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, // 28 февраль
1498 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, // 31 март
1499 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, // 30 апрель
1500 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,
1501 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,
1502 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,
1503 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,
1504 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,
1505 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,
1506 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,
1507 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,
1508 };
1509 const size_t dy = day_of_year(ts);
1510 if(is_leap_year(ts)) {
1511 if(dy == JAN_AND_FEB_DAY_LEAP_YEAR) return TABLE_DAY_OF_YEAR[dy - 1] + 1;
1512 if(dy > JAN_AND_FEB_DAY_LEAP_YEAR) return TABLE_DAY_OF_YEAR[dy - 1];
1513 }
1514 return TABLE_DAY_OF_YEAR[dy];
1515 }
1516
1517//------------------------------------------------------------------------------
1518
1526 template<class T1 = int, class T2 = year_t, class T3 = int>
1527 constexpr const T1 num_days_in_month(const T2& year, const T3& month) noexcept {
1528 if (month > MONTHS_PER_YEAR || month < 0) return 0;
1529 constexpr T1 num_days[13] = {0,31,30,31,30,31,30,31,31,30,31,30,31};
1530 if (month == FEB) {
1531 if (is_leap_year_date(year)) return 29;
1532 return 28;
1533 }
1534 return num_days[month];
1535 }
1536
1539 template<class T1 = int, class T2 = year_t, class T3 = int>
1540 constexpr const T1 days_in_month(const T2& year, const T3& month) noexcept {
1541 return num_days_in_month(year, month);
1542 }
1543
1544//------------------------------------------------------------------------------
1545
1552 template<class T1 = int>
1553 TIME_SHIELD_CONSTEXPR const T1 num_days_in_month_ts(const ts_t& ts = ts()) noexcept {
1554 constexpr T1 num_days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
1555 const int month = month_of_year(ts);
1556 if (month == FEB) {
1557 return is_leap_year(ts) ? 29 : 28;
1558 }
1559 return num_days[month];
1560 }
1561
1564 template<class T1 = int>
1565 constexpr const T1 num_days_in_month(const ts_t& ts = ts()) noexcept {
1566 return num_days_in_month_ts(ts);
1567 }
1568
1571 template<class T1 = int>
1572 constexpr const T1 days_in_month(const ts_t& ts = ts()) noexcept {
1573 return num_days_in_month_ts(ts);
1574 }
1575
1576//------------------------------------------------------------------------------
1577
1584 template<class T1 = int, class T2 = year_t>
1585 constexpr const T1 num_days_in_year(const T2& year) noexcept {
1587 return DAYS_PER_YEAR;
1588 }
1589
1592 template<class T1 = int, class T2 = year_t>
1593 constexpr const T1 days_in_year(const T2& year) noexcept {
1594 return num_days_in_year(year);
1595 }
1596
1597//------------------------------------------------------------------------------
1598
1605 template<class T = int>
1606 constexpr const T num_days_in_year_ts(const ts_t& ts = ts()) {
1608 return DAYS_PER_YEAR;
1609 }
1610
1613 template<class T = int>
1614 constexpr const T days_in_year_ts(const ts_t& ts = ts()) {
1615 return num_days_in_year_ts(ts);
1616 }
1617
1618//------------------------------------------------------------------------------
1619
1627 constexpr const ts_t start_of_day(const ts_t& ts = ts()) noexcept {
1628 return ts - (ts % SEC_PER_DAY);
1629 }
1630
1633 constexpr const ts_t day_start(const ts_t& ts = ts()) noexcept {
1634 return start_of_day(ts);
1635 }
1636
1637//------------------------------------------------------------------------------
1638
1646 template<class T = int>
1647 constexpr const ts_t start_of_prev_day(const ts_t& ts = ts(), const T& days = 1) {
1648 return ts - (ts % SEC_PER_DAY) - SEC_PER_DAY * days;
1649 }
1650
1653 template<class T = int>
1654 constexpr const ts_t previous_day_start(const ts_t& ts = ts(), const T& days = 1) {
1655 return start_of_prev_day(ts, days);
1656 }
1657
1658//------------------------------------------------------------------------------
1659
1667 constexpr const ts_t start_of_day_sec(const ts_ms_t& ts_ms = ts_ms()) {
1668 return start_of_day(ms_to_sec(ts_ms));
1669 }
1670
1673 constexpr const ts_t day_start_sec(const ts_ms_t& ts_ms = ts_ms()) {
1674 return start_of_day_sec(ts_ms);
1675 }
1676
1677//------------------------------------------------------------------------------
1678
1686 constexpr const ts_ms_t start_of_day_ms(const ts_ms_t& ts_ms = ts_ms()) {
1687 return ts_ms - (ts_ms % MS_PER_DAY);
1688 }
1689
1692 constexpr const ts_ms_t day_start_ms(const ts_ms_t& ts_ms = ts_ms()) {
1693 return start_of_day_ms(ts_ms);
1694 }
1695
1696//------------------------------------------------------------------------------
1697
1705 template<class T = int>
1706 constexpr const ts_t start_of_next_day(const ts_t& ts, const T& days = 1) {
1707 return start_of_day(ts) + days * SEC_PER_DAY;
1708 }
1709
1712 template<class T = int>
1713 constexpr const ts_t next_day_start(const ts_t& ts, const T& days = 1) {
1714 return start_of_next_day(ts, days);
1715 }
1716
1717//------------------------------------------------------------------------------
1718
1725 constexpr const ts_t end_of_day(const ts_t& ts = ts()) noexcept {
1726 return ts - (ts % SEC_PER_DAY) + SEC_PER_DAY - 1;
1727 }
1728
1731 constexpr const ts_t day_end(const ts_t& ts = ts()) noexcept {
1732 return end_of_day(ts);
1733 }
1734
1735//------------------------------------------------------------------------------
1736
1743 constexpr const ts_t end_of_day_sec(const ts_ms_t& ts_ms = ts_ms()) noexcept {
1744 return end_of_day(ms_to_sec(ts_ms));
1745 }
1746
1749 constexpr const ts_t day_end_sec(const ts_ms_t& ts_ms = ts_ms()) noexcept {
1750 return end_of_day_sec(ts_ms);
1751 }
1752
1753//------------------------------------------------------------------------------
1754
1761 constexpr const ts_ms_t end_of_day_ms(const ts_ms_t& ts_ms = ts_ms()) noexcept {
1762 return ts_ms - (ts_ms % MS_PER_DAY) + MS_PER_DAY - 1;
1763 }
1764
1767 constexpr const ts_ms_t day_end_ms(const ts_ms_t& ts_ms = ts_ms()) noexcept {
1768 return end_of_day_ms(ts_ms);
1769 }
1770
1771//------------------------------------------------------------------------------
1772
1778 template<class T1 = Weekday, class T2 = year_t, class T3 = int>
1779 constexpr const T1 day_of_week_date(const T2& year, const T3& month, const T3& day) {
1780 year_t a, y, m, R;
1781 a = (14 - month) / MONTHS_PER_YEAR;
1782 y = year - a;
1783 m = month + MONTHS_PER_YEAR * a - 2;
1784 R = 7000 + ( day + y + (y / 4) - (y / 100) + (y / 400) + (31 * m) / MONTHS_PER_YEAR);
1785 return static_cast<T1>(R % DAYS_PER_WEEK);
1786 }
1787
1790 template<class T1 = Weekday, class T2 = year_t, class T3 = int>
1791 constexpr const T1 get_weekday(const T2& year, const T3& month, const T3& day) {
1792 return day_of_week_date(year, month, day);
1793 }
1794
1797 template<class T1 = Weekday, class T2 = year_t, class T3 = int>
1798 constexpr const T1 day_of_week(const T2& year, const T3& month, const T3& day) {
1799 return day_of_week_date(year, month, day);
1800 }
1801
1802//------------------------------------------------------------------------------
1803
1811 template<class T1 = Weekday, class T2>
1812 constexpr const T1 get_weekday_from_date(const T2& date) {
1813 return day_of_week_date(date.year, date.mon, date.day);
1814 }
1815
1818 template<class T1 = int, class T2>
1819 constexpr const T1 day_of_week_dt(const T2& date) {
1820 return get_weekday_from_date(date);
1821 }
1822
1825 template<class T1 = int, class T2>
1826 constexpr const T1 day_of_week(const T2& date) {
1827 return get_weekday_from_date(date);
1828 }
1829
1830//------------------------------------------------------------------------------
1831
1835 template<class T = Weekday>
1836 constexpr const T get_weekday_from_ts(const ts_t& ts) noexcept {
1837 return static_cast<T>((ts / SEC_PER_DAY + THU) % DAYS_PER_WEEK);
1838 }
1839
1842 template<class T = Weekday>
1843 constexpr const T day_of_week(const ts_t& ts) noexcept {
1844 return get_weekday_from_ts(ts);
1845 }
1846
1847//------------------------------------------------------------------------------
1848
1852 template<class T = Weekday>
1853 constexpr const T get_weekday_from_ts_ms(const ts_ms_t& ts_ms) {
1855 }
1856
1859 template<class T = Weekday>
1860 constexpr const T day_of_week_ms(const ts_ms_t& ts_ms) {
1862 }
1863
1864//------------------------------------------------------------------------------
1865
1874 return start_of_day(ts) - (day_of_month(ts) - 1) * SEC_PER_DAY;
1875 }
1876
1879 TIME_SHIELD_CONSTEXPR inline const ts_t month_begin(const ts_t& ts = ts()) {
1880 return start_of_month(ts);
1881 }
1882
1883//------------------------------------------------------------------------------
1884
1894 }
1895
1899 return end_of_month(ts);
1900 }
1901
1902//------------------------------------------------------------------------------
1903
1913 }
1914
1918 return last_sunday_of_month(ts);
1919 }
1920
1921//------------------------------------------------------------------------------
1922
1930 template<class T1 = int, class T2 = year_t, class T3 = int>
1931 TIME_SHIELD_CONSTEXPR inline const T1 last_sunday_month_day(const T2& year, const T3& month) {
1932 const T1 days = num_days_in_month(year, month);
1933 return days - day_of_week_date(year, month, days);
1934 }
1935
1938 template<class T1 = int, class T2 = year_t, class T3 = int>
1939 TIME_SHIELD_CONSTEXPR inline const T1 final_sunday_month_day(const T2& year, const T3& month) {
1940 return last_sunday_month_day(year, month);
1941 }
1942
1943//------------------------------------------------------------------------------
1944
1951 constexpr const ts_t start_of_hour(const ts_t& ts = ts()) noexcept {
1952 return ts - (ts % SEC_PER_HOUR);
1953 }
1954
1957 constexpr const ts_t hour_begin(const ts_t& ts = ts()) noexcept {
1958 return start_of_hour(ts);
1959 }
1960
1961//------------------------------------------------------------------------------
1962
1969 constexpr const ts_t start_of_hour_sec(const ts_ms_t& ts_ms = ts_ms()) noexcept {
1970 return start_of_hour(ms_to_sec(ts_ms));
1971 }
1972
1975 constexpr const ts_t hour_begin_sec(const ts_ms_t& ts_ms = ts_ms()) noexcept {
1976 return start_of_hour_sec(ts_ms);
1977 }
1978
1979//------------------------------------------------------------------------------
1980
1985 constexpr const ts_ms_t start_of_hour_ms(const ts_ms_t& ts_ms = ts_ms()) noexcept {
1986 return ts_ms - (ts_ms % MS_PER_HOUR);
1987 }
1988
1991 constexpr const ts_ms_t hour_begin_ms(const ts_ms_t& ts_ms = ts_ms()) noexcept {
1992 return start_of_hour_ms(ts_ms);
1993 }
1994
1995//------------------------------------------------------------------------------
1996
2001 constexpr const ts_t end_of_hour(const ts_t& ts = ts()) noexcept {
2002 return ts - (ts % SEC_PER_HOUR) + SEC_PER_HOUR - 1;
2003 }
2004
2007 constexpr const ts_t finish_of_hour(const ts_t& ts = ts()) noexcept {
2008 return end_of_hour(ts);
2009 }
2010
2011//------------------------------------------------------------------------------
2012
2019 constexpr const ts_t end_of_hour_sec(const ts_ms_t& ts_ms = ts_ms()) noexcept {
2020 return end_of_hour(ms_to_sec(ts_ms));
2021 }
2022
2025 constexpr const ts_t finish_of_hour_sec(const ts_ms_t& ts_ms = ts_ms()) noexcept {
2026 return end_of_hour_sec(ts_ms);
2027 }
2028
2029//------------------------------------------------------------------------------
2030
2037 constexpr const ts_ms_t end_of_hour_ms(const ts_ms_t& ts_ms = ts_ms()) noexcept {
2038 return ts_ms - (ts_ms % MS_PER_HOUR) + MS_PER_HOUR - 1;
2039 }
2040
2043 constexpr const ts_ms_t finish_of_hour_ms(const ts_ms_t& ts_ms = ts_ms()) noexcept {
2044 return end_of_hour_ms(ts_ms);
2045 }
2046
2047//------------------------------------------------------------------------------
2048
2055 template<class T = int>
2056 constexpr const T hour_of_day(const ts_t& ts = ts()) noexcept {
2057 return ((ts / SEC_PER_HOUR) % HOURS_PER_DAY);
2058 }
2059
2062 template<class T = int>
2063 constexpr const T hour_in_day(const ts_t& ts = ts()) noexcept {
2064 return hour_of_day(ts);
2065 }
2066
2067//------------------------------------------------------------------------------
2068
2076 constexpr const ts_t start_of_week(const ts_t& ts = ts()) {
2078 }
2079
2082 constexpr const ts_t week_begin(const ts_t& ts = ts()) {
2083 return start_of_week(ts);
2084 }
2085
2086//------------------------------------------------------------------------------
2087
2095 constexpr const ts_t end_of_week(const ts_t& ts = ts()) {
2097 }
2098
2101 constexpr const ts_t finish_of_week(const ts_t& ts = ts()) {
2102 return end_of_week(ts);
2103 }
2104
2105//------------------------------------------------------------------------------
2106
2114 constexpr const ts_t start_of_saturday(const ts_t& ts = ts()) {
2115 return start_of_day(ts) + (SAT - day_of_week(ts)) * SEC_PER_DAY;
2116 }
2117
2120 constexpr const ts_t saturday_begin(const ts_t& ts = ts()) {
2121 return start_of_saturday(ts);
2122 }
2123
2124//------------------------------------------------------------------------------
2125
2129 constexpr const ts_t start_of_min(const ts_t& ts = ts()) noexcept {
2130 return ts - (ts % SEC_PER_MIN);
2131 }
2132
2135 constexpr const ts_t min_begin(const ts_t& ts = ts()) noexcept {
2136 return start_of_min(ts);
2137 }
2138
2139//------------------------------------------------------------------------------
2140
2144 constexpr const ts_t end_of_min(const ts_t& ts = ts()) noexcept {
2145 return ts - (ts % SEC_PER_MIN) + SEC_PER_MIN - 1;
2146 }
2147
2150 constexpr const ts_t finish_of_min(const ts_t& ts = ts()) noexcept {
2151 return end_of_min(ts);
2152 }
2153
2154//------------------------------------------------------------------------------
2155
2160 template<class T = int>
2161 constexpr const T min_of_day(const ts_t& ts = ts()) noexcept {
2162 return ((ts / SEC_PER_MIN) % MIN_PER_DAY);
2163 }
2164
2165//------------------------------------------------------------------------------
2166
2171 template<class T = int>
2172 constexpr const T min_of_hour(const ts_t& ts = ts()) noexcept {
2173 return ((ts / SEC_PER_MIN) % MIN_PER_HOUR);
2174 }
2175
2176//------------------------------------------------------------------------------
2177
2182 template<class T = int>
2183 constexpr const ts_t start_of_period(const T& p, const ts_t& ts = ts()) {
2184 return ts - (ts % p);
2185 }
2186
2187//------------------------------------------------------------------------------
2188
2193 template<class T = int>
2194 constexpr const ts_t end_of_period(const T& p, const ts_t& ts = ts()) {
2195 return ts - (ts % p) + p - 1;
2196 }
2197
2198//------------------------------------------------------------------------------
2199
2205 template<class T = TimeZoneStruct>
2207 const tz_t& offset) {
2208 T tz;
2209 int abs_val = std::abs(offset);
2210 tz.hour = abs_val / SEC_PER_HOUR;
2211 tz.min = abs_val % SEC_PER_MIN;
2212 tz.is_positive = (offset >= 0);
2213 return tz;
2214 }
2215
2216}; // namespace time_shield
2217
2218#endif // _TIME_SHIELD_TIME_CONVERSIONS_HPP_INCLUDED
#define TIME_SHIELD_CONSTEXPR
Definition config.hpp:16
Header file with enumerations for weekdays, months, and other time-related categories.
Main namespace for the Time Shield library.
Definition constants.hpp:12
constexpr const T to_unix_year(const ts_t &ts) noexcept
Alias for get_unix_year function.
const int64_t MONTHS_PER_YEAR
Months per year.
Definition constants.hpp:56
TIME_SHIELD_CONSTEXPR const fts_t dt_to_ftimestamp(const T &date_time)
Converts a date-time structure to a floating-point timestamp.
constexpr const T umin(const ts_t &ts=ts())
Alias for get_unix_min function.
constexpr const ts_t end_of_period(const T &p, const ts_t &ts=ts())
Get the timestamp of the end of the period.
constexpr int64_t SEC_PER_LEAP_YEAR
Seconds per leap year (366 days)
Definition constants.hpp:34
TIME_SHIELD_CONSTEXPR const fts_t tm_to_ftimestamp(const std::tm *timeinfo)
Converts a std::tm structure to a floating-point timestamp.
constexpr const T min_of_day(const ts_t &ts=ts()) noexcept
Get minute of day. This function returns a value between 0 to 1439 (minute of day).
constexpr int64_t HOURS_PER_DAY
Hours per day.
Definition constants.hpp:49
constexpr const T get_unix_min(const ts_t &ts=ts())
Get UNIX minute.
TIME_SHIELD_CONSTEXPR const ts_t get_timestamp(const T1 &year, const T2 &month, const T2 &day, const T2 &hour=0, const T2 &min=0, const T2 &sec=0)
Alias for to_timestamp function.
constexpr const T uday(const ts_t &ts=ts()) noexcept
Alias for get_unix_day function.
TIME_SHIELD_CONSTEXPR const ts_ms_t year_end_ms(const ts_ms_t &ts_ms=ts_ms())
Alias for end_of_year_ms function.
constexpr const T unix_day_to_ts(const uday_t &unix_day) noexcept
Alias for unix_day_to_timestamp function.
constexpr const ts_t min_begin(const ts_t &ts=ts()) noexcept
Alias for start_of_min function.
constexpr int64_t SEC_PER_FIRST_100_YEARS
Seconds per first 100 years.
Definition constants.hpp:36
TIME_SHIELD_CONSTEXPR const T month_of_year(const ts_t &ts) noexcept
Get the month of the year.
constexpr const ts_t saturday_begin(const ts_t &ts=ts())
Alias for start_of_saturday function.
TIME_SHIELD_CONSTEXPR const ts_t ts_from_tm(const std::tm *timeinfo)
Alias for tm_to_timestamp function.
constexpr const T1 num_days_in_month(const T2 &year, const T3 &month) noexcept
Get the number of days in a month.
int64_t ts_t
Integer timestamp type.
Definition types.hpp:15
constexpr const ts_ms_t end_of_day_ms(const ts_ms_t &ts_ms=ts_ms()) noexcept
Get the timestamp at the end of the day in milliseconds.
TIME_SHIELD_CONSTEXPR const ts_t last_day_of_month(const ts_t &ts=ts())
Alias for end_of_month function.
const ts_ms_t timestamp_ms() noexcept
Get the current UTC timestamp in milliseconds.
TIME_SHIELD_CONSTEXPR const T to_year(const ts_t &ts=ts())
Alias for get_year function.
constexpr const T to_unix_min(const ts_t &ts=ts())
Alias for get_unix_min function.
constexpr const bool is_leap_year_date(const T &year) noexcept
Checks if the given year is a leap year.
constexpr const ts_t start_of_week(const ts_t &ts=ts())
Get the timestamp of the beginning of the week.
constexpr const T sec_of_day(const ts_t &ts=ts())
Get the second of the day.
T to_dt_ms(const ts_ms_t &ts)
Alias for to_date_time_ms function.
constexpr const ts_t start_of_saturday(const ts_t &ts=ts())
Get the timestamp of the start of Saturday.
constexpr const ts_ms_t day_end_ms(const ts_ms_t &ts_ms=ts_ms()) noexcept
Alias for end_of_day_ms function.
constexpr int64_t MIN_PER_HOUR
Minutes per hour.
Definition constants.hpp:42
constexpr int64_t MIN_PER_DAY
Minutes per day.
Definition constants.hpp:43
TIME_SHIELD_CONSTEXPR const fts_t to_ftimestamp(const T1 &year, const T2 &month, const T2 &day, const T2 &hour=0, const T2 &min=0, const T2 &sec=0, const T3 &ms=0)
Converts a date and time to a floating-point timestamp.
TIME_SHIELD_CONSTEXPR const bool is_leap_year_ts(const ts_t &ts)
Checks if the given year is a leap year.
constexpr int64_t DAYS_PER_LEAP_YEAR
Days per leap year.
Definition constants.hpp:51
constexpr const ts_t day_end_sec(const ts_ms_t &ts_ms=ts_ms()) noexcept
Alias for end_of_day_sec function.
const T us_of_sec() noexcept
Get the microsecond part of the current second.
TIME_SHIELD_CONSTEXPR const T day_of_month(const ts_t &ts)
Get the day of the month.
constexpr const T sec_of_hour(const ts_t &ts=ts())
Get the second of the hour.
constexpr const T1 get_weekday_from_date(const T2 &date)
Get the day of the week from a date structure.
constexpr const T day_of_week_ms(const ts_ms_t &ts_ms)
Alias for get_weekday_from_ts_ms function.
TIME_SHIELD_CONSTEXPR const T hour24_to_12(const T &hour) noexcept
Converts a 24-hour format hour to a 12-hour format.
constexpr const T get_unix_day_ms(const ts_ms_t &t_ms=ts_ms()) noexcept
Get UNIX day from milliseconds timestamp.
TIME_SHIELD_CONSTEXPR const ts_t to_timestamp(const T1 &year, const T2 &month, const T2 &day, const T2 &hour=0, const T2 &min=0, const T2 &sec=0)
Converts a date and time to a timestamp.
constexpr const T get_days(const ts_t &start, const ts_t &stop) noexcept
Alias for get_days_difference function.
constexpr int64_t MS_PER_DAY
Milliseconds per day.
Definition constants.hpp:25
constexpr const ts_t end_of_day(const ts_t &ts=ts()) noexcept
Get the timestamp at the end of the day.
TIME_SHIELD_CONSTEXPR const ts_t to_ts(const T1 &year, const T2 &month, const T2 &day, const T2 &hour=0, const T2 &min=0, const T2 &sec=0)
Alias for to_timestamp function.
T1 to_dt(const T2 &ts)
Alias for to_date_time function.
constexpr const ts_t end_of_min(const ts_t &ts=ts()) noexcept
Get the timestamp of the end of the minute.
TIME_SHIELD_CONSTEXPR const T to_year_ms(const ts_ms_t &ts_ms=ts_ms())
Alias for get_year_ms function.
int64_t ts_ms_t
Integer timestamp milliseconds type.
Definition types.hpp:16
TIME_SHIELD_CONSTEXPR const ts_ms_t start_of_year_ms(const ts_ms_t &ts_ms=ts_ms()) noexcept
Get the start of the year timestamp in milliseconds.
constexpr const ts_t end_of_hour_sec(const ts_ms_t &ts_ms=ts_ms()) noexcept
Get the timestamp at the end of the hour.
constexpr int64_t MS_PER_HOUR
Milliseconds per hour.
Definition constants.hpp:24
constexpr const T1 days_in_year(const T2 &year) noexcept
Alias for num_days_in_year function.
constexpr const T get_weekday_from_ts_ms(const ts_ms_t &ts_ms)
Get the weekday from a timestamp in milliseconds.
constexpr const ts_t week_begin(const ts_t &ts=ts())
Alias for start_of_week function.
constexpr const T ms_of_ts(const ts_ms_t &ts) noexcept
Get the millisecond part of the timestamp.
constexpr int64_t DAYS_PER_YEAR
Days per year.
Definition constants.hpp:52
constexpr const T sec_of_min(const ts_t &ts=ts())
Get the second of the minute.
constexpr const T days_in_year_ts(const ts_t &ts=ts())
Alias for num_days_in_year_ts function.
TIME_SHIELD_CONSTEXPR const ts_t tm_to_timestamp_ms(const std::tm *timeinfo)
Converts a std::tm structure to a timestamp in milliseconds.
constexpr const ts_t day_start_sec(const ts_ms_t &ts_ms=ts_ms())
Alias for start_of_day_sec function.
constexpr const ts_t start_of_next_day(const ts_t &ts, const T &days=1)
Get timestamp of the first day after a specified number of days.
constexpr const ts_t start_of_day_sec(const ts_ms_t &ts_ms=ts_ms())
Get the start of the day timestamp in seconds.
constexpr int64_t SEC_PER_DAY
Seconds per day.
Definition constants.hpp:31
constexpr const T1 days_in_month(const T2 &year, const T3 &month) noexcept
Alias for num_days_in_month function.
constexpr const ts_t start_of_hour_sec(const ts_ms_t &ts_ms=ts_ms()) noexcept
Get the timestamp at the start of the hour.
TIME_SHIELD_CONSTEXPR const ts_ms_t end_of_year_ms(const ts_ms_t &ts_ms=ts_ms())
Get the timestamp in milliseconds of the end of the year.
int64_t uday_t
Integer unix day type.
Definition types.hpp:14
constexpr const ts_t start_of_day(const ts_t &ts=ts()) noexcept
Get the start of the day timestamp.
TIME_SHIELD_CONSTEXPR const ts_t start_of_month(const ts_t &ts=ts())
Get the timestamp at the start of the current month.
constexpr int64_t MS_PER_SEC
Milliseconds per second.
Definition constants.hpp:21
constexpr int64_t SEC_PER_MIN
Seconds per minute.
Definition constants.hpp:28
constexpr const T1 num_days_in_year(const T2 &year) noexcept
Get the number of days in a given year.
constexpr const T hour_in_day(const ts_t &ts=ts()) noexcept
Alias for hour_of_day function.
const TimeZoneStruct to_time_zone(const tz_t &offset)
Converts an integer to a time zone structure.
constexpr const ts_t start_of_hour(const ts_t &ts=ts()) noexcept
Get the timestamp at the start of the hour.
constexpr const T1 sec_to_ms(const T2 &ts) noexcept
Converts a timestamp from seconds to milliseconds.
constexpr const ts_t day_start(const ts_t &ts=ts()) noexcept
Alias for start_of_day function.
TIME_SHIELD_CONSTEXPR const T1 last_sunday_month_day(const T2 &year, const T3 &month)
Get the day of the last Sunday of the given month and year.
constexpr const ts_t finish_of_hour_sec(const ts_ms_t &ts_ms=ts_ms()) noexcept
Alias for end_of_hour_sec function.
constexpr const ts_t next_day_start(const ts_t &ts, const T &days=1)
Alias for start_of_next_day function.
TIME_SHIELD_CONSTEXPR const ts_t month_begin(const ts_t &ts=ts())
Alias for start_of_month function.
constexpr const fts_t ms_to_fsec(const T &ts_ms) noexcept
Converts a timestamp from milliseconds to floating-point seconds.
TIME_SHIELD_CONSTEXPR const T year_ms(const ts_ms_t &ts_ms=ts_ms())
Alias for get_year_ms function.
constexpr const T get_days_difference(const ts_t &start, const ts_t &stop) noexcept
Get the number of days between two timestamps.
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
Definition constants.hpp:30
constexpr const T unix_day_ms(const ts_ms_t &t_ms=ts_ms()) noexcept
Alias for get_unix_day_ms function.
const ts_t timestamp() noexcept
Get the current UTC timestamp in seconds.
TIME_SHIELD_CONSTEXPR const ts_t last_sunday_of_month(const ts_t &ts=ts())
Get the timestamp of the last Sunday of the current month.
constexpr const ts_t finish_of_min(const ts_t &ts=ts()) noexcept
Alias for end_of_min function.
const ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
constexpr const ts_t hour_begin(const ts_t &ts=ts()) noexcept
Alias for start_of_hour function.
const T ns_of_sec() noexcept
Get the nanosecond part of the current second.
constexpr const T get_unix_year(const ts_t &ts) noexcept
Converts a UNIX timestamp to a year.
constexpr const ts_t finish_of_hour(const ts_t &ts=ts()) noexcept
Alias for end_of_hour function.
constexpr const T unix_day(const ts_t &ts=ts()) noexcept
Alias for get_unix_day function.
const T day_of_year(const ts_t &ts=ts())
Get the day of the year.
TIME_SHIELD_CONSTEXPR const ts_t year_end(const ts_t &ts=ts())
Alias for end_of_year function.
TIME_SHIELD_CONSTEXPR const T get_year_ms(const ts_ms_t &ts_ms=ts_ms())
Get the year from the timestamp in milliseconds.
TIME_SHIELD_CONSTEXPR const ts_t dt_to_timestamp(const T &date_time)
Converts a date-time structure to a timestamp.
constexpr const T uday_to_ts(const uday_t &unix_day) noexcept
Alias for unix_day_to_timestamp function.
constexpr int64_t DAYS_PER_WEEK
Days per week.
Definition constants.hpp:50
constexpr const T1 get_weekday(const T2 &year, const T3 &month, const T3 &day)
Alias for day_of_week_date function.
TIME_SHIELD_CONSTEXPR const ts_t end_of_month(const ts_t &ts=ts())
Get the last timestamp of the current month.
double fts_t
Floating point timestamp type.
Definition types.hpp:18
constexpr const T1 day_of_week_date(const T2 &year, const T3 &month, const T3 &day)
Get the day of the week.
TIME_SHIELD_CONSTEXPR const ts_t year_start(const ts_t &ts=ts())
Alias for start_of_year function.
constexpr const T get_weekday_from_ts(const ts_t &ts) noexcept
Get the weekday from a timestamp.
constexpr const T num_days_in_year_ts(const ts_t &ts=ts())
Get the number of days in the current year.
TIME_SHIELD_CONSTEXPR const ts_t year_begin_date(const T &year)
Alias for start_of_year_date function.
constexpr const ts_ms_t sec_to_ms_impl(const T &t, std::true_type) noexcept
const fts_t fts() noexcept
Get the current UTC timestamp in floating-point seconds.
constexpr const T1 day_of_week(const T2 &year, const T3 &month, const T3 &day)
Alias for day_of_week_date function.
const fts_t ftimestamp() noexcept
Get the current UTC timestamp in floating-point seconds.
constexpr const ts_t start_of_prev_day(const ts_t &ts=ts(), const T &days=1)
Get timestamp of the start of the previous day.
T to_date_time_ms(const ts_ms_t &ts)
Converts a timestamp in milliseconds to a date-time structure with milliseconds.
constexpr int64_t SEC_PER_4_YEARS
Seconds per 4 years.
Definition constants.hpp:35
constexpr int64_t SEC_PER_100_YEARS
Seconds per 100 years.
Definition constants.hpp:37
TIME_SHIELD_CONSTEXPR const T year(const ts_t &ts=ts())
Alias for get_year function.
TIME_SHIELD_CONSTEXPR const T get_year(const ts_t &ts=ts())
Get the year from the timestamp.
TIME_SHIELD_CONSTEXPR const ts_t tm_to_timestamp(const std::tm *timeinfo)
Converts a std::tm structure to a timestamp.
TIME_SHIELD_CONSTEXPR const fts_t to_fts(const T1 &year, const T2 &month, const T2 &day, const T2 &hour=0, const T2 &min=0, const T2 &sec=0, const T3 &ms=0)
Alias for to_ftimestamp function.
constexpr const T1 day_of_week_dt(const T2 &date)
Alias for get_weekday_from_date function that accepts a date structure.
TIME_SHIELD_CONSTEXPR const ts_t final_sunday_of_month(const ts_t &ts=ts())
Alias for last_sunday_of_month function.
constexpr const ts_ms_t end_of_hour_ms(const ts_ms_t &ts_ms=ts_ms()) noexcept
Get the timestamp at the end of the hour.
constexpr const ts_ms_t finish_of_hour_ms(const ts_ms_t &ts_ms=ts_ms()) noexcept
Alias for end_of_hour_ms function.
TIME_SHIELD_CONSTEXPR ts_t end_of_year(const ts_t &ts=ts())
Get the end-of-year timestamp.
TIME_SHIELD_CONSTEXPR const bool is_valid_date_time(const T1 &year, const T2 &month, const T2 &day, const T2 &hour=0, const T2 &min=0, const T2 &sec=0, const T3 &ms=0) noexcept
Checks the correctness of a date and time.
TIME_SHIELD_CONSTEXPR const T1 final_sunday_month_day(const T2 &year, const T3 &month)
Alias for last_sunday_month_day function.
constexpr const ts_t start_of_min(const ts_t &ts=ts()) noexcept
Get the timestamp of the beginning of the minute.
TIME_SHIELD_CONSTEXPR const ts_ms_t start_of_year_date_ms(const T &year)
Get the timestamp in milliseconds of the start of the year.
TIME_SHIELD_CONSTEXPR const bool is_leap_year(const ts_t &ts)
Alias for is_leap_year_ts function.
constexpr const T min_of_hour(const ts_t &ts=ts()) noexcept
Get minute of hour. This function returns a value between 0 to 59.
constexpr const ts_ms_t fsec_to_ms(const fts_t &ts) noexcept
Converts a floating-point timestamp from seconds to milliseconds.
T1 to_date_time(const T2 &ts)
Converts a timestamp to a date-time structure.
constexpr const T unix_min(const ts_t &ts=ts())
Alias for get_unix_min function.
int64_t year_t
Integer year type.
Definition types.hpp:13
TIME_SHIELD_CONSTEXPR const ts_t year_start_ms(const ts_t &ts_ms=ts_ms())
Alias for start_of_year_ms function.
constexpr const T unix_day_to_timestamp(const uday_t &unix_day) noexcept
Convert a UNIX day to a timestamp.
@ FEB
February.
Definition enums.hpp:87
TIME_SHIELD_CONSTEXPR const ts_t year_begin_ms(const ts_t &ts_ms=ts_ms())
Alias for start_of_year_ms function.
constexpr const ts_ms_t start_of_hour_ms(const ts_ms_t &ts_ms=ts_ms()) noexcept
Get the timestamp at the start of the hour. This function sets the minute and second to zero.
constexpr const ts_t previous_day_start(const ts_t &ts=ts(), const T &days=1)
Alias for start_of_prev_day function.
constexpr const T sec_of_day_ms(const ts_ms_t &ts_ms) noexcept
Get the second of the day from milliseconds timestamp.
TIME_SHIELD_CONSTEXPR const ts_ms_t year_start_date_ms(const T &year)
Alias for start_of_year_date_ms function.
constexpr int64_t SEC_PER_400_YEARS
Seconds per 400 years.
Definition constants.hpp:38
constexpr const ts_t hour_begin_sec(const ts_ms_t &ts_ms=ts_ms()) noexcept
Alias for start_of_hour_sec function.
constexpr int64_t MAX_YEAR
Maximum representable year.
Definition constants.hpp:64
TIME_SHIELD_CONSTEXPR const ts_t start_of_year(const ts_t &ts) noexcept
Get the start of the year timestamp.
TIME_SHIELD_CONSTEXPR const ts_t dt_to_timestamp_ms(const T &date_time)
Converts a date-time structure to a timestamp in milliseconds.
TIME_SHIELD_CONSTEXPR const T h24_to_h12(const T &hour) noexcept
Alias for hour24_to_12 function.
TIME_SHIELD_CONSTEXPR const ts_ms_t year_begin_date_ms(const T &year)
Alias for start_of_year_date_ms function.
constexpr const T1 ms_to_sec(const T2 &ts_ms) noexcept
Converts a timestamp from milliseconds to seconds.
constexpr const ts_ms_t day_start_ms(const ts_ms_t &ts_ms=ts_ms())
Alias for start_of_day_ms function.
constexpr const ts_t end_of_day_sec(const ts_ms_t &ts_ms=ts_ms()) noexcept
Get the timestamp at the end of the day in seconds.
constexpr const ts_ms_t hour_begin_ms(const ts_ms_t &ts_ms=ts_ms()) noexcept
Alias for start_of_hour_ms function.
constexpr const T days(const ts_t &start, const ts_t &stop) noexcept
Alias for get_days_difference function.
constexpr const ts_t day_end(const ts_t &ts=ts()) noexcept
Alias for end_of_day function.
TIME_SHIELD_CONSTEXPR const ts_t get_ts(const T1 &year, const T2 &month, const T2 &day, const T2 &hour=0, const T2 &min=0, const T2 &sec=0)
Alias for to_timestamp function.
constexpr const ts_t finish_of_week(const ts_t &ts=ts())
Alias for end_of_week function.
constexpr const ts_t end_of_hour(const ts_t &ts=ts()) noexcept
Get the timestamp at the end of the hour. This function sets the minute and second to 59.
@ SAT
Saturday.
Definition enums.hpp:29
@ THU
Thursday.
Definition enums.hpp:27
TIME_SHIELD_CONSTEXPR const ts_t start_of_year_date(const T &year)
Get the timestamp of the start of the year.
constexpr const T get_unix_day(const ts_t &ts=ts()) noexcept
Get UNIX day.
TIME_SHIELD_CONSTEXPR const ts_ms_t to_ts_ms(const T1 &year, const T2 &month, const T2 &day, const T2 &hour=0, const T2 &min=0, const T2 &sec=0, const T2 &ms=0)
Alias for to_timestamp_ms function.
constexpr int64_t NS_PER_SEC
Nanoseconds per second.
Definition constants.hpp:17
const T ms_of_sec() noexcept
Get the millisecond part of the current second.
constexpr int64_t UNIX_EPOCH
Start year of UNIX time.
Definition constants.hpp:62
TIME_SHIELD_CONSTEXPR const ts_t year_start_date(const T &year)
Alias for start_of_year_date function.
const ts_t ts() noexcept
Get the current UTC timestamp in seconds.
constexpr const T unix_year(const ts_t &ts) noexcept
Alias for get_unix_year function.
constexpr const ts_t start_of_period(const T &p, const ts_t &ts=ts())
Get the timestamp of the start of the period.
constexpr int64_t SEC_PER_YEAR
Seconds per year (365 days)
Definition constants.hpp:32
TIME_SHIELD_CONSTEXPR const T1 num_days_in_month_ts(const ts_t &ts=ts()) noexcept
Get the number of days in the month of the given timestamp.
constexpr const T hour_of_day(const ts_t &ts=ts()) noexcept
Get the hour of the day.
TIME_SHIELD_CONSTEXPR const ts_ms_t to_timestamp_ms(const T1 &year, const T2 &month, const T2 &day, const T2 &hour=0, const T2 &min=0, const T2 &sec=0, const T2 &ms=0)
Converts a date and time to a timestamp in milliseconds.
constexpr const T uday_ms(const ts_ms_t &t_ms=ts_ms()) noexcept
Alias for get_unix_day_ms function.
constexpr const ts_ms_t start_of_day_ms(const ts_ms_t &ts_ms=ts_ms())
Get the start of the day timestamp in milliseconds.
constexpr const ts_t end_of_week(const ts_t &ts=ts())
Get the timestamp of the end of the week.
constexpr int64_t US_PER_SEC
Microseconds per second.
Definition constants.hpp:20
TIME_SHIELD_CONSTEXPR const ts_t year_begin(const ts_t &ts=ts())
Alias for start_of_year function.
Structure to represent time zone information.
Header file with time-related utility functions.
Header for time zone structure and related functions.
Header file with time-related validation functions.