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#include <stdexcept>
17
18namespace time_shield {
19
51
56 template<class T = int>
57 constexpr const T ns_of_sec(fts_t ts) noexcept {
58 fts_t temp;
59 return static_cast<T>(std::round(std::modf(ts, &temp) * static_cast<fts_t>(NS_PER_SEC)));
60 }
61
66 template<class T = int>
67 constexpr const T us_of_sec(fts_t ts) noexcept {
68 fts_t temp;
69 return static_cast<T>(std::round(std::modf(ts, &temp) * static_cast<fts_t>(US_PER_SEC)));
70 }
71
76 template<class T = int>
77 constexpr const T ms_of_sec(fts_t ts) noexcept {
78 fts_t temp;
79 return static_cast<T>(std::round(std::modf(ts, &temp) * static_cast<fts_t>(MS_PER_SEC)));
80 }
81
86 template<class T = int>
87 constexpr const T ms_of_ts(ts_ms_t ts) noexcept {
88 return ts % MS_PER_SEC;
89 }
90
91# ifndef TIME_SHIELD_CPP17
92 template<class T>
93 constexpr const ts_ms_t sec_to_ms_impl(T t, std::true_type) noexcept {
94 return static_cast<ts_ms_t>(std::round(t * static_cast<T>(MS_PER_SEC)));
95 }
96
97 template<class T>
98 constexpr const ts_ms_t sec_to_ms_impl(T t, std::false_type) noexcept {
99 return static_cast<ts_ms_t>(t) * static_cast<ts_ms_t>(MS_PER_SEC);
100 }
101# endif
102
108 template<class T1 = ts_ms_t, class T2>
109 constexpr const T1 sec_to_ms(T2 ts) noexcept {
110# ifdef TIME_SHIELD_CPP17
111 if constexpr(std::is_same_v<T2, double>) {
112 return static_cast<T1>(std::round(ts * static_cast<T2>(MS_PER_SEC)));
113 } else
114 if constexpr(std::is_same_v<T2, float>) {
115 return static_cast<T1>(std::round(ts * static_cast<T2>(MS_PER_SEC)));
116 } else {
117 return static_cast<T1>(ts) * static_cast<T1>(MS_PER_SEC);
118 }
119# else
120 return sec_to_ms_impl(ts, std::is_same<T2, double>());
121# endif
122 }
123
127 constexpr const ts_ms_t fsec_to_ms(fts_t ts) noexcept {
128 return static_cast<ts_ms_t>(std::round(ts * static_cast<fts_t>(MS_PER_SEC)));
129 }
130
136 template<class T1 = ts_t, class T2 = ts_ms_t>
137 constexpr const T1 ms_to_sec(T2 ts_ms) noexcept {
138 return static_cast<T1>(ts_ms) / static_cast<T1>(MS_PER_SEC);
139 }
140
145 template<class T = ts_ms_t>
146 constexpr const fts_t ms_to_fsec(T ts_ms) noexcept {
147 return static_cast<fts_t>(ts_ms) / static_cast<fts_t>(MS_PER_SEC);
148 }
149
150//------------------------------------------------------------------------------
151
156 template<class T = year_t>
157 constexpr const T get_unix_year(ts_t ts) noexcept {
158 // 9223372029693630000 - значение на момент 292277024400 от 2000 года
159 // Такое значение приводит к неправильному вычислению умножения n_400_years * SEC_PER_400_YEARS
160 // Поэтому пришлось снизить до 9223371890843040000
161 constexpr int64_t BIAS_292277022000 = 9223371890843040000LL;
162 constexpr int64_t BIAS_2000 = 946684800LL;
163
164 int64_t y = MAX_YEAR;
165 int64_t secs = -((ts - BIAS_2000) - BIAS_292277022000);
166
167 const int64_t n_400_years = secs / SEC_PER_400_YEARS;
168 secs -= n_400_years * SEC_PER_400_YEARS;
169 y -= n_400_years * 400;
170
171 const int64_t n_100_years = secs / SEC_PER_100_YEARS;
172 secs -= n_100_years * SEC_PER_100_YEARS;
173 y -= n_100_years * 100;
174
175 const int64_t n_4_years = secs / SEC_PER_4_YEARS;
176 secs -= n_4_years * SEC_PER_4_YEARS;
177 y -= n_4_years * 4;
178
179 const int64_t n_1_years = secs / SEC_PER_YEAR;
180 secs -= n_1_years * SEC_PER_YEAR;
181 y -= n_1_years;
182
183 y = secs == 0 ? y : y - 1;
184 return y - UNIX_EPOCH;
185 }
186
189 template<class T = year_t>
190 constexpr const T unix_year(ts_t ts) noexcept {
191 return get_unix_year(ts);
192 }
193
196 template<class T = year_t>
197 constexpr const T to_unix_year(ts_t ts) noexcept {
198 return get_unix_year(ts);
199 }
200
201//------------------------------------------------------------------------------
202
207 template<class T = int>
208 TIME_SHIELD_CONSTEXPR inline const T hour24_to_12(T hour) noexcept {
209 if (hour == 0 || hour > 12) return 12;
210 return hour;
211 }
212
215 template<class T = int>
216 TIME_SHIELD_CONSTEXPR inline const T h24_to_h12(T hour) noexcept {
217 return hour24_to_12(hour);
218 }
219
220//------------------------------------------------------------------------------
221
232 template<class T1, class T2 = ts_t>
234 // 9223372029693630000 - значение на момент 292277024400 от 2000 года
235 // Такое значение приводит к неправильному вычислению умножения n_400_years * SEC_PER_400_YEARS
236 // Поэтому пришлось снизить до 9223371890843040000
237 constexpr int64_t BIAS_292277022000 = 9223371890843040000LL;
238 constexpr int64_t BIAS_2000 = 946684800LL;
239
240 int64_t y = MAX_YEAR;
241 uint64_t secs = -((ts - BIAS_2000) - BIAS_292277022000);
242
243 const uint64_t n_400_years = secs / SEC_PER_400_YEARS;
244 secs -= n_400_years * SEC_PER_400_YEARS;
245 y -= n_400_years * 400;
246
247 const uint64_t n_100_years = secs / SEC_PER_100_YEARS;
248 secs -= n_100_years * SEC_PER_100_YEARS;
249 y -= n_100_years * 100;
250
251 const uint64_t n_4_years = secs / SEC_PER_4_YEARS;
252 secs -= n_4_years * SEC_PER_4_YEARS;
253 y -= n_4_years * 4;
254
255 const uint64_t n_1_years = secs / SEC_PER_YEAR;
256 secs -= n_1_years * SEC_PER_YEAR;
257 y -= n_1_years;
258
259 T1 date_time;
260
261 if (secs == 0) {
262 date_time.year = y;
263 date_time.mon = 1;
264 date_time.day = 1;
265 return date_time;
266 }
267
268 date_time.year = y - 1;
269 const bool is_leap_year = is_leap_year_date(date_time.year);
270 secs = is_leap_year ? SEC_PER_LEAP_YEAR - secs : SEC_PER_YEAR - secs;
271 const int days = secs / SEC_PER_DAY;
272
273 constexpr int JAN_AND_FEB_DAY_LEAP_YEAR = 60 - 1;
274 constexpr int TABLE_MONTH_OF_YEAR[] = {
275 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 январь
276 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 февраль
277 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 март
278 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 апрель
279 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,
280 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,
281 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,
282 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,
283 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,
284 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,
285 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,
286 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,
287 };
288 constexpr int TABLE_DAY_OF_YEAR[] = {
289 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 январь
290 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 февраль
291 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 март
292 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 апрель
293 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,
294 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,
295 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,
296 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,
297 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,
298 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,
299 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,
300 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,
301 };
302
303 if (is_leap_year) {
304 const int prev_days = days - 1;
305 date_time.day = days == JAN_AND_FEB_DAY_LEAP_YEAR ? (TABLE_DAY_OF_YEAR[prev_days] + 1) :
306 (days > JAN_AND_FEB_DAY_LEAP_YEAR ? TABLE_DAY_OF_YEAR[prev_days] : TABLE_DAY_OF_YEAR[days]);
307 date_time.mon = days >= JAN_AND_FEB_DAY_LEAP_YEAR ? TABLE_MONTH_OF_YEAR[prev_days] : TABLE_MONTH_OF_YEAR[days];
308 } else {
309 date_time.day = TABLE_DAY_OF_YEAR[days];
310 date_time.mon = TABLE_MONTH_OF_YEAR[days];
311 }
312
313 ts_t day_secs = secs % SEC_PER_DAY;
314 date_time.hour = day_secs / SEC_PER_HOUR;
315 ts_t min_secs = day_secs - date_time.hour * SEC_PER_HOUR;
316 date_time.min = min_secs / SEC_PER_MIN;
317 date_time.sec = min_secs - date_time.min * SEC_PER_MIN;
318# ifdef TIME_SHIELD_CPP17
319 if constexpr (std::is_floating_point<T2>::value) {
320 date_time.ms = static_cast<int>(std::round(std::fmod(static_cast<double>(ts), static_cast<double>(MS_PER_SEC))));
321 } else date_time.ms = 0;
322# else
323 if (std::is_floating_point<T2>::value) {
324 date_time.ms = static_cast<int>(std::round(std::fmod(static_cast<double>(ts), static_cast<double>(MS_PER_SEC))));
325 } else date_time.ms = 0;
326# endif
327 return date_time;
328 }
329
333 template<class T1, class T2 = ts_t>
334 T1 to_dt(T2 ts) {
335 return to_date_time(ts);
336 }
337
338//------------------------------------------------------------------------------
339
345 template<class T>
348 date_time.ms = ms_of_ts(ts); // Extract and set the ms component
349 return date_time;
350 }
351
355 template<class T>
356 inline T to_dt_ms(ts_ms_t ts) {
357 return to_date_time_ms<T>(ts);
358 }
359
360//------------------------------------------------------------------------------
361
377 template<class T1 = year_t, class T2 = int>
378 TIME_SHIELD_CONSTEXPR inline const ts_t to_timestamp(
379 T1 year,
380 T2 month,
381 T2 day,
382 T2 hour = 0,
383 T2 min = 0,
384 T2 sec = 0) {
385
386 if (day >= UNIX_EPOCH && year <= 31) {
387 return to_timestamp((T1)day, month, (T2)year, hour, min, sec);
388 }
389 if (!is_valid_date_time(year, month, day, hour, min, sec)) {
390 throw std::invalid_argument("Invalid date-time combination");
391 }
392
393 int64_t secs = 0;
394 uint64_t years = (static_cast<int64_t>(MAX_YEAR) - year);
395
396 const int64_t n_400_years = years / 400;
397 secs += n_400_years * SEC_PER_400_YEARS;
398 years -= n_400_years * 400;
399
400 const int64_t n_100_years = years / 100;
401 secs += n_100_years * SEC_PER_100_YEARS;
402 years -= n_100_years * 100;
403
404 const int64_t n_4_years = years / 4;
405 secs += n_4_years * SEC_PER_4_YEARS;
406 years -= n_4_years * 4;
407
408 secs += years * SEC_PER_YEAR;
409
410 // 9223372029693630000 - значение на момент 292277024400 от 2000 года
411 // Такое значение приводит к неправильному вычислению умножения n_400_years * SEC_PER_400_YEARS
412 // Поэтому пришлось снизить до 9223371890843040000
413 constexpr int64_t BIAS_292277022000 = 9223371890843040000LL;
414 constexpr int64_t BIAS_2000 = 946684800LL;
415
416 secs = BIAS_292277022000 - secs;
417 secs += BIAS_2000;
418
419 if (month == 1 && day == 1 &&
420 hour == 0 && min == 0 &&
421 sec == 0) {
422 return secs;
423 }
424
425 constexpr int lmos[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
426 constexpr int mos[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
427
428 secs += (is_leap_year_date(year) ? (lmos[month - 1] + day - 1) : (mos[month - 1] + day - 1)) * SEC_PER_DAY;
429 secs += SEC_PER_HOUR * hour + SEC_PER_MIN * min + sec;
430 return secs;
431 }
432
435 template<class T1 = year_t, class T2 = int>
436 TIME_SHIELD_CONSTEXPR inline const ts_t to_ts(
437 T1 year,
438 T2 month,
439 T2 day,
440 T2 hour = 0,
441 T2 min = 0,
442 T2 sec = 0) {
443 return to_timestamp<T1, T2>(year, month, day, hour, min, sec);
444 }
445
448 template<class T1 = year_t, class T2 = int>
449 TIME_SHIELD_CONSTEXPR inline const ts_t get_ts(
450 T1 year,
451 T2 month,
452 T2 day,
453 T2 hour = 0,
454 T2 min = 0,
455 T2 sec = 0) {
456 return to_timestamp<T1, T2>(year, month, day, hour, min, sec);
457 }
458
461 template<class T1 = year_t, class T2 = int>
462 TIME_SHIELD_CONSTEXPR inline const ts_t ts(
463 T1 year,
464 T2 month,
465 T2 day,
466 T2 hour = 0,
467 T2 min = 0,
468 T2 sec = 0) {
469 return to_timestamp<T1, T2>(year, month, day, hour, min, sec);
470 }
471
474 template<class T1 = year_t, class T2 = int>
475 TIME_SHIELD_CONSTEXPR inline const ts_t timestamp(
476 T1 year,
477 T2 month,
478 T2 day,
479 T2 hour = 0,
480 T2 min = 0,
481 T2 sec = 0) {
482 return to_timestamp<T1, T2>(year, month, day, hour, min, sec);
483 }
484
487 template<class T1 = year_t, class T2 = int>
488 TIME_SHIELD_CONSTEXPR inline const ts_t get_timestamp(
489 T1 year,
490 T2 month,
491 T2 day,
492 T2 hour = 0,
493 T2 min = 0,
494 T2 sec = 0) {
495 return to_timestamp<T1, T2>(year, month, day, hour, min, sec);
496 }
497
498//------------------------------------------------------------------------------
499
510 template<class T>
511 TIME_SHIELD_CONSTEXPR inline const ts_t dt_to_timestamp(
512 const T& date_time) {
513 return to_timestamp(
514 date_time.year,
515 date_time.mon,
516 date_time.day,
517 date_time.hour,
518 date_time.min,
519 date_time.sec);
520 }
521
525 template<class T>
526 TIME_SHIELD_CONSTEXPR inline const ts_t to_timestamp(
527 const T& date_time) {
528 return dt_to_timestamp(date_time);
529 }
530
534 template<class T>
535 TIME_SHIELD_CONSTEXPR inline const ts_t to_ts(
536 const T& date_time) {
537 return dt_to_timestamp(date_time);
538 }
539
543 template<class T>
544 TIME_SHIELD_CONSTEXPR inline const ts_t ts(
545 const T& date_time) {
546 return dt_to_timestamp(date_time);
547 }
548
552 template<class T>
553 TIME_SHIELD_CONSTEXPR inline const ts_t timestamp(
554 const T& date_time) {
555 return dt_to_timestamp(date_time);
556 }
557
558//------------------------------------------------------------------------------
559
568 TIME_SHIELD_CONSTEXPR inline const ts_t tm_to_timestamp(
569 const std::tm *timeinfo) {
570 return to_timestamp(
571 timeinfo->tm_year + 1900,
572 timeinfo->tm_mon + 1,
573 timeinfo->tm_mday,
574 timeinfo->tm_hour,
575 timeinfo->tm_min,
576 timeinfo->tm_sec);
577 }
578
581 TIME_SHIELD_CONSTEXPR inline const ts_t to_timestamp(
582 const std::tm *timeinfo) {
583 return tm_to_timestamp(timeinfo);
584 }
585
588 TIME_SHIELD_CONSTEXPR inline const ts_t to_ts(
589 const std::tm *timeinfo) {
590 return tm_to_timestamp(timeinfo);
591 }
592
595 TIME_SHIELD_CONSTEXPR inline const ts_t ts(
596 const std::tm *timeinfo) {
597 return tm_to_timestamp(timeinfo);
598 }
599
602 TIME_SHIELD_CONSTEXPR inline const ts_t timestamp(
603 const std::tm *timeinfo) {
604 return tm_to_timestamp(timeinfo);
605 }
606
609 TIME_SHIELD_CONSTEXPR inline const ts_t ts_from_tm(
610 const std::tm *timeinfo) {
611 return tm_to_timestamp(timeinfo);
612 }
613
614//------------------------------------------------------------------------------
615
632 template<class T1 = year_t, class T2 = int>
633 TIME_SHIELD_CONSTEXPR inline const ts_ms_t to_timestamp_ms(
634 T1 year,
635 T2 month,
636 T2 day,
637 T2 hour = 0,
638 T2 min = 0,
639 T2 sec = 0,
640 T2 ms = 0) {
641 return sec_to_ms(to_timestamp<T1, T2>(year, month, day, hour, min, sec)) + ms;
642 }
643
646 template<class T1 = year_t, class T2 = int>
647 TIME_SHIELD_CONSTEXPR inline const ts_ms_t to_ts_ms(
648 T1 year,
649 T2 month,
650 T2 day,
651 T2 hour = 0,
652 T2 min = 0,
653 T2 sec = 0,
654 T2 ms = 0) {
655 return to_timestamp_ms(year, month, day, hour, min, sec, ms);
656 }
657
660 template<class T1 = year_t, class T2 = int>
661 TIME_SHIELD_CONSTEXPR inline const ts_ms_t ts_ms(
662 T1 year,
663 T2 month,
664 T2 day,
665 T2 hour = 0,
666 T2 min = 0,
667 T2 sec = 0,
668 T2 ms = 0) {
669 return to_timestamp_ms(year, month, day, hour, min, sec, ms);
670 }
671
674 template<class T1 = year_t, class T2 = int>
675 TIME_SHIELD_CONSTEXPR inline const ts_ms_t timestamp_ms(
676 T1 year,
677 T2 month,
678 T2 day,
679 T2 hour = 0,
680 T2 min = 0,
681 T2 sec = 0,
682 T2 ms = 0) {
683 return to_timestamp_ms(year, month, day, hour, min, sec, ms);
684 }
685
686//------------------------------------------------------------------------------
687
698 template<class T>
699 TIME_SHIELD_CONSTEXPR inline const ts_t dt_to_timestamp_ms(
700 const T& date_time) {
701 return sec_to_ms(dt_to_timestamp(date_time)) + date_time.ms;
702 }
703
707 template<class T>
708 TIME_SHIELD_CONSTEXPR inline const ts_t to_timestamp_ms(
709 const T& date_time) {
710 return dt_to_timestamp_ms(date_time);
711 }
712
716 template<class T>
717 TIME_SHIELD_CONSTEXPR inline const ts_t to_ts_ms(
718 const T& date_time) {
719 return dt_to_timestamp_ms(date_time);
720 }
721
725 template<class T>
726 TIME_SHIELD_CONSTEXPR inline const ts_t ts_ms(
727 const T& date_time) {
728 return dt_to_timestamp_ms(date_time);
729 }
730
734 template<class T>
735 TIME_SHIELD_CONSTEXPR inline const ts_t timestamp_ms(
736 const T& date_time) {
737 return dt_to_timestamp_ms(date_time);
738 }
739
740//------------------------------------------------------------------------------
741
749 TIME_SHIELD_CONSTEXPR inline const ts_t tm_to_timestamp_ms(
750 const std::tm *timeinfo) {
751 return sec_to_ms(tm_to_timestamp(timeinfo));
752 }
753
756 TIME_SHIELD_CONSTEXPR inline const ts_t to_timestamp_ms(
757 const std::tm *timeinfo) {
758 return tm_to_timestamp_ms(timeinfo);
759 }
760
763 TIME_SHIELD_CONSTEXPR inline const ts_t to_ts_ms(
764 const std::tm *timeinfo) {
765 return tm_to_timestamp_ms(timeinfo);
766 }
767
770 TIME_SHIELD_CONSTEXPR inline const ts_t ts_ms(
771 const std::tm *timeinfo) {
772 return tm_to_timestamp_ms(timeinfo);
773 }
774
777 TIME_SHIELD_CONSTEXPR inline const ts_t timestamp_ms(
778 const std::tm *timeinfo) {
779 return tm_to_timestamp_ms(timeinfo);
780 }
781
782//------------------------------------------------------------------------------
783
802 template<class T1 = year_t, class T2 = int, class T3 = int>
803 TIME_SHIELD_CONSTEXPR inline const fts_t to_ftimestamp(
804 T1 year,
805 T2 month,
806 T2 day,
807 T2 hour = 0,
808 T2 min = 0,
809 T2 sec = 0,
810 T3 ms = 0) {
811 return static_cast<fts_t>(to_timestamp(year, month, day, hour, min, sec)) +
812 static_cast<fts_t>(ms)/static_cast<fts_t>(MS_PER_SEC);
813 }
814
817 template<class T1 = year_t, class T2 = int, class T3 = int>
818 TIME_SHIELD_CONSTEXPR inline const fts_t to_fts(
819 T1 year,
820 T2 month,
821 T2 day,
822 T2 hour = 0,
823 T2 min = 0,
824 T2 sec = 0,
825 T3 ms = 0) {
826 return to_ftimestamp(year, month, day, hour, min, sec, ms);
827 }
828
831 template<class T1 = year_t, class T2 = int, class T3 = int>
832 TIME_SHIELD_CONSTEXPR inline const fts_t fts(
833 T1 year,
834 T2 month,
835 T2 day,
836 T2 hour = 0,
837 T2 min = 0,
838 T2 sec = 0,
839 T3 ms = 0) {
840 return to_ftimestamp(year, month, day, hour, min, sec, ms);
841 }
842
845 template<class T1 = year_t, class T2 = int, class T3 = int>
846 TIME_SHIELD_CONSTEXPR inline const fts_t ftimestamp(
847 T1 year,
848 T2 month,
849 T2 day,
850 T2 hour = 0,
851 T2 min = 0,
852 T2 sec = 0,
853 T3 ms = 0) {
854 return to_ftimestamp(year, month, day, hour, min, sec, ms);
855 }
856
857//------------------------------------------------------------------------------
858
870 template<class T>
871 TIME_SHIELD_CONSTEXPR inline const fts_t dt_to_ftimestamp(
872 const T& date_time) {
873 return static_cast<fts_t>(to_timestamp(date_time)) +
874 static_cast<fts_t>(date_time.ms)/static_cast<fts_t>(MS_PER_SEC);
875 }
876
880 template<class T>
881 TIME_SHIELD_CONSTEXPR inline const fts_t to_ftimestamp(
882 const T& date_time) {
883 return dt_to_ftimestamp(date_time);
884 }
885
889 template<class T>
890 TIME_SHIELD_CONSTEXPR inline const fts_t to_fts(
891 const T& date_time) {
892 return dt_to_ftimestamp(date_time);
893 }
894
898 template<class T>
899 TIME_SHIELD_CONSTEXPR inline const fts_t fts(
900 const T& date_time) {
901 return dt_to_ftimestamp(date_time);
902 }
903
907 template<class T>
908 TIME_SHIELD_CONSTEXPR inline const fts_t ftimestamp(
909 const T& date_time) {
910 return dt_to_ftimestamp(date_time);
911 }
912
913//------------------------------------------------------------------------------
914
924 TIME_SHIELD_CONSTEXPR inline const fts_t tm_to_ftimestamp(
925 const std::tm* timeinfo) {
926 return static_cast<fts_t>(tm_to_timestamp(timeinfo));
927 }
928
931 TIME_SHIELD_CONSTEXPR inline const fts_t to_ftimestamp(
932 const std::tm* timeinfo) {
933 return tm_to_ftimestamp(timeinfo);
934 }
935
938 TIME_SHIELD_CONSTEXPR inline const fts_t to_fts(
939 const std::tm* timeinfo) {
940 return tm_to_ftimestamp(timeinfo);
941 }
942
945 TIME_SHIELD_CONSTEXPR inline const fts_t fts(
946 const std::tm* timeinfo) {
947 return tm_to_ftimestamp(timeinfo);
948 }
949
952 TIME_SHIELD_CONSTEXPR inline const fts_t ftimestamp(
953 const std::tm* timeinfo) {
954 return tm_to_ftimestamp(timeinfo);
955 }
956
957//------------------------------------------------------------------------------
958
966 template<class T = uday_t>
967 constexpr const T get_unix_day(ts_t ts = ts()) noexcept {
968 return ts / SEC_PER_DAY;
969 }
970
973 template<class T = uday_t>
974 constexpr const T get_unixday(ts_t ts = ts()) noexcept {
975 return get_unix_day(ts);
976 }
977
980 template<class T = uday_t>
981 constexpr const T unix_day(ts_t ts = ts()) noexcept {
982 return get_unix_day(ts);
983 }
984
987 template<class T = uday_t>
988 constexpr const T unixday(ts_t ts = ts()) noexcept {
989 return get_unix_day(ts);
990 }
991
994 template<class T = uday_t>
995 constexpr const T uday(ts_t ts = ts()) noexcept {
996 return get_unix_day(ts);
997 }
998
999//------------------------------------------------------------------------------
1000
1001
1002
1003//------------------------------------------------------------------------------
1004
1013 template<class T = int>
1014 constexpr const T get_days_difference(ts_t start, ts_t stop) noexcept {
1015 return (stop - start) / SEC_PER_DAY;
1016 }
1017
1020 template<class T = int>
1021 constexpr const T get_days(ts_t start, ts_t stop) noexcept {
1022 return get_days_difference(start, stop);
1023 }
1024
1027 template<class T = int>
1028 constexpr const T days(ts_t start, ts_t stop) noexcept {
1029 return get_days_difference(start, stop);
1030 }
1031
1032//------------------------------------------------------------------------------
1033
1041 template<class T = uday_t>
1042 constexpr const T get_unix_day_ms(ts_ms_t t_ms = ts_ms()) noexcept {
1043 return get_unix_day(ms_to_sec(t_ms));
1044 }
1045
1048 template<class T = uday_t>
1049 constexpr const T get_unixday_ms(ts_ms_t t_ms = ts_ms()) noexcept {
1050 return get_unix_day_ms(t_ms);
1051 }
1052
1055 template<class T = uday_t>
1056 constexpr const T unix_day_ms(ts_ms_t t_ms = ts_ms()) noexcept {
1057 return get_unix_day_ms(t_ms);
1058 }
1059
1062 template<class T = uday_t>
1063 constexpr const T unixday_ms(ts_ms_t t_ms = ts_ms()) noexcept {
1064 return get_unix_day_ms(t_ms);
1065 }
1066
1069 template<class T = uday_t>
1070 constexpr const T uday_ms(ts_ms_t t_ms = ts_ms()) noexcept {
1071 return get_unix_day_ms(t_ms);
1072 }
1073
1074//------------------------------------------------------------------------------
1075
1084 template<class T = ts_t>
1085 constexpr const T unix_day_to_timestamp(uday_t unix_day) noexcept {
1086 return unix_day * SEC_PER_DAY;
1087 }
1088
1091 template<class T = ts_t>
1092 constexpr const T unix_day_to_ts(uday_t unix_day) noexcept {
1094 }
1095
1098 template<class T = ts_t>
1099 constexpr const T unixday_to_ts(uday_t unix_day) noexcept {
1101 }
1102
1105 template<class T = ts_t>
1106 constexpr const T uday_to_ts(uday_t unix_day) noexcept {
1108 }
1109
1112 template<class T = ts_t>
1113 constexpr const T start_of_day_from_unix_day(uday_t unix_day) noexcept {
1115 }
1116
1125 template<class T = ts_t>
1126 constexpr const T unix_day_to_timestamp_ms(uday_t unix_day) noexcept {
1127 return unix_day * MS_PER_DAY;
1128 }
1129
1132 template<class T = ts_t>
1133 constexpr const T unix_day_to_ts_ms(uday_t unix_day) noexcept {
1135 }
1136
1139 template<class T = ts_t>
1140 constexpr const T unixday_to_ts_ms(uday_t unix_day) noexcept {
1142 }
1143
1146 template<class T = ts_t>
1147 constexpr const T uday_to_ts_ms(uday_t unix_day) noexcept {
1149 }
1150
1153 template<class T = ts_t>
1154 constexpr const T start_of_day_from_unix_day_ms(uday_t unix_day) noexcept {
1156 }
1157
1158//------------------------------------------------------------------------------
1159
1168 template<class T = ts_t>
1169 constexpr const T end_of_day_from_unix_day(uday_t unix_day) noexcept {
1170 return unix_day * SEC_PER_DAY + SEC_PER_DAY - 1;
1171 }
1172
1181 template<class T = ts_ms_t>
1182 constexpr const T end_of_day_from_unix_day_ms(uday_t unix_day) noexcept {
1183 return unix_day * MS_PER_DAY + MS_PER_DAY - 1;
1184 }
1185
1194 template<class T = ts_ms_t>
1196 return unix_day * SEC_PER_DAY + SEC_PER_DAY;
1197 }
1198
1207 template<class T = ts_ms_t>
1209 return unix_day * MS_PER_DAY + MS_PER_DAY;
1210 }
1211
1214 template<class T = ts_t>
1215 constexpr const T next_day_from_unix_day(uday_t unix_day) noexcept {
1217 }
1218
1221 template<class T = ts_t>
1222 constexpr const T next_day_unix_day(uday_t unix_day) noexcept {
1224 }
1225
1228 template<class T = ts_t>
1229 constexpr const T next_day_unixday(uday_t unix_day) noexcept {
1231 }
1232
1235 template<class T = ts_ms_t>
1239
1242 template<class T = ts_ms_t>
1243 constexpr const T next_day_unix_day_ms(uday_t unix_day) noexcept {
1245 }
1246
1249 template<class T = ts_ms_t>
1250 constexpr const T next_day_unixday_ms(uday_t unix_day) noexcept {
1252 }
1253
1254//------------------------------------------------------------------------------
1255
1263 template<class T = int64_t>
1264 constexpr const T get_unix_min(ts_t ts = ts()) {
1265 return ts / SEC_PER_MIN;
1266 }
1267
1270 template<class T = int64_t>
1271 constexpr const T unix_min(ts_t ts = ts()) {
1272 return get_unix_min(ts);
1273 }
1274
1277 template<class T = int64_t>
1278 constexpr const T to_unix_min(ts_t ts = ts()) {
1279 return get_unix_min(ts);
1280 }
1281
1284 template<class T = int64_t>
1285 constexpr const T umin(ts_t ts = ts()) {
1286 return get_unix_min(ts);
1287 }
1288
1289//------------------------------------------------------------------------------
1290
1298 template<class T = int>
1299 constexpr const T sec_of_day(ts_t ts = ts()) noexcept {
1300 return ts % SEC_PER_DAY;
1301 }
1302
1310 template<class T = int>
1311 constexpr const T sec_of_day_ms(ts_ms_t ts_ms) noexcept {
1312 return sec_of_day(ms_to_sec(ts_ms));
1313 }
1314
1325 template<class T1 = int, class T2 = int>
1326 constexpr const T1 sec_of_day(
1327 T2 hour,
1328 T2 min,
1329 T2 sec) noexcept {
1330 return hour * SEC_PER_HOUR + min * SEC_PER_MIN + sec;
1331 }
1332
1340 template<class T = int>
1341 constexpr const T sec_of_min(ts_t ts = ts()) {
1342 return (ts % SEC_PER_MIN);
1343 }
1344
1352 template<class T = int>
1353 constexpr const T sec_of_hour(ts_t ts = ts()) {
1354 return (ts % SEC_PER_HOUR);
1355 }
1356
1357//------------------------------------------------------------------------------
1358
1366 template<class T = year_t>
1367 TIME_SHIELD_CONSTEXPR inline const T get_year(ts_t ts = ts()) {
1368 return get_unix_year(ts) + UNIX_EPOCH;
1369 }
1370
1373 template<class T = year_t>
1374 TIME_SHIELD_CONSTEXPR inline const T year(ts_t ts = ts()) {
1375 return get_year(ts);
1376 }
1377
1380 template<class T = year_t>
1381 TIME_SHIELD_CONSTEXPR inline const T to_year(ts_t ts = ts()) {
1382 return get_year(ts);
1383 }
1384
1385//------------------------------------------------------------------------------
1386
1394 template<class T = year_t>
1395 TIME_SHIELD_CONSTEXPR inline const T get_year_ms(ts_ms_t ts_ms = ts_ms()) {
1396 return get_year(ms_to_sec(ts_ms));
1397 }
1398
1401 template<class T = year_t>
1402 TIME_SHIELD_CONSTEXPR inline const T year_ms(ts_ms_t ts_ms = ts_ms()) {
1403 return get_year_ms(ts_ms);
1404 }
1405
1408 template<class T = year_t>
1409 TIME_SHIELD_CONSTEXPR inline const T to_year_ms(ts_ms_t ts_ms = ts_ms()) {
1410 return get_year_ms(ts_ms);
1411 }
1412
1413//------------------------------------------------------------------------------
1414
1422 TIME_SHIELD_CONSTEXPR inline const ts_t start_of_year(ts_t ts) noexcept {
1423 constexpr ts_t BIAS_2100 = 4102444800;
1424 if (ts < BIAS_2100) {
1425 constexpr ts_t SEC_PER_YEAR_X2 = SEC_PER_YEAR * 2;
1426 ts_t year_start_ts = ts % SEC_PER_4_YEARS;
1427 if (year_start_ts < SEC_PER_YEAR) {
1428 return ts - year_start_ts;
1429 } else
1430 if (year_start_ts < SEC_PER_YEAR_X2) {
1431 return ts + SEC_PER_YEAR - year_start_ts;
1432 } else
1433 if (year_start_ts < (SEC_PER_YEAR_X2 + SEC_PER_LEAP_YEAR)) {
1434 return ts + SEC_PER_YEAR_X2 - year_start_ts;
1435 }
1436 return ts + (SEC_PER_YEAR_X2 + SEC_PER_LEAP_YEAR) - year_start_ts;
1437 }
1438
1439 constexpr ts_t BIAS_2000 = 946684800;
1440 ts_t secs = ts - BIAS_2000;
1441
1442 ts_t offset_y400 = secs % SEC_PER_400_YEARS;
1443 ts_t start_ts = secs - offset_y400 + BIAS_2000;
1444 secs = offset_y400;
1445
1446 if (secs >= SEC_PER_FIRST_100_YEARS) {
1448 start_ts += SEC_PER_FIRST_100_YEARS;
1449 while (secs >= SEC_PER_100_YEARS) {
1450 secs -= SEC_PER_100_YEARS;
1451 start_ts += SEC_PER_100_YEARS;
1452 }
1453
1454 constexpr ts_t SEC_PER_4_YEARS_V2 = 4 * SEC_PER_YEAR;
1455 if (secs >= SEC_PER_4_YEARS_V2) {
1456 secs -= SEC_PER_4_YEARS_V2;
1457 start_ts += SEC_PER_4_YEARS_V2;
1458 } else {
1459 start_ts += secs - secs % SEC_PER_YEAR;
1460 return start_ts;
1461 }
1462 }
1463
1464 ts_t offset_4y = secs % SEC_PER_4_YEARS;
1465 start_ts += secs - offset_4y;
1466 secs = offset_4y;
1467
1468 if (secs >= SEC_PER_LEAP_YEAR) {
1469 secs -= SEC_PER_LEAP_YEAR;
1470 start_ts += SEC_PER_LEAP_YEAR;
1471 start_ts += secs - secs % SEC_PER_YEAR;
1472 }
1473 return start_ts;
1474 }
1475
1478 TIME_SHIELD_CONSTEXPR inline const ts_t year_start(ts_t ts = ts()) {
1479 return start_of_year(ts);
1480 }
1481
1484 TIME_SHIELD_CONSTEXPR inline const ts_t year_begin(ts_t ts = ts()) {
1485 return start_of_year(ts);
1486 }
1487
1488//------------------------------------------------------------------------------
1489
1497 TIME_SHIELD_CONSTEXPR inline const ts_ms_t start_of_year_ms(ts_ms_t ts_ms = ts_ms()) noexcept {
1499 }
1500
1503 TIME_SHIELD_CONSTEXPR inline const ts_t year_start_ms(ts_t ts_ms = ts_ms()) {
1504 return start_of_year_ms(ts_ms);
1505 }
1506
1509 TIME_SHIELD_CONSTEXPR inline const ts_t year_begin_ms(ts_t ts_ms = ts_ms()) {
1510 return start_of_year_ms(ts_ms);
1511 }
1512
1513//------------------------------------------------------------------------------
1514
1522 template<class T = year_t>
1523 TIME_SHIELD_CONSTEXPR inline const ts_t start_of_year_date(T year) {
1524 if (year < 2100) {
1525 const ts_t year_diff = year >= UNIX_EPOCH ? year - UNIX_EPOCH : UNIX_EPOCH - year;
1526 const ts_t year_start_ts = (year_diff / 4) * SEC_PER_4_YEARS;
1527 const ts_t year_remainder = year_diff % 4;
1528 constexpr ts_t SEC_PER_YEAR_X2 = 2 * SEC_PER_YEAR;
1529 constexpr ts_t SEC_PER_YEAR_V2 = SEC_PER_YEAR_X2 + SEC_PER_LEAP_YEAR;
1530 switch (year_remainder) {
1531 case 0: return year_start_ts;
1532 case 1: return year_start_ts + SEC_PER_YEAR;
1533 case 2: return year_start_ts + SEC_PER_YEAR_X2;
1534 default: return year_start_ts + SEC_PER_YEAR_V2;
1535 };
1536 return year_start_ts + SEC_PER_YEAR_V2;
1537 }
1538 return to_timestamp(year, 1, 1);
1539 }
1540
1543 template<class T = year_t>
1544 TIME_SHIELD_CONSTEXPR inline const ts_t year_start_date(T year) {
1545 return start_of_year_date(year);
1546 }
1547
1550 template<class T = year_t>
1551 TIME_SHIELD_CONSTEXPR inline const ts_t year_begin_date(T year) {
1552 return start_of_year_date(year);
1553 }
1554
1555//------------------------------------------------------------------------------
1556
1564 template<class T = year_t>
1565 TIME_SHIELD_CONSTEXPR inline const ts_ms_t start_of_year_date_ms(T year) {
1567 }
1568
1571 template<class T = year_t>
1572 TIME_SHIELD_CONSTEXPR inline const ts_ms_t year_start_date_ms(T year) {
1574 }
1575
1578 template<class T = year_t>
1579 TIME_SHIELD_CONSTEXPR inline const ts_ms_t year_begin_date_ms(T year) {
1581 }
1582
1583//------------------------------------------------------------------------------
1584
1591 TIME_SHIELD_CONSTEXPR inline ts_t end_of_year(ts_t ts = ts()) {
1592 constexpr ts_t BIAS_2100 = 4102444800;
1593 if (ts < BIAS_2100) {
1594 constexpr ts_t SEC_PER_YEAR_X2 = SEC_PER_YEAR * 2;
1595 constexpr ts_t SEC_PER_YEAR_X3 = SEC_PER_YEAR * 3;
1596 constexpr ts_t SEC_PER_YEAR_X3_V2 = SEC_PER_YEAR_X2 + SEC_PER_LEAP_YEAR;
1597 ts_t year_end_ts = ts % SEC_PER_4_YEARS;
1598 if (year_end_ts < SEC_PER_YEAR) {
1599 return ts + SEC_PER_YEAR - year_end_ts - 1;
1600 } else
1601 if (year_end_ts < SEC_PER_YEAR_X2) {
1602 return ts + SEC_PER_YEAR_X2 - year_end_ts - 1;
1603 } else
1604 if (year_end_ts < SEC_PER_YEAR_X3_V2) {
1605 return ts + SEC_PER_YEAR_X3_V2 - year_end_ts - 1;
1606 }
1607 return ts + (SEC_PER_YEAR_X3 + SEC_PER_LEAP_YEAR) - year_end_ts - 1;
1608 }
1609
1610 constexpr ts_t BIAS_2000 = 946684800;
1611 ts_t secs = ts - BIAS_2000;
1612
1613 ts_t offset_y400 = secs % SEC_PER_400_YEARS;
1614 ts_t end_ts = secs - offset_y400 + BIAS_2000;
1615 secs = offset_y400;
1616
1617 if (secs >= SEC_PER_FIRST_100_YEARS) {
1619 end_ts += SEC_PER_FIRST_100_YEARS;
1620 while (secs >= SEC_PER_100_YEARS) {
1621 secs -= SEC_PER_100_YEARS;
1622 end_ts += SEC_PER_100_YEARS;
1623 }
1624
1625 constexpr ts_t SEC_PER_4_YEARS_V2 = 4 * SEC_PER_YEAR;
1626 if (secs >= SEC_PER_4_YEARS_V2) {
1627 secs -= SEC_PER_4_YEARS_V2;
1628 end_ts += SEC_PER_4_YEARS_V2;
1629 } else {
1630 end_ts += secs - secs % SEC_PER_YEAR;
1631 return end_ts + SEC_PER_YEAR - 1;
1632 }
1633 }
1634
1635 ts_t offset_4y = secs % SEC_PER_4_YEARS;
1636 end_ts += secs - offset_4y;
1637 secs = offset_4y;
1638
1639 if (secs >= SEC_PER_LEAP_YEAR) {
1640 secs -= SEC_PER_LEAP_YEAR;
1641 end_ts += SEC_PER_LEAP_YEAR;
1642 end_ts += secs - secs % SEC_PER_YEAR;
1643 end_ts += SEC_PER_YEAR;
1644 } else {
1645 end_ts += SEC_PER_LEAP_YEAR;
1646 }
1647 return end_ts - 1;
1648 }
1649
1652 TIME_SHIELD_CONSTEXPR inline const ts_t year_end(ts_t ts = ts()) {
1653 return end_of_year(ts);
1654 }
1655
1656//------------------------------------------------------------------------------
1657
1664 template<class T = year_t>
1665 TIME_SHIELD_CONSTEXPR inline const ts_ms_t end_of_year_ms(ts_ms_t ts_ms = ts_ms()) {
1667 }
1668
1671 TIME_SHIELD_CONSTEXPR inline const ts_ms_t year_end_ms(ts_ms_t ts_ms = ts_ms()) {
1672 return end_of_year_ms(ts_ms);
1673 }
1674
1675//------------------------------------------------------------------------------
1676
1683 template<class T = int>
1684 inline const T day_of_year(ts_t ts = ts()) {
1685 return ((ts - start_of_year(ts)) / SEC_PER_DAY) + 1;
1686 }
1687
1688//------------------------------------------------------------------------------
1689
1696 template<class T = Month>
1697 TIME_SHIELD_CONSTEXPR inline const T month_of_year(ts_t ts) noexcept {
1698 constexpr int JAN_AND_FEB_DAY_LEAP_YEAR = 60;
1699 constexpr int TABLE_MONTH_OF_YEAR[] = {
1700 0,
1701 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 январь
1702 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 февраль
1703 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 март
1704 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 апрель
1705 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,
1706 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,
1707 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,
1708 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,
1709 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,
1710 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,
1711 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,
1712 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,
1713 };
1714 const size_t dy = day_of_year(ts);
1715 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]);
1716 }
1717
1718//------------------------------------------------------------------------------
1719
1726 template<class T = int>
1727 TIME_SHIELD_CONSTEXPR inline const T day_of_month(ts_t ts) {
1728 constexpr int JAN_AND_FEB_DAY_LEAP_YEAR = 60;
1729 // таблица для обычного года, не високосного
1730 constexpr int TABLE_DAY_OF_YEAR[] = {
1731 0,
1732 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 январь
1733 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 февраль
1734 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 март
1735 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 апрель
1736 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,
1737 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,
1738 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,
1739 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,
1740 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,
1741 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,
1742 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,
1743 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,
1744 };
1745 const size_t dy = day_of_year(ts);
1746 if(is_leap_year(ts)) {
1747 if(dy == JAN_AND_FEB_DAY_LEAP_YEAR) return TABLE_DAY_OF_YEAR[dy - 1] + 1;
1748 if(dy > JAN_AND_FEB_DAY_LEAP_YEAR) return TABLE_DAY_OF_YEAR[dy - 1];
1749 }
1750 return TABLE_DAY_OF_YEAR[dy];
1751 }
1752
1753//------------------------------------------------------------------------------
1754
1762 template<class T1 = int, class T2 = year_t, class T3 = int>
1763 constexpr const T1 num_days_in_month(T2 year, T3 month) noexcept {
1764 if (month > MONTHS_PER_YEAR || month < 0) return 0;
1765 constexpr T1 num_days[13] = {0,31,30,31,30,31,30,31,31,30,31,30,31};
1766 if (month == FEB) {
1767 if (is_leap_year_date(year)) return 29;
1768 return 28;
1769 }
1770 return num_days[month];
1771 }
1772
1775 template<class T1 = int, class T2 = year_t, class T3 = int>
1776 constexpr const T1 days_in_month(T2 year, T3 month) noexcept {
1777 return num_days_in_month(year, month);
1778 }
1779
1780//------------------------------------------------------------------------------
1781
1788 template<class T1 = int>
1789 TIME_SHIELD_CONSTEXPR const T1 num_days_in_month_ts(ts_t ts = ts()) noexcept {
1790 constexpr T1 num_days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
1791 const int month = month_of_year(ts);
1792 if (month == FEB) {
1793 return is_leap_year(ts) ? 29 : 28;
1794 }
1795 return num_days[month];
1796 }
1797
1800 template<class T1 = int>
1801 constexpr const T1 num_days_in_month(ts_t ts = ts()) noexcept {
1802 return num_days_in_month_ts(ts);
1803 }
1804
1807 template<class T1 = int>
1808 constexpr const T1 days_in_month(ts_t ts = ts()) noexcept {
1809 return num_days_in_month_ts(ts);
1810 }
1811
1812//------------------------------------------------------------------------------
1813
1820 template<class T1 = int, class T2 = year_t>
1821 constexpr const T1 num_days_in_year(T2 year) noexcept {
1823 return DAYS_PER_YEAR;
1824 }
1825
1828 template<class T1 = int, class T2 = year_t>
1829 constexpr const T1 days_in_year(T2 year) noexcept {
1830 return num_days_in_year(year);
1831 }
1832
1833//------------------------------------------------------------------------------
1834
1841 template<class T = int>
1842 constexpr const T num_days_in_year_ts(ts_t ts = ts()) {
1844 return DAYS_PER_YEAR;
1845 }
1846
1849 template<class T = int>
1850 constexpr const T days_in_year_ts(ts_t ts = ts()) {
1851 return num_days_in_year_ts(ts);
1852 }
1853
1854//------------------------------------------------------------------------------
1855
1863 constexpr const ts_t start_of_day(ts_t ts = ts()) noexcept {
1864 return ts - (ts % SEC_PER_DAY);
1865 }
1866
1869 constexpr const ts_t day_start(ts_t ts = ts()) noexcept {
1870 return start_of_day(ts);
1871 }
1872
1873//------------------------------------------------------------------------------
1874
1882 template<class T = int>
1883 constexpr const ts_t start_of_prev_day(ts_t ts = ts(), T days = 1) noexcept {
1884 return ts - (ts % SEC_PER_DAY) - SEC_PER_DAY * days;
1885 }
1886
1889 template<class T = int>
1890 constexpr const ts_t previous_day_start(ts_t ts = ts(), T days = 1) noexcept {
1891 return start_of_prev_day(ts, days);
1892 }
1893
1894//------------------------------------------------------------------------------
1895
1903 constexpr const ts_t start_of_day_sec(ts_ms_t ts_ms = ts_ms()) noexcept {
1904 return start_of_day(ms_to_sec(ts_ms));
1905 }
1906
1909 constexpr const ts_t day_start_sec(ts_ms_t ts_ms = ts_ms()) noexcept {
1910 return start_of_day_sec(ts_ms);
1911 }
1912
1913//------------------------------------------------------------------------------
1914
1922 constexpr const ts_ms_t start_of_day_ms(ts_ms_t ts_ms = ts_ms()) noexcept {
1923 return ts_ms - (ts_ms % MS_PER_DAY);
1924 }
1925
1928 constexpr const ts_ms_t day_start_ms(ts_ms_t ts_ms = ts_ms()) noexcept {
1929 return start_of_day_ms(ts_ms);
1930 }
1931
1932//------------------------------------------------------------------------------
1933
1942 template<class T = int>
1943 constexpr const ts_t start_of_next_day(ts_t ts, T days = 1) noexcept {
1944 return start_of_day(ts) + days * SEC_PER_DAY;
1945 }
1946
1949 template<class T = int>
1950 constexpr const ts_t next_day_start(ts_t ts, T days = 1) noexcept {
1951 return start_of_next_day(ts, days);
1952 }
1953
1954//------------------------------------------------------------------------------
1955
1964 template<class T = int>
1965 constexpr const ts_ms_t start_of_next_day_ms(ts_ms_t ts_ms, T days = 1) noexcept {
1967 }
1968
1971 template<class T = int>
1972 constexpr const ts_ms_t next_day_start_ms(ts_ms_t ts_ms, T days = 1) noexcept {
1974 }
1975
1976//------------------------------------------------------------------------------
1977
1985 template<class T = int>
1986 constexpr const ts_t next_day(ts_t ts, T days = 1) noexcept {
1987 return ts + days * SEC_PER_DAY;
1988 }
1989
1997 template<class T = int>
1998 constexpr const ts_ms_t next_day_ms(ts_ms_t ts_ms, T days = 1) noexcept {
1999 return ts_ms + days * MS_PER_DAY;
2000 }
2001
2002//------------------------------------------------------------------------------
2003
2010 constexpr const ts_t end_of_day(const ts_t& ts = ts()) noexcept {
2011 return ts - (ts % SEC_PER_DAY) + SEC_PER_DAY - 1;
2012 }
2013
2016 constexpr const ts_t day_end(const ts_t& ts = ts()) noexcept {
2017 return end_of_day(ts);
2018 }
2019
2020//------------------------------------------------------------------------------
2021
2028 constexpr const ts_t end_of_day_sec(ts_ms_t ts_ms = ts_ms()) noexcept {
2029 return end_of_day(ms_to_sec(ts_ms));
2030 }
2031
2034 constexpr const ts_t day_end_sec(ts_ms_t ts_ms = ts_ms()) noexcept {
2035 return end_of_day_sec(ts_ms);
2036 }
2037
2038//------------------------------------------------------------------------------
2039
2046 constexpr const ts_ms_t end_of_day_ms(ts_ms_t ts_ms = ts_ms()) noexcept {
2047 return ts_ms - (ts_ms % MS_PER_DAY) + MS_PER_DAY - 1;
2048 }
2049
2052 constexpr const ts_ms_t day_end_ms(ts_ms_t ts_ms = ts_ms()) noexcept {
2053 return end_of_day_ms(ts_ms);
2054 }
2055
2056//------------------------------------------------------------------------------
2057
2063 template<class T1 = Weekday, class T2 = year_t, class T3 = int>
2064 constexpr const T1 day_of_week_date(T2 year, T3 month, T3 day) {
2065 year_t a, y, m, R;
2066 a = (14 - month) / MONTHS_PER_YEAR;
2067 y = year - a;
2068 m = month + MONTHS_PER_YEAR * a - 2;
2069 R = 7000 + ( day + y + (y / 4) - (y / 100) + (y / 400) + (31 * m) / MONTHS_PER_YEAR);
2070 return static_cast<T1>(R % DAYS_PER_WEEK);
2071 }
2072
2075 template<class T1 = Weekday, class T2 = year_t, class T3 = int>
2076 constexpr const T1 get_weekday(T2 year, T3 month, T3 day) {
2077 return day_of_week_date(year, month, day);
2078 }
2079
2082 template<class T1 = Weekday, class T2 = year_t, class T3 = int>
2083 constexpr const T1 day_of_week(T2 year, T3 month, T3 day) {
2084 return day_of_week_date(year, month, day);
2085 }
2086
2087//------------------------------------------------------------------------------
2088
2097 template<class T1 = Weekday, class T2>
2098 constexpr const T1 get_weekday_from_date(const T2& date) {
2099 return day_of_week_date(date.year, date.mon, date.day);
2100 }
2101
2105 template<class T1 = int, class T2>
2106 constexpr const T1 day_of_week_dt(const T2& date) {
2107 return get_weekday_from_date(date);
2108 }
2109
2113 template<class T1 = int, class T2>
2114 constexpr const T1 day_of_week(const T2& date) {
2115 return get_weekday_from_date(date);
2116 }
2117
2118//------------------------------------------------------------------------------
2119
2123 template<class T = Weekday>
2124 constexpr const T get_weekday_from_ts(ts_t ts) noexcept {
2125 return static_cast<T>((ts / SEC_PER_DAY + THU) % DAYS_PER_WEEK);
2126 }
2127
2130 template<class T = Weekday>
2131 constexpr const T day_of_week(ts_t ts) noexcept {
2132 return get_weekday_from_ts(ts);
2133 }
2134
2135//------------------------------------------------------------------------------
2136
2140 template<class T = Weekday>
2143 }
2144
2147 template<class T = Weekday>
2148 constexpr const T day_of_week_ms(const ts_ms_t& ts_ms) {
2150 }
2151
2152//------------------------------------------------------------------------------
2153
2161 TIME_SHIELD_CONSTEXPR inline const ts_t start_of_month(ts_t ts = ts()) {
2162 return start_of_day(ts) - (day_of_month(ts) - 1) * SEC_PER_DAY;
2163 }
2164
2167 TIME_SHIELD_CONSTEXPR inline const ts_t month_begin(ts_t ts = ts()) {
2168 return start_of_month(ts);
2169 }
2170
2171//------------------------------------------------------------------------------
2172
2180 TIME_SHIELD_CONSTEXPR inline const ts_t end_of_month(ts_t ts = ts()) {
2182 }
2183
2186 TIME_SHIELD_CONSTEXPR inline const ts_t last_day_of_month(ts_t ts = ts()) {
2187 return end_of_month(ts);
2188 }
2189
2190//------------------------------------------------------------------------------
2191
2199 TIME_SHIELD_CONSTEXPR inline const ts_t last_sunday_of_month(ts_t ts = ts()) {
2201 }
2202
2205 TIME_SHIELD_CONSTEXPR inline const ts_t final_sunday_of_month(ts_t ts = ts()) {
2206 return last_sunday_of_month(ts);
2207 }
2208
2209//------------------------------------------------------------------------------
2210
2218 template<class T1 = int, class T2 = year_t, class T3 = int>
2219 TIME_SHIELD_CONSTEXPR inline const T1 last_sunday_month_day(T2 year, T3 month) {
2220 const T1 days = num_days_in_month(year, month);
2221 return days - day_of_week_date(year, month, days);
2222 }
2223
2226 template<class T1 = int, class T2 = year_t, class T3 = int>
2227 TIME_SHIELD_CONSTEXPR inline const T1 final_sunday_month_day(T2 year, T3 month) {
2228 return last_sunday_month_day(year, month);
2229 }
2230
2231//------------------------------------------------------------------------------
2232
2239 constexpr const ts_t start_of_hour(ts_t ts = ts()) noexcept {
2240 return ts - (ts % SEC_PER_HOUR);
2241 }
2242
2245 constexpr const ts_t hour_begin(ts_t ts = ts()) noexcept {
2246 return start_of_hour(ts);
2247 }
2248
2249//------------------------------------------------------------------------------
2250
2257 constexpr const ts_t start_of_hour_sec(ts_ms_t ts_ms = ts_ms()) noexcept {
2258 return start_of_hour(ms_to_sec(ts_ms));
2259 }
2260
2263 constexpr const ts_t hour_begin_sec(ts_ms_t ts_ms = ts_ms()) noexcept {
2264 return start_of_hour_sec(ts_ms);
2265 }
2266
2267//------------------------------------------------------------------------------
2268
2273 constexpr const ts_ms_t start_of_hour_ms(ts_ms_t ts_ms = ts_ms()) noexcept {
2274 return ts_ms - (ts_ms % MS_PER_HOUR);
2275 }
2276
2279 constexpr const ts_ms_t hour_begin_ms(ts_ms_t ts_ms = ts_ms()) noexcept {
2280 return start_of_hour_ms(ts_ms);
2281 }
2282
2283//------------------------------------------------------------------------------
2284
2289 constexpr const ts_t end_of_hour(ts_t ts = ts()) noexcept {
2290 return ts - (ts % SEC_PER_HOUR) + SEC_PER_HOUR - 1;
2291 }
2292
2295 constexpr const ts_t finish_of_hour(ts_t ts = ts()) noexcept {
2296 return end_of_hour(ts);
2297 }
2298
2299//------------------------------------------------------------------------------
2300
2307 constexpr const ts_t end_of_hour_sec(ts_ms_t ts_ms = ts_ms()) noexcept {
2308 return end_of_hour(ms_to_sec(ts_ms));
2309 }
2310
2313 constexpr const ts_t finish_of_hour_sec(ts_ms_t ts_ms = ts_ms()) noexcept {
2314 return end_of_hour_sec(ts_ms);
2315 }
2316
2317//------------------------------------------------------------------------------
2318
2325 constexpr const ts_ms_t end_of_hour_ms(ts_ms_t ts_ms = ts_ms()) noexcept {
2326 return ts_ms - (ts_ms % MS_PER_HOUR) + MS_PER_HOUR - 1;
2327 }
2328
2331 constexpr const ts_ms_t finish_of_hour_ms(ts_ms_t ts_ms = ts_ms()) noexcept {
2332 return end_of_hour_ms(ts_ms);
2333 }
2334
2335//------------------------------------------------------------------------------
2336
2343 template<class T = int>
2344 constexpr const T hour_of_day(ts_t ts = ts()) noexcept {
2345 return ((ts / SEC_PER_HOUR) % HOURS_PER_DAY);
2346 }
2347
2350 template<class T = int>
2351 constexpr const T hour_in_day(ts_t ts = ts()) noexcept {
2352 return hour_of_day(ts);
2353 }
2354
2355//------------------------------------------------------------------------------
2356
2364 constexpr const ts_t start_of_week(ts_t ts = ts()) {
2366 }
2367
2370 constexpr const ts_t week_begin(ts_t ts = ts()) {
2371 return start_of_week(ts);
2372 }
2373
2374//------------------------------------------------------------------------------
2375
2383 constexpr const ts_t end_of_week(ts_t ts = ts()) {
2385 }
2386
2389 constexpr const ts_t finish_of_week(ts_t ts = ts()) {
2390 return end_of_week(ts);
2391 }
2392
2393//------------------------------------------------------------------------------
2394
2402 constexpr const ts_t start_of_saturday(ts_t ts = ts()) {
2403 return start_of_day(ts) + (SAT - day_of_week(ts)) * SEC_PER_DAY;
2404 }
2405
2408 constexpr const ts_t saturday_begin(ts_t ts = ts()) {
2409 return start_of_saturday(ts);
2410 }
2411
2412//------------------------------------------------------------------------------
2413
2417 constexpr const ts_t start_of_min(ts_t ts = ts()) noexcept {
2418 return ts - (ts % SEC_PER_MIN);
2419 }
2420
2423 constexpr const ts_t min_begin(ts_t ts = ts()) noexcept {
2424 return start_of_min(ts);
2425 }
2426
2427//------------------------------------------------------------------------------
2428
2432 constexpr const ts_t end_of_min(ts_t ts = ts()) noexcept {
2433 return ts - (ts % SEC_PER_MIN) + SEC_PER_MIN - 1;
2434 }
2435
2438 constexpr const ts_t finish_of_min(ts_t ts = ts()) noexcept {
2439 return end_of_min(ts);
2440 }
2441
2442//------------------------------------------------------------------------------
2443
2448 template<class T = int>
2449 constexpr const T min_of_day(ts_t ts = ts()) noexcept {
2450 return ((ts / SEC_PER_MIN) % MIN_PER_DAY);
2451 }
2452
2453//------------------------------------------------------------------------------
2454
2459 template<class T = int>
2460 constexpr const T min_of_hour(ts_t ts = ts()) noexcept {
2461 return ((ts / SEC_PER_MIN) % MIN_PER_HOUR);
2462 }
2463
2464//------------------------------------------------------------------------------
2465
2470 template<class T = int>
2471 constexpr const ts_t start_of_period(T p, ts_t ts = ts()) {
2472 return ts - (ts % p);
2473 }
2474
2475//------------------------------------------------------------------------------
2476
2481 template<class T = int>
2482 constexpr const ts_t end_of_period(T p, ts_t ts = ts()) {
2483 return ts - (ts % p) + p - 1;
2484 }
2485
2486//------------------------------------------------------------------------------
2487
2493 template<class T = TimeZoneStruct>
2494 inline const TimeZoneStruct to_time_zone(tz_t offset) {
2495 T tz;
2496 int abs_val = std::abs(offset);
2497 tz.hour = abs_val / SEC_PER_HOUR;
2498 tz.min = abs_val % SEC_PER_MIN;
2499 tz.is_positive = (offset >= 0);
2500 return tz;
2501 }
2502
2504
2505}; // namespace time_shield
2506
2507#endif // _TIME_SHIELD_TIME_CONVERSIONS_HPP_INCLUDED
Header file with enumerations for weekdays, months, and other time-related categories.
const int64_t MONTHS_PER_YEAR
Months per year.
Definition constants.hpp:91
constexpr int64_t SEC_PER_LEAP_YEAR
Seconds per leap year (366 days)
Definition constants.hpp:63
constexpr int64_t HOURS_PER_DAY
Hours per day.
Definition constants.hpp:84
constexpr int64_t SEC_PER_FIRST_100_YEARS
Seconds per first 100 years.
Definition constants.hpp:65
constexpr int64_t MIN_PER_HOUR
Minutes per hour.
Definition constants.hpp:71
constexpr int64_t MIN_PER_DAY
Minutes per day.
Definition constants.hpp:72
constexpr int64_t DAYS_PER_LEAP_YEAR
Days per leap year.
Definition constants.hpp:86
constexpr int64_t MS_PER_DAY
Milliseconds per day.
Definition constants.hpp:50
constexpr int64_t MS_PER_HOUR
Milliseconds per hour.
Definition constants.hpp:49
constexpr int64_t DAYS_PER_YEAR
Days per year.
Definition constants.hpp:87
constexpr int64_t SEC_PER_DAY
Seconds per day.
Definition constants.hpp:60
constexpr int64_t MS_PER_SEC
Milliseconds per second.
Definition constants.hpp:38
constexpr int64_t SEC_PER_MIN
Seconds per minute.
Definition constants.hpp:53
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
Definition constants.hpp:59
constexpr int64_t DAYS_PER_WEEK
Days per week.
Definition constants.hpp:85
constexpr int64_t SEC_PER_4_YEARS
Seconds per 4 years.
Definition constants.hpp:64
constexpr int64_t SEC_PER_100_YEARS
Seconds per 100 years.
Definition constants.hpp:66
constexpr int64_t SEC_PER_400_YEARS
Seconds per 400 years.
Definition constants.hpp:67
constexpr int64_t MAX_YEAR
Maximum representable year.
Definition constants.hpp:99
constexpr int64_t NS_PER_SEC
Nanoseconds per second.
Definition constants.hpp:34
constexpr int64_t UNIX_EPOCH
Start year of UNIX time.
Definition constants.hpp:97
constexpr int64_t SEC_PER_YEAR
Seconds per year (365 days)
Definition constants.hpp:61
constexpr int64_t US_PER_SEC
Microseconds per second.
Definition constants.hpp:37
constexpr const T sec_of_hour(ts_t ts=ts())
Get the second of the hour.
TIME_SHIELD_CONSTEXPR const T year_ms(ts_ms_t ts_ms=ts_ms())
Alias for get_year_ms function.
TIME_SHIELD_CONSTEXPR const T day_of_month(ts_t ts)
Get the day of the month.
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 unix_year(ts_t ts) noexcept
Alias for get_unix_year function.
constexpr const T unixday_ms(ts_ms_t t_ms=ts_ms()) noexcept
Alias for get_unix_day_ms function.
TIME_SHIELD_CONSTEXPR const ts_t to_ts(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0)
Alias for to_timestamp function.
constexpr const T uday_to_ts(uday_t unix_day) noexcept
Alias for unix_day_to_timestamp function.
constexpr const ts_ms_t day_end_ms(ts_ms_t ts_ms=ts_ms()) noexcept
Alias for end_of_day_ms function.
TIME_SHIELD_CONSTEXPR const T to_year_ms(ts_ms_t ts_ms=ts_ms())
Alias for get_year_ms function.
constexpr const T start_of_next_day_from_unix_day_ms(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp representing the start of the next day in milliseconds.
TIME_SHIELD_CONSTEXPR const ts_t ts_from_tm(const std::tm *timeinfo)
Alias for tm_to_timestamp function.
constexpr const ts_t hour_begin(ts_t ts=ts()) noexcept
Alias for start_of_hour function.
TIME_SHIELD_CONSTEXPR const ts_t get_ts(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0)
Alias for to_timestamp function.
constexpr const ts_ms_t next_day_ms(ts_ms_t ts_ms, T days=1) noexcept
Calculate the timestamp for a specified number of days in the future (milliseconds).
constexpr const T unix_day(ts_t ts=ts()) noexcept
Alias for get_unix_day function.
const TimeZoneStruct to_time_zone(tz_t offset)
Converts an integer to a time zone structure.
constexpr const T1 day_of_week(T2 year, T3 month, T3 day)
Alias for day_of_week_date function.
constexpr const ts_t next_day(ts_t ts, T days=1) noexcept
Calculate the timestamp for a specified number of days in the future.
constexpr const ts_t day_start(ts_t ts=ts()) noexcept
Alias for start_of_day function.
constexpr const T next_day_unixday_ms(uday_t unix_day) noexcept
Alias for start_of_next_day_from_unix_day_ms function.
constexpr const T next_day_unixday(uday_t unix_day) noexcept
Alias for start_of_next_day_from_unix_day function.
constexpr const T end_of_day_from_unix_day_ms(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp representing the end of the day in milliseconds.
constexpr const T unixday_to_ts_ms(uday_t unix_day) noexcept
Alias for unix_day_to_timestamp_ms function.
constexpr const ts_t hour_begin_sec(ts_ms_t ts_ms=ts_ms()) noexcept
Alias for start_of_hour_sec function.
TIME_SHIELD_CONSTEXPR const ts_t last_sunday_of_month(ts_t ts=ts())
Get the timestamp of the last Sunday of the current month.
constexpr const T get_unix_day_ms(ts_ms_t t_ms=ts_ms()) noexcept
Get UNIX day from milliseconds timestamp.
constexpr const ts_t end_of_day_sec(ts_ms_t ts_ms=ts_ms()) noexcept
Get the timestamp at the end of the day in seconds.
constexpr const ts_t start_of_week(ts_t ts=ts())
Get the timestamp of the beginning of the week.
constexpr const T1 sec_to_ms(T2 ts) noexcept
Converts a timestamp from seconds to milliseconds.
TIME_SHIELD_CONSTEXPR const ts_t year_begin_date(T year)
Alias for start_of_year_date function.
constexpr const T next_day_from_unix_day_ms(uday_t unix_day) noexcept
Alias for start_of_next_day_from_unix_day_ms function.
constexpr const ts_t start_of_day_sec(ts_ms_t ts_ms=ts_ms()) noexcept
Get the start of the day timestamp in seconds.
constexpr const ts_t start_of_saturday(ts_t ts=ts())
Get the timestamp of the start of Saturday.
constexpr const T day_of_week_ms(const ts_ms_t &ts_ms)
Alias for get_weekday_from_ts_ms function.
constexpr const T next_day_unix_day_ms(uday_t unix_day) noexcept
Alias for start_of_next_day_from_unix_day_ms function.
TIME_SHIELD_CONSTEXPR const ts_t start_of_year_date(T year)
Get the timestamp of the start of the year.
constexpr const ts_ms_t hour_begin_ms(ts_ms_t ts_ms=ts_ms()) noexcept
Alias for start_of_hour_ms function.
constexpr const ts_t finish_of_min(ts_t ts=ts()) noexcept
Alias for end_of_min function.
constexpr const ts_ms_t start_of_day_ms(ts_ms_t ts_ms=ts_ms()) noexcept
Get the start of the day timestamp in milliseconds.
constexpr const ts_ms_t fsec_to_ms(fts_t ts) noexcept
Converts a floating-point timestamp from seconds to milliseconds.
constexpr const ts_t end_of_day(const ts_t &ts=ts()) noexcept
Get the timestamp at the end of the day.
constexpr const T min_of_hour(ts_t ts=ts()) noexcept
Get minute of hour. This function returns a value between 0 to 59.
constexpr const ts_t end_of_min(ts_t ts=ts()) noexcept
Get the timestamp of the end of the minute.
constexpr const T get_unix_day(ts_t ts=ts()) noexcept
Get UNIX day.
constexpr const T next_day_from_unix_day(uday_t unix_day) noexcept
Alias for start_of_next_day_from_unix_day function.
constexpr const T1 num_days_in_month(T2 year, T3 month) noexcept
Get the number of days in a month.
constexpr const ts_t week_begin(ts_t ts=ts())
Alias for start_of_week function.
constexpr const T unix_day_to_ts(uday_t unix_day) noexcept
Alias for unix_day_to_timestamp function.
TIME_SHIELD_CONSTEXPR const ts_t get_timestamp(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0)
Alias for to_timestamp function.
constexpr const ts_t previous_day_start(ts_t ts=ts(), T days=1) noexcept
Alias for start_of_prev_day function.
constexpr const T get_unixday(ts_t ts=ts()) noexcept
Alias for get_unix_day 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 next_day_start(ts_t ts, T days=1) noexcept
Alias for start_of_next_day function.
constexpr const ts_t start_of_day(ts_t ts=ts()) noexcept
Get the start of the day timestamp.
constexpr const T uday_ms(ts_ms_t t_ms=ts_ms()) noexcept
Alias for get_unix_day_ms function.
constexpr const T ms_of_ts(ts_ms_t ts) noexcept
Get the millisecond part of the timestamp.
constexpr const ts_t finish_of_week(ts_t ts=ts())
Alias for end_of_week function.
constexpr const ts_t start_of_prev_day(ts_t ts=ts(), T days=1) noexcept
Get timestamp of the start of the previous day.
TIME_SHIELD_CONSTEXPR const ts_ms_t start_of_year_date_ms(T year)
Get the timestamp in milliseconds of the start of the year.
constexpr const T get_days_difference(ts_t start, ts_t stop) noexcept
Get the number of days between two timestamps.
TIME_SHIELD_CONSTEXPR const ts_ms_t year_start_date_ms(T year)
Alias for start_of_year_date_ms function.
TIME_SHIELD_CONSTEXPR const T month_of_year(ts_t ts) noexcept
Get the month of the year.
constexpr const ts_t finish_of_hour_sec(ts_ms_t ts_ms=ts_ms()) noexcept
Alias for end_of_hour_sec function.
constexpr const ts_t end_of_hour(ts_t ts=ts()) noexcept
Get the timestamp at the end of the hour. This function sets the minute and second to 59.
constexpr const T end_of_day_from_unix_day(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp representing the end of the day in seconds.
TIME_SHIELD_CONSTEXPR const T1 last_sunday_month_day(T2 year, T3 month)
Get the day of the last Sunday of the given month and year.
constexpr const T unixday_to_ts(uday_t unix_day) noexcept
Alias for unix_day_to_timestamp function.
TIME_SHIELD_CONSTEXPR const T to_year(ts_t ts=ts())
Alias for get_year function.
constexpr const T get_unix_year(ts_t ts) noexcept
Converts a UNIX timestamp to a year.
constexpr const T unix_min(ts_t ts=ts())
Alias for get_unix_min function.
constexpr const T days_in_year_ts(ts_t ts=ts())
Alias for num_days_in_year_ts function.
TIME_SHIELD_CONSTEXPR const ts_t month_begin(ts_t ts=ts())
Alias for start_of_month function.
constexpr const T to_unix_year(ts_t ts) noexcept
Alias for get_unix_year function.
TIME_SHIELD_CONSTEXPR const ts_t year_start_ms(ts_t ts_ms=ts_ms())
Alias for start_of_year_ms function.
constexpr const T get_weekday_from_ts(ts_t ts) noexcept
Get the weekday from a timestamp.
constexpr const ts_t start_of_period(T p, ts_t ts=ts())
Get the timestamp of the start of the period.
TIME_SHIELD_CONSTEXPR const ts_t to_timestamp(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0)
Converts a date and time to a timestamp.
constexpr const T unix_day_ms(ts_ms_t t_ms=ts_ms()) noexcept
Alias for get_unix_day_ms function.
constexpr const T uday(ts_t ts=ts()) noexcept
Alias for get_unix_day function.
TIME_SHIELD_CONSTEXPR const ts_ms_t year_begin_date_ms(T year)
Alias for start_of_year_date_ms function.
TIME_SHIELD_CONSTEXPR const T hour24_to_12(T hour) noexcept
Converts a 24-hour format hour to a 12-hour format.
constexpr const T num_days_in_year_ts(ts_t ts=ts())
Get the number of days in the current year.
TIME_SHIELD_CONSTEXPR const ts_t year_start_date(T year)
Alias for start_of_year_date function.
constexpr const T1 days_in_year(T2 year) noexcept
Alias for num_days_in_year function.
constexpr const T unix_day_to_ts_ms(uday_t unix_day) noexcept
Alias for unix_day_to_timestamp_ms function.
TIME_SHIELD_CONSTEXPR const fts_t to_ftimestamp(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0, T3 ms=0)
Converts a date and time to a floating-point timestamp.
TIME_SHIELD_CONSTEXPR const ts_t year_end(ts_t ts=ts())
Alias for end_of_year function.
constexpr const T start_of_day_from_unix_day_ms(uday_t unix_day) noexcept
Alias for unix_day_to_timestamp_ms function.
TIME_SHIELD_CONSTEXPR const ts_t start_of_month(ts_t ts=ts())
Get the timestamp at the start of the current month.
constexpr const ts_t saturday_begin(ts_t ts=ts())
Alias for start_of_saturday function.
constexpr const T unix_day_to_timestamp(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp in seconds.
constexpr const ts_t start_of_next_day(ts_t ts, T days=1) noexcept
Get the timestamp of the start of the day after a specified number of days.
constexpr const ts_t end_of_period(T p, ts_t ts=ts())
Get the timestamp of the end of the period.
constexpr const T get_weekday_from_ts_ms(ts_ms_t ts_ms)
Get the weekday from a timestamp in milliseconds.
constexpr const T umin(ts_t ts=ts())
Alias for get_unix_min function.
TIME_SHIELD_CONSTEXPR const ts_t tm_to_timestamp(const std::tm *timeinfo)
Converts a std::tm structure to a timestamp.
constexpr const T unix_day_to_timestamp_ms(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp in milliseconds.
TIME_SHIELD_CONSTEXPR const T1 final_sunday_month_day(T2 year, T3 month)
Alias for last_sunday_month_day function.
constexpr const ts_ms_t finish_of_hour_ms(ts_ms_t ts_ms=ts_ms()) noexcept
Alias for end_of_hour_ms function.
constexpr const ts_ms_t start_of_next_day_ms(ts_ms_t ts_ms, T days=1) noexcept
Get the timestamp of the start of the day after a specified number of days.
constexpr const T sec_of_day(ts_t ts=ts()) noexcept
Get the second of the day.
constexpr const ts_ms_t end_of_hour_ms(ts_ms_t ts_ms=ts_ms()) noexcept
Get the timestamp at the end of the hour.
TIME_SHIELD_CONSTEXPR const T1 num_days_in_month_ts(ts_t ts=ts()) noexcept
Get the number of days in the month of the given timestamp.
constexpr const ts_t end_of_hour_sec(ts_ms_t ts_ms=ts_ms()) noexcept
Get the timestamp at the end of the hour.
TIME_SHIELD_CONSTEXPR const T get_year_ms(ts_ms_t ts_ms=ts_ms())
Get the year from the timestamp in milliseconds.
TIME_SHIELD_CONSTEXPR const ts_ms_t year_end_ms(ts_ms_t ts_ms=ts_ms())
Alias for end_of_year_ms function.
constexpr const ts_ms_t start_of_hour_ms(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 finish_of_hour(ts_t ts=ts()) noexcept
Alias for end_of_hour function.
TIME_SHIELD_CONSTEXPR const ts_ms_t to_ts_ms(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0, T2 ms=0)
Alias for to_timestamp_ms function.
constexpr const T next_day_unix_day(uday_t unix_day) noexcept
Alias for start_of_next_day_from_unix_day function.
TIME_SHIELD_CONSTEXPR const ts_ms_t end_of_year_ms(ts_ms_t ts_ms=ts_ms())
Get the timestamp in milliseconds of the end of the year.
TIME_SHIELD_CONSTEXPR const ts_t year_start(ts_t ts=ts())
Alias for start_of_year function.
constexpr const T start_of_next_day_from_unix_day(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp representing the start of the next day in seconds.
TIME_SHIELD_CONSTEXPR const ts_t final_sunday_of_month(ts_t ts=ts())
Alias for last_sunday_of_month function.
TIME_SHIELD_CONSTEXPR const T get_year(ts_t ts=ts())
Get the year from the timestamp.
constexpr const T1 num_days_in_year(T2 year) noexcept
Get the number of days in a given year.
constexpr const ts_t day_end_sec(ts_ms_t ts_ms=ts_ms()) noexcept
Alias for end_of_day_sec function.
TIME_SHIELD_CONSTEXPR ts_t end_of_year(ts_t ts=ts())
Get the end-of-year timestamp.
constexpr const T unixday(ts_t ts=ts()) noexcept
Alias for get_unix_day function.
TIME_SHIELD_CONSTEXPR const fts_t to_fts(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0, T3 ms=0)
Alias for to_ftimestamp function.
constexpr const ts_t day_start_sec(ts_ms_t ts_ms=ts_ms()) noexcept
Alias for start_of_day_sec function.
TIME_SHIELD_CONSTEXPR const ts_ms_t start_of_year_ms(ts_ms_t ts_ms=ts_ms()) noexcept
Get the start of the year timestamp in milliseconds.
constexpr const ts_t start_of_hour_sec(ts_ms_t ts_ms=ts_ms()) noexcept
Get the timestamp at the start of the hour.
constexpr const ts_t day_end(const ts_t &ts=ts()) noexcept
Alias for end_of_day function.
TIME_SHIELD_CONSTEXPR const T year(ts_t ts=ts())
Alias for get_year function.
constexpr const T to_unix_min(ts_t ts=ts())
Alias for get_unix_min function.
constexpr const T get_unix_min(ts_t ts=ts())
Get UNIX minute.
constexpr const T start_of_day_from_unix_day(uday_t unix_day) noexcept
Alias for unix_day_to_timestamp function.
TIME_SHIELD_CONSTEXPR const ts_t last_day_of_month(ts_t ts=ts())
Alias for end_of_month function.
constexpr const T days(ts_t start, ts_t stop) noexcept
Alias for get_days_difference function.
TIME_SHIELD_CONSTEXPR const ts_t start_of_year(ts_t ts) noexcept
Get the start of the year timestamp.
constexpr const ts_ms_t day_start_ms(ts_ms_t ts_ms=ts_ms()) noexcept
Alias for start_of_day_ms function.
constexpr const ts_t start_of_min(ts_t ts=ts()) noexcept
Get the timestamp of the beginning of the minute.
constexpr const ts_ms_t sec_to_ms_impl(T t, std::true_type) noexcept
TIME_SHIELD_CONSTEXPR const T h24_to_h12(T hour) noexcept
Alias for hour24_to_12 function.
constexpr const T uday_to_ts_ms(uday_t unix_day) noexcept
Alias for unix_day_to_timestamp_ms function.
constexpr const T sec_of_day_ms(ts_ms_t ts_ms) noexcept
Get the second of the day from milliseconds timestamp.
constexpr const T1 get_weekday(T2 year, T3 month, T3 day)
Alias for day_of_week_date function.
constexpr const T1 days_in_month(T2 year, T3 month) noexcept
Alias for num_days_in_month function.
constexpr const ts_t start_of_hour(ts_t ts=ts()) noexcept
Get the timestamp at the start of the hour.
TIME_SHIELD_CONSTEXPR const ts_ms_t to_timestamp_ms(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0, T2 ms=0)
Converts a date and time to a timestamp in milliseconds.
constexpr const ts_t end_of_week(ts_t ts=ts())
Get the timestamp of the end of the week.
TIME_SHIELD_CONSTEXPR const ts_t year_begin_ms(ts_t ts_ms=ts_ms())
Alias for start_of_year_ms function.
TIME_SHIELD_CONSTEXPR const ts_t year_begin(ts_t ts=ts())
Alias for start_of_year function.
constexpr const T sec_of_min(ts_t ts=ts())
Get the second of the minute.
constexpr const T get_days(ts_t start, ts_t stop) noexcept
Alias for get_days_difference function.
constexpr const ts_ms_t end_of_day_ms(ts_ms_t ts_ms=ts_ms()) noexcept
Get the timestamp at the end of the day in milliseconds.
constexpr const T get_unixday_ms(ts_ms_t t_ms=ts_ms()) noexcept
Alias for get_unix_day_ms function.
constexpr const T min_of_day(ts_t ts=ts()) noexcept
Get minute of day. This function returns a value between 0 to 1439 (minute of day).
constexpr const ts_t min_begin(ts_t ts=ts()) noexcept
Alias for start_of_min function.
constexpr const fts_t ms_to_fsec(T ts_ms) noexcept
Converts a timestamp from milliseconds to floating-point seconds.
constexpr const T1 ms_to_sec(T2 ts_ms) noexcept
Converts a timestamp from milliseconds to seconds.
constexpr const T hour_of_day(ts_t ts=ts()) noexcept
Get the hour of the day.
const T day_of_year(ts_t ts=ts())
Get the day of the year.
constexpr const T1 day_of_week_date(T2 year, T3 month, T3 day)
Get the day of the week.
constexpr const T hour_in_day(ts_t ts=ts()) noexcept
Alias for hour_of_day function.
constexpr const ts_ms_t next_day_start_ms(ts_ms_t ts_ms, T days=1) noexcept
Alias for start_of_next_day_ms function.
TIME_SHIELD_CONSTEXPR const ts_t end_of_month(ts_t ts=ts())
Get the last timestamp of the current month.
@ FEB
February.
Definition enums.hpp:105
@ SAT
Saturday.
Definition enums.hpp:47
@ THU
Thursday.
Definition enums.hpp:45
TIME_SHIELD_CONSTEXPR const fts_t dt_to_ftimestamp(const T &date_time)
Converts a date-time structure to a floating-point timestamp.
T to_date_time_ms(ts_ms_t ts)
Converts a timestamp in milliseconds to a date-time structure with milliseconds.
constexpr const T1 get_weekday_from_date(const T2 &date)
Get the day of the week from a date structure.
TIME_SHIELD_CONSTEXPR const ts_t dt_to_timestamp(const T &date_time)
Converts a date-time structure to a timestamp.
T to_dt_ms(ts_ms_t ts)
Alias for to_date_time_ms function.
constexpr const T1 day_of_week_dt(const T2 &date)
Alias for get_weekday_from_date function that accepts a date structure.
T1 to_date_time(T2 ts)
Converts a timestamp to a date-time structure.
TIME_SHIELD_CONSTEXPR const ts_t dt_to_timestamp_ms(const T &date_time)
Converts a date-time structure to a timestamp in milliseconds.
T1 to_dt(T2 ts)
Alias for to_date_time function.
int64_t ts_t
Type for representing timestamps in seconds.
Definition types.hpp:33
int tz_t
Type for representing time zone offsets in minutes.
Definition types.hpp:41
int64_t ts_ms_t
Type for representing timestamps in milliseconds.
Definition types.hpp:34
int64_t uday_t
Type for representing Unix days as integers (days since January 1, 1970).
Definition types.hpp:32
double fts_t
Type for representing timestamps as floating-point numbers (e.g., fractional seconds).
Definition types.hpp:36
int64_t year_t
Type for representing years as integers.
Definition types.hpp:31
const ts_ms_t timestamp_ms() noexcept
Get the current UTC timestamp in milliseconds.
const T us_of_sec() noexcept
Get the microsecond part of the current second.
const ts_t timestamp() noexcept
Get the current UTC timestamp in seconds.
const ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
const T ns_of_sec() noexcept
Get the nanosecond part of the current second.
const fts_t fts() noexcept
Get the current UTC timestamp in floating-point seconds.
const fts_t ftimestamp() noexcept
Get the current UTC timestamp in floating-point seconds.
const T ms_of_sec() noexcept
Get the millisecond part of the current second.
const ts_t ts() noexcept
Get the current UTC timestamp in seconds.
constexpr const bool is_leap_year_date(T year) noexcept
Checks if the given year is a leap year.
TIME_SHIELD_CONSTEXPR const bool is_valid_date_time(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0, T3 ms=0) noexcept
Checks the correctness of a date and time.
TIME_SHIELD_CONSTEXPR const bool is_leap_year(ts_t ts)
Alias for is_leap_year_ts function.
TIME_SHIELD_CONSTEXPR const bool is_leap_year_ts(ts_t ts)
Checks if the given year is a leap year.
Main namespace for the Time Shield library.
Definition constants.hpp:12
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.