Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_conversions.mqh
Go to the documentation of this file.
1//+------------------------------------------------------------------+
2//| time_conversions.mqh |
3//| Time Shield - MQL5 Time Conversions |
4//| Copyright 2025, NewYaroslav |
5//| https://github.com/NewYaroslav/time-shield-cpp |
6//+------------------------------------------------------------------+
7#ifndef __TIME_SHIELD_TIME_CONVERSIONS_MQH__
8#define __TIME_SHIELD_TIME_CONVERSIONS_MQH__
9
18
19#property copyright "Copyright 2025, NewYaroslav"
20#property link "https://github.com/NewYaroslav/time-shield-cpp"
21#property strict
22
23#include "constants.mqh"
24#include "date_time_struct.mqh"
25#include "enums.mqh"
26#include "validation.mqh"
27#include "time_zone_struct.mqh"
28
29namespace time_shield {
30
34
38 int ns_of_sec(double ts) {
39 double frac = ts - MathFloor(ts);
40 return (int)MathRound(frac * NS_PER_SEC);
41 }
42
46 int us_of_sec(double ts) {
47 double frac = ts - MathFloor(ts);
48 return (int)MathRound(frac * US_PER_SEC);
49 }
50
54 int ms_of_sec(double ts) {
55 double frac = ts - MathFloor(ts);
56 return (int)MathRound(frac * MS_PER_SEC);
57 }
58
62 int ms_of_ts(long ts_ms) {
63 return (int)(ts_ms % MS_PER_SEC);
64 }
65
66 //----------------------------------------------------------------------
67 // Seconds <-> Milliseconds
68 //----------------------------------------------------------------------
69
73 long sec_to_ms(double sec) {
74 return (long)MathRound(sec * MS_PER_SEC);
75 }
76
80 long ms_to_sec(long ms) {
81 return ms / MS_PER_SEC;
82 }
83
87 double ms_to_fsec(long ms) {
88 return (double)ms / (double)MS_PER_SEC;
89 }
90
91 //----------------------------------------------------------------------
92 // Minutes <-> Milliseconds / Seconds
93 //----------------------------------------------------------------------
94
98 long min_to_ms(double min) {
99 return (long)MathRound(min * MS_PER_MIN);
100 }
101
105 int ms_to_min(long ms) {
106 return (int)(ms / MS_PER_MIN);
107 }
108
112 long min_to_sec(double min) {
113 return (long)MathRound(min * SEC_PER_MIN);
114 }
115
119 int sec_to_min(long sec) {
120 return (int)(sec / SEC_PER_MIN);
121 }
122
126 double min_to_fsec(double min) {
127 return min * SEC_PER_MIN;
128 }
129
133 double sec_to_fmin(long sec) {
134 return (double)sec / (double)SEC_PER_MIN;
135 }
136
137 //----------------------------------------------------------------------
138 // Hours <-> Milliseconds / Seconds
139 //----------------------------------------------------------------------
140
144 long hour_to_ms(double hr) {
145 return (long)MathRound(hr * MS_PER_HOUR);
146 }
147
151 int ms_to_hour(long ms) {
152 return (int)(ms / MS_PER_HOUR);
153 }
154
158 long hour_to_sec(double hr) {
159 return (long)MathRound(hr * SEC_PER_HOUR);
160 }
161
165 int sec_to_hour(long sec) {
166 return (int)(sec / SEC_PER_HOUR);
167 }
168
172 double hour_to_fsec(double hr) {
173 return hr * SEC_PER_HOUR;
174 }
175
179double sec_to_fhour(long sec) {
180 return (double)sec / (double)SEC_PER_HOUR;
181 }
182
186 int hour24_to_12(int hour) {
187 if(hour == 0 || hour > 12) return 12;
188 return hour;
189 }
190
193 int h24_to_h12(int hour) { return hour24_to_12(hour); }
194
195 //----------------------------------------------------------------------
196 // Timestamp <-> Year
197 //----------------------------------------------------------------------
198
202 long get_unix_year(long ts) {
203 const long BIAS_292277022000 = 9223371890843040000;
204 const long BIAS_2000 = 946684800;
205
206 long y = MAX_YEAR;
207 long secs = -((ts - BIAS_2000) - BIAS_292277022000);
208
209 long 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 long 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 long 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 long n_1_years = secs / SEC_PER_YEAR;
222 secs -= n_1_years * SEC_PER_YEAR;
223 y -= n_1_years;
224
225 y = secs == 0 ? y : y - 1;
226 return y - UNIX_EPOCH;
227 }
228
231 long unix_year(long ts) { return get_unix_year(ts); }
232
235 long to_unix_year(long ts) { return get_unix_year(ts); }
236
240 long get_year(long ts) {
241 return get_unix_year(ts) + UNIX_EPOCH;
242 }
243
246 long year(long ts) { return get_year(ts); }
247
250 long to_year(long ts) { return get_year(ts); }
251
255 long get_year_ms(long ts_ms) {
256 return get_year(ms_to_sec(ts_ms));
257 }
258
261 long year_ms(long ts_ms) { return get_year_ms(ts_ms); }
262
265 long to_year_ms(long ts_ms) { return get_year_ms(ts_ms); }
266
267 //----------------------------------------------------------------------
268 // DateTime conversions
269 //----------------------------------------------------------------------
270
275 MqlDateTime tmp;
276 TimeToStruct((datetime)ts, tmp);
278 dt.year = tmp.year;
279 dt.mon = tmp.mon;
280 dt.day = tmp.day;
281 dt.hour = tmp.hour;
282 dt.min = tmp.min;
283 dt.sec = tmp.sec;
284 dt.ms = 0;
285 return dt;
286 }
287
291
295 MqlDateTime to_date_time_mql(long ts) {
296 MqlDateTime dt;
297 TimeToStruct((datetime)ts, dt);
298 return dt;
299 }
300
303 MqlDateTime to_mql_dt(long ts) { return to_date_time_mql(ts); }
304
310 dt.ms = ms_of_ts(ts_ms);
311 return dt;
312 }
313
317
322 MqlDateTime tmp;
323 tmp.year = (int)dt.year;
324 tmp.mon = dt.mon;
325 tmp.day = dt.day;
326 tmp.hour = dt.hour;
327 tmp.min = dt.min;
328 tmp.sec = dt.sec;
329 return (long)StructToTime(tmp);
330 }
331
335 long dt_to_timestamp(const MqlDateTime &dt) {
336 return (long)StructToTime(dt);
337 }
338
341 long to_timestamp(const DateTimeStruct &dt) { return dt_to_timestamp(dt); }
342
345 long to_timestamp(const MqlDateTime &dt) { return dt_to_timestamp(dt); }
346
355 long to_timestamp(long year, int mon, int day, int hour=0, int min=0, int sec=0) {
356 MqlDateTime dt;
357 dt.year = (int)year;
358 dt.mon = mon;
359 dt.day = day;
360 dt.hour = hour;
361 dt.min = min;
362 dt.sec = sec;
363 return (long)StructToTime(dt);
364 }
365
368 long to_ts(long year, int mon, int day, int hour=0, int min=0, int sec=0) {
369 return to_timestamp(year, mon, day, hour, min, sec);
370 }
371
381 long to_timestamp_ms(long year, int mon, int day, int hour=0, int min=0, int sec=0, int ms=0) {
382 return sec_to_ms(to_timestamp(year, mon, day, hour, min, sec)) + ms;
383 }
384
387 long to_ts_ms(long year, int mon, int day, int hour=0, int min=0, int sec=0, int ms=0) {
388 return to_timestamp_ms(year, mon, day, hour, min, sec, ms);
389 }
390
393 long ts_ms(long year, int mon, int day, int hour=0, int min=0, int sec=0, int ms=0) {
394 return to_timestamp_ms(year, mon, day, hour, min, sec, ms);
395 }
396
401 return sec_to_ms(dt_to_timestamp(dt)) + dt.ms;
402 }
403
407 long dt_to_timestamp_ms(const MqlDateTime &dt) {
408 return sec_to_ms(dt_to_timestamp(dt));
409 }
410
413 long to_timestamp_ms(const DateTimeStruct &dt) { return dt_to_timestamp_ms(dt); }
414
417 long to_timestamp_ms(const MqlDateTime &dt) { return dt_to_timestamp_ms(dt); }
418
423 return (double)dt_to_timestamp(dt) + (double)dt.ms / (double)MS_PER_SEC;
424 }
425
429 double dt_to_ftimestamp(const MqlDateTime &dt) {
430 return (double)dt_to_timestamp(dt);
431 }
432
435 double to_ftimestamp(const DateTimeStruct &dt) { return dt_to_ftimestamp(dt); }
436
439 double to_ftimestamp(const MqlDateTime &dt) { return dt_to_ftimestamp(dt); }
440
441 //----------------------------------------------------------------------
442 // Start/End of intervals
443 //----------------------------------------------------------------------
444
448 long start_of_day(long ts) {
449 return ts - (ts % SEC_PER_DAY);
450 }
451
454 long day_start(long ts) { return start_of_day(ts); }
455
460 long start_of_prev_day(long ts, int days=1) {
461 return start_of_day(ts) - days * SEC_PER_DAY;
462 }
463
466 long previous_day_start(long ts, int days=1) { return start_of_prev_day(ts, days); }
467
473 }
474
478
483 return ts_ms - (ts_ms % MS_PER_DAY);
484 }
485
488 long day_start_ms(long ts_ms) { return start_of_day_ms(ts_ms); }
489
494 long start_of_next_day(long ts, int days=1) {
495 return start_of_day(ts) + days * SEC_PER_DAY;
496 }
497
500 long next_day_start(long ts, int days=1) { return start_of_next_day(ts, days); }
501
506 long start_of_next_day_ms(long ts_ms, int days=1) {
508 }
509
513
518 long next_day(long ts, int days=1) {
519 return ts + days * SEC_PER_DAY;
520 }
521
526 long next_day_ms(long ts_ms, int days=1) {
527 return ts_ms + days * MS_PER_DAY;
528 }
529
533 long end_of_day(long ts) {
534 return ts - (ts % SEC_PER_DAY) + SEC_PER_DAY - 1;
535 }
536
539 long day_end(long ts) { return end_of_day(ts); }
540
545 return end_of_day(ms_to_sec(ts_ms));
546 }
547
550 long day_end_sec(long ts_ms) { return end_of_day_sec(ts_ms); }
551
555 long end_of_day_ms(long ts_ms) {
556 return ts_ms - (ts_ms % MS_PER_DAY) + MS_PER_DAY - 1;
557 }
558
561 long day_end_ms(long ts_ms) { return end_of_day_ms(ts_ms); }
562
568 Weekday day_of_week_date(long year, int month, int day) {
569 long a = (14 - month) / MONTHS_PER_YEAR;
570 long y = year - a;
571 long m = month + MONTHS_PER_YEAR * a - 2;
572 long r = 7000 + day + y + (y / 4) - (y / 100) + (y / 400) + (31 * m) / MONTHS_PER_YEAR;
573 return (Weekday)(r % DAYS_PER_WEEK);
574 }
575
578 Weekday get_weekday(long year, int month, int day) { return day_of_week_date(year, month, day); }
579
582 Weekday day_of_week(long year, int month, int day) { return day_of_week_date(year, month, day); }
583
588 return day_of_week_date(dt.year, dt.mon, dt.day);
589 }
590
594 Weekday get_weekday_from_date(const MqlDateTime &dt) {
595 return day_of_week_date(dt.year, dt.mon, dt.day);
596 }
597
601
605
608 Weekday day_of_week_dt(const MqlDateTime &dt) { return get_weekday_from_date(dt); }
609
612 Weekday day_of_week(const MqlDateTime &dt) { return get_weekday_from_date(dt); }
613
618 return (Weekday)((ts / SEC_PER_DAY + THU) % DAYS_PER_WEEK);
619 }
620
624
631
635
639 long start_of_year(long ts) {
640 MqlDateTime dt; TimeToStruct((datetime)ts, dt);
641 dt.mon = 1; dt.day = 1; dt.hour = 0; dt.min = 0; dt.sec = 0;
642 return (long)StructToTime(dt);
643 }
644
648 long end_of_year(long ts) {
649 MqlDateTime dt; TimeToStruct((datetime)ts, dt);
650 dt.year++; dt.mon = 1; dt.day = 1; dt.hour = 0; dt.min = 0; dt.sec = 0;
651 return (long)StructToTime(dt) - 1;
652 }
653
656 long year_start(long ts) { return start_of_year(ts); }
657
660 long year_begin(long ts) { return start_of_year(ts); }
661
667 }
668
672
676
681 if (year < 2100) {
682 long year_diff = year >= UNIX_EPOCH ? year - UNIX_EPOCH : UNIX_EPOCH - year;
683 long year_start_ts = (year_diff / 4) * SEC_PER_4_YEARS;
684 int year_remainder = (int)(year_diff % 4);
685 long SEC_PER_YEAR_X2 = 2 * SEC_PER_YEAR;
686 long SEC_PER_YEAR_V2 = SEC_PER_YEAR_X2 + SEC_PER_LEAP_YEAR;
687 switch (year_remainder) {
688 case 0: return year_start_ts;
689 case 1: return year_start_ts + SEC_PER_YEAR;
690 case 2: return year_start_ts + SEC_PER_YEAR_X2;
691 default: return year_start_ts + SEC_PER_YEAR_V2;
692 }
693 return year_start_ts + SEC_PER_YEAR_V2;
694 }
695 return to_timestamp(year, 1, 1);
696 }
697
701
705
712
716
720
726 }
727
730 long year_end_ms(long ts_ms) { return end_of_year_ms(ts_ms); }
731
735 int day_of_year(long ts) {
736 return (int)((ts - start_of_year(ts)) / SEC_PER_DAY) + 1;
737 }
738
743 const int JAN_AND_FEB_DAY_LEAP_YEAR = 60;
744 static const int TABLE_MONTH_OF_YEAR[] = {
745 0,
746 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,
747 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,
748 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,
749 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,
750 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,
751 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,
752 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,
753 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,
754 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,
755 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,
756 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,
757 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,
758 };
759 int dy = day_of_year(ts);
760 if(is_leap_year(ts) && dy >= JAN_AND_FEB_DAY_LEAP_YEAR)
761 return (Month)TABLE_MONTH_OF_YEAR[dy - 1];
762 return (Month)TABLE_MONTH_OF_YEAR[dy];
763 }
764
768 int day_of_month(long ts) {
769 const int JAN_AND_FEB_DAY_LEAP_YEAR = 60;
770 static const int TABLE_DAY_OF_YEAR[] = {
771 0,
772 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,
773 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,
774 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,
775 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,
776 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,
777 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,
778 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,
779 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,
780 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,
781 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,
782 };
783 int dy = day_of_year(ts);
784 if(is_leap_year(ts)) {
785 if(dy == JAN_AND_FEB_DAY_LEAP_YEAR) return TABLE_DAY_OF_YEAR[dy - 1] + 1;
786 if(dy > JAN_AND_FEB_DAY_LEAP_YEAR) return TABLE_DAY_OF_YEAR[dy - 1];
787 }
788 return TABLE_DAY_OF_YEAR[dy];
789 }
790
795 int num_days_in_month(long year, int month) {
796 if(month > MONTHS_PER_YEAR || month < 0) return 0;
797 static const int num_days[13] = {0,31,30,31,30,31,30,31,31,30,31,30,31};
798 if(month == FEB) {
799 if(is_leap_year_date(year)) return 29;
800 return 28;
801 }
802 return num_days[month];
803 }
804
807 int days_in_month(long year, int month) { return num_days_in_month(year, month); }
808
813 static const int num_days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
814 int month = month_of_year(ts);
815 if(month == FEB) {
816 return is_leap_year(ts) ? 29 : 28;
817 }
818 return num_days[month];
819 }
820
824
828
829 //----------------------------------------------------------------------
830 // Workday boundary helpers
831 //----------------------------------------------------------------------
832
833 int first_workday_day(long year, int month) {
834 int days = num_days_in_month(year, month);
835 if(days <= 0) return 0;
836 for(int day = 1; day <= days; ++day) {
837 if(is_workday(year, month, day)) return day;
838 }
839 return 0;
840 }
841
842 int last_workday_day(long year, int month) {
843 int days = num_days_in_month(year, month);
844 if(days <= 0) return 0;
845 for(int day = days; day >= 1; --day) {
846 if(is_workday(year, month, day)) return day;
847 }
848 return 0;
849 }
850
851 int count_workdays_in_month(long year, int month) {
852 int days = num_days_in_month(year, month);
853 if(days <= 0) return 0;
854 int total = 0;
855 for(int day = 1; day <= days; ++day) {
856 if(is_workday(year, month, day)) ++total;
857 }
858 return total;
859 }
860
865 long start_of_first_workday_month(long year, int month) {
866 int day = first_workday_day(year, month);
867 if(day <= 0) return ERROR_TIMESTAMP;
868 return to_timestamp(year, month, day);
869 }
870
875 long start_of_first_workday_month_ms(long year, int month) {
876 int day = first_workday_day(year, month);
877 if(day <= 0) return ERROR_TIMESTAMP;
878 return sec_to_ms(to_timestamp(year, month, day));
879 }
880
887
894
899 long end_of_first_workday_month(long year, int month) {
900 int day = first_workday_day(year, month);
901 if(day <= 0) return ERROR_TIMESTAMP;
902 return end_of_day(to_timestamp(year, month, day));
903 }
904
909 long end_of_first_workday_month_ms(long year, int month) {
910 int day = first_workday_day(year, month);
911 if(day <= 0) return ERROR_TIMESTAMP;
912 return end_of_day_ms(sec_to_ms(to_timestamp(year, month, day)));
913 }
914
921
928
933 long start_of_last_workday_month(long year, int month) {
934 int day = last_workday_day(year, month);
935 if(day <= 0) return ERROR_TIMESTAMP;
936 return to_timestamp(year, month, day);
937 }
938
943 long start_of_last_workday_month_ms(long year, int month) {
944 int day = last_workday_day(year, month);
945 if(day <= 0) return ERROR_TIMESTAMP;
946 return sec_to_ms(to_timestamp(year, month, day));
947 }
948
955
962
967 long end_of_last_workday_month(long year, int month) {
968 int day = last_workday_day(year, month);
969 if(day <= 0) return ERROR_TIMESTAMP;
970 return end_of_day(to_timestamp(year, month, day));
971 }
972
977 long end_of_last_workday_month_ms(long year, int month) {
978 int day = last_workday_day(year, month);
979 if(day <= 0) return ERROR_TIMESTAMP;
980 return end_of_day_ms(sec_to_ms(to_timestamp(year, month, day)));
981 }
982
989
996
997 int workday_index_in_month(long year, int month, int day) {
998 if(!is_workday(year, month, day)) return 0;
999 int days = num_days_in_month(year, month);
1000 if(days <= 0) return 0;
1001 int index = 0;
1002 for(int current = 1; current <= days; ++current) {
1003 if(is_workday(year, month, current)) {
1004 ++index;
1005 if(current == day) return index;
1006 }
1007 }
1008 return 0;
1009 }
1010
1011 //----------------------------------------------------------------------
1012 // Workday boundary predicates
1013 //----------------------------------------------------------------------
1014
1015 bool is_first_workday_of_month(long year, int month, int day) {
1016 return is_workday(year, month, day) && first_workday_day(year, month) == day;
1017 }
1018
1019 bool is_within_first_workdays_of_month(long year, int month, int day, int count) {
1020 if(count <= 0) return false;
1021 int total = count_workdays_in_month(year, month);
1022 if(count > total) return false;
1023 int index = workday_index_in_month(year, month, day);
1024 return index > 0 && index <= count;
1025 }
1026
1027 bool is_last_workday_of_month(long year, int month, int day) {
1028 return is_workday(year, month, day) && last_workday_day(year, month) == day;
1029 }
1030
1031 bool is_within_last_workdays_of_month(long year, int month, int day, int count) {
1032 if(count <= 0) return false;
1033 int total = count_workdays_in_month(year, month);
1034 if(count > total) return false;
1035 int index = workday_index_in_month(year, month, day);
1036 return index > 0 && index >= (total - count + 1);
1037 }
1038
1042
1043 bool is_within_first_workdays_of_month(const long ts, int count) {
1045 }
1046
1050
1051 bool is_within_last_workdays_of_month(const long ts, int count) {
1053 }
1054
1058
1059 bool is_within_first_workdays_of_month_ms(const long ts_ms, int count) {
1061 }
1062
1066
1067 bool is_within_last_workdays_of_month_ms(const long ts_ms, int count) {
1069 }
1070
1075 if (is_leap_year_date(year)) return (int)DAYS_PER_LEAP_YEAR;
1076 return (int)DAYS_PER_YEAR;
1077 }
1078
1082
1087 if(is_leap_year_ts(ts)) return (int)DAYS_PER_LEAP_YEAR;
1088 return (int)DAYS_PER_YEAR;
1089 }
1090
1094
1098 long start_of_month(long ts) {
1099 return start_of_day(ts) - (day_of_month(ts) - 1) * SEC_PER_DAY;
1100 }
1101
1104 long month_begin(long ts) { return start_of_month(ts); }
1105
1109 long end_of_month(long ts) {
1111 }
1112
1115 long last_day_of_month(long ts) { return end_of_month(ts); }
1116
1122 }
1123
1127
1132 int last_sunday_month_day(long year, int month) {
1133 int days = num_days_in_month(year, month);
1134 return days - day_of_week_date(year, month, days);
1135 }
1136
1139 int final_sunday_month_day(long year, int month) { return last_sunday_month_day(year, month); }
1140
1144 long start_of_hour(long ts) {
1145 return ts - (ts % SEC_PER_HOUR);
1146 }
1147
1150 long hour_begin(long ts) { return start_of_hour(ts); }
1151
1156 return start_of_hour(ms_to_sec(ts_ms));
1157 }
1158
1162
1167 return ts_ms - (ts_ms % MS_PER_HOUR);
1168 }
1169
1173
1177 long end_of_hour(long ts) {
1178 return ts - (ts % SEC_PER_HOUR) + SEC_PER_HOUR - 1;
1179 }
1180
1183 long finish_of_hour(long ts) { return end_of_hour(ts); }
1184
1189 return end_of_hour(ms_to_sec(ts_ms));
1190 }
1191
1195
1200 return ts_ms - (ts_ms % MS_PER_HOUR) + MS_PER_HOUR - 1;
1201 }
1202
1206
1210 int hour_of_day(long ts) {
1211 return (int)((ts / SEC_PER_HOUR) % HOURS_PER_DAY);
1212 }
1213
1216 int hour_in_day(long ts) { return hour_of_day(ts); }
1217
1221 long start_of_week(long ts) {
1223 }
1224
1227 long week_begin(long ts) { return start_of_week(ts); }
1228
1232 long end_of_week(long ts) {
1234 }
1235
1238 long finish_of_week(long ts) { return end_of_week(ts); }
1239
1244 return start_of_day(ts) + (SAT - day_of_week(ts)) * SEC_PER_DAY;
1245 }
1246
1249 long saturday_begin(long ts) { return start_of_saturday(ts); }
1250
1254 long start_of_min(long ts) {
1255 return ts - (ts % SEC_PER_MIN);
1256 }
1257
1260 long min_begin(long ts) { return start_of_min(ts); }
1261
1265 long end_of_min(long ts) {
1266 return ts - (ts % SEC_PER_MIN) + SEC_PER_MIN - 1;
1267 }
1268
1271 long finish_of_min(long ts) { return end_of_min(ts); }
1272
1276 int min_of_day(long ts) {
1277 return (int)((ts / SEC_PER_MIN) % MIN_PER_DAY);
1278 }
1279
1283 int min_of_hour(long ts) {
1284 return (int)((ts / SEC_PER_MIN) % MIN_PER_HOUR);
1285 }
1286
1289 int min_in_hour(long ts) { return min_of_hour(ts); }
1290
1295 long start_of_period(int p, long ts) {
1296 return ts - (ts % p);
1297 }
1298
1303 long end_of_period(int p, long ts) {
1304 return ts - (ts % p) + p - 1;
1305 }
1306
1307 //----------------------------------------------------------------------
1308 // UNIX day and minute helpers
1309 //----------------------------------------------------------------------
1310
1316 long date_to_unix_day(const long year, const int month, const int day) {
1317 const long adj_y = year - (month <= 2 ? 1 : 0);
1318 const long adj_m = month <= 2 ? month + 9 : month - 3;
1319 const long era = (adj_y >= 0 ? adj_y : adj_y - 399) / 400;
1320 const long yoe = adj_y - era * 400;
1321 const long doy = (153 * adj_m + 2) / 5 + day - 1;
1322 const long doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
1323 return era * 146097 + doe - 719468;
1324 }
1325
1329 long get_unix_day(long ts) {
1330 return ts / SEC_PER_DAY;
1331 }
1332
1335 long unix_day(long ts) { return get_unix_day(ts); }
1336
1339 long get_unixday(long ts) { return get_unix_day(ts); }
1340
1343 long unixday(long ts) { return get_unix_day(ts); }
1344
1347 long uday(long ts) { return get_unix_day(ts); }
1348
1353 int get_days_difference(long start, long stop) {
1354 return (int)((stop - start) / SEC_PER_DAY);
1355 }
1356
1359 int get_days(long start, long stop) { return get_days_difference(start, stop); }
1360
1363 int days(long start, long stop) { return get_days_difference(start, stop); }
1364
1369 return get_unix_day(ms_to_sec(ts_ms));
1370 }
1371
1374 long unix_day_ms(long ts_ms) { return get_unix_day_ms(ts_ms); }
1375
1379
1382 long unixday_ms(long ts_ms) { return get_unix_day_ms(ts_ms); }
1383
1386 long uday_ms(long ts_ms) { return get_unix_day_ms(ts_ms); }
1387
1392 return uday * SEC_PER_DAY;
1393 }
1394
1398
1402
1406
1410
1415 return uday * MS_PER_DAY;
1416 }
1417
1421
1425
1429
1433
1438 return uday * SEC_PER_DAY + SEC_PER_DAY - 1;
1439 }
1440
1445 return uday * MS_PER_DAY + MS_PER_DAY - 1;
1446 }
1447
1451
1455
1462
1469
1473
1477
1481
1485
1489
1493 long get_unix_min(long ts) {
1494 return ts / SEC_PER_MIN;
1495 }
1496
1499 long unix_min(long ts) { return get_unix_min(ts); }
1500
1503 long to_unix_min(long ts) { return get_unix_min(ts); }
1504
1507 long umin(long ts) { return get_unix_min(ts); }
1508
1512 int sec_of_day(long ts) {
1513 return (int)(ts % SEC_PER_DAY);
1514 }
1515
1520 return sec_of_day(ms_to_sec(ts_ms));
1521 }
1522
1528 int sec_of_day(int hour, int min, int sec) {
1529 return hour * (int)SEC_PER_HOUR + min * (int)SEC_PER_MIN + sec;
1530 }
1531
1536 return to_time_zone_struct(offset);
1537 }
1538
1541 TimeZoneStruct to_tz_struct(int offset) { return to_time_zone(offset); }
1542
1544
1545}; // namespace time_shield
1546
1547#endif // __TIME_SHIELD_TIME_CONVERSIONS_MQH__
Header file with time-related constants.
Header for date and time structure and related functions (MQL5).
Header file with enumerations for weekdays, months, and other time-related categories.
constexpr int64_t MIN_PER_HOUR
Minutes per hour.
constexpr int64_t ERROR_TIMESTAMP
Error timestamp value.
constexpr int64_t DAYS_PER_WEEK
Days per week.
constexpr int64_t SEC_PER_YEAR
Seconds per year (365 days)
const int64_t MONTHS_PER_YEAR
Months per year.
constexpr int64_t DAYS_PER_YEAR
Days per year.
constexpr int64_t HOURS_PER_DAY
Hours per day.
constexpr int64_t SEC_PER_100_YEARS
Seconds per 100 years.
constexpr int64_t MAX_YEAR
Maximum representable year.
constexpr int64_t MS_PER_MIN
Milliseconds per minute.
Definition constants.hpp:83
constexpr int64_t SEC_PER_LEAP_YEAR
Seconds per leap year (366 days)
constexpr int64_t MIN_PER_DAY
Minutes per day.
constexpr int64_t DAYS_PER_LEAP_YEAR
Days per leap year.
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
constexpr int64_t UNIX_EPOCH
Start year of UNIX time.
constexpr int64_t MS_PER_DAY
Milliseconds per day.
Definition constants.hpp:97
constexpr int64_t MS_PER_SEC
Milliseconds per second.
Definition constants.hpp:77
constexpr int64_t SEC_PER_400_YEARS
Seconds per 400 years.
constexpr int64_t SEC_PER_DAY
Seconds per day.
constexpr int64_t SEC_PER_MIN
Seconds per minute.
constexpr int64_t NS_PER_SEC
Nanoseconds per second.
Definition constants.hpp:73
constexpr int64_t MS_PER_HOUR
Milliseconds per hour.
Definition constants.hpp:90
constexpr int64_t US_PER_SEC
Microseconds per second.
Definition constants.hpp:76
constexpr int64_t SEC_PER_4_YEARS
Seconds per 4 years.
constexpr T start_of_day_from_unix_day_ms(uday_t unix_day) noexcept
Alias for unix_day_to_ts_ms function.
constexpr ts_t min_begin(ts_t ts=time_shield::ts()) noexcept
Alias for start_of_min function.
constexpr T unix_day(ts_t ts=time_shield::ts()) noexcept
Alias for days_since_epoch function.
constexpr 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 T1 days_in_month(T2 year, T3 month) noexcept
Alias for num_days_in_month function.
constexpr T get_unixday(ts_t ts=time_shield::ts()) noexcept
Alias for get_unix_day function.
TIME_SHIELD_CONSTEXPR T get_year(ts_t ts=time_shield::ts())
Alias for year_of function.
TIME_SHIELD_CONSTEXPR ts_ms_t end_of_last_workday_month_ms(year_t year, int month) noexcept
Returns end-of-day millisecond timestamp for the last workday of month.
TIME_SHIELD_CONSTEXPR ts_ms_t year_begin_date_ms(T year)
Alias for start_of_year_date_ms function.
constexpr T get_unix_day(ts_t ts=time_shield::ts()) noexcept
Alias for days_since_epoch function.
constexpr T next_day_unixday_ms(uday_t unix_day) noexcept
Alias for start_of_next_day_from_unix_day_ms function.
ts_ms_t year_start_ms(ts_ms_t ts_ms=time_shield::ts_ms())
Alias for start_of_year_ms function.
TIME_SHIELD_CONSTEXPR ts_t end_of_first_workday_month(year_t year, int month) noexcept
Returns end-of-day timestamp for the first workday of month.
constexpr T get_days_difference(ts_t start, ts_t stop) noexcept
Alias for days_between function.
constexpr ts_ms_t day_start_ms(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Alias for start_of_day_ms function.
constexpr T1 min_to_sec(T2 ts) noexcept
Converts a timestamp from minutes to seconds.
TIME_SHIELD_CONSTEXPR T1 get_weekday_from_date(const T2 &date)
Alias for weekday_of_date.
constexpr T unixday_ms(ts_ms_t t_ms=time_shield::ts_ms()) noexcept
Alias for days_since_epoch function.
constexpr ts_t finish_of_min(ts_t ts=time_shield::ts()) noexcept
Alias for end_of_min function.
long eod_from_unix_day(long uday)
Alias for end_of_day_from_unix_day.
constexpr 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 int last_workday_day(year_t year, int month) noexcept
Finds the last workday number within a month.
constexpr T unixday_to_ts_ms(uday_t unix_day) noexcept
Alias for unix_day_to_ts_ms function.
constexpr ts_ms_t day_end_ms(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Alias for end_of_day_ms function.
constexpr T uday_to_ts_ms(uday_t unix_day) noexcept
Alias for unix_day_to_ts_ms function.
constexpr ts_ms_t finish_of_hour_ms(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Alias for end_of_hour_ms function.
constexpr T next_day_from_unix_day(uday_t unix_day) noexcept
Alias for start_of_next_day_from_unix_day function.
constexpr T1 hour_to_ms(T2 ts) noexcept
Converts a timestamp from hours to milliseconds.
TIME_SHIELD_CONSTEXPR ts_ms_t start_of_first_workday_month_ms(year_t year, int month) noexcept
Returns start-of-day millisecond timestamp for the first workday of month.
TIME_SHIELD_CONSTEXPR uday_t date_to_unix_day(Year year, Month month, Day day) noexcept
Convert a calendar date to UNIX day count.
constexpr ts_t previous_day_start(ts_t ts=time_shield::ts(), T days=1) noexcept
Alias for start_of_prev_day function.
TIME_SHIELD_CONSTEXPR ts_t year_begin_date(T year)
Alias for start_of_year_date function.
TIME_SHIELD_CONSTEXPR ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
constexpr T ms_of_ts(ts_ms_t ts) noexcept
Get the millisecond part of the timestamp.
TIME_SHIELD_CONSTEXPR T1 num_days_in_year(T2 year) noexcept
Get the number of days in a year.
TIME_SHIELD_CONSTEXPR T1 day_of_week_date(T2 year, T3 month, T4 day)
Get the day of the week.
TIME_SHIELD_CONSTEXPR ts_ms_t year_start_date_ms(T year)
Alias for start_of_year_date_ms function.
TIME_SHIELD_CONSTEXPR T to_year(ts_t ts=time_shield::ts())
Alias for year_of function.
constexpr T get_unix_day_ms(ts_ms_t t_ms=time_shield::ts_ms()) noexcept
Alias for days_since_epoch function.
constexpr ts_t finish_of_week(ts_t ts=time_shield::ts())
Alias for end_of_week function.
constexpr double sec_to_fhour(T ts) noexcept
Converts a timestamp from seconds to floating-point hours.
constexpr T unix_day_ms(ts_ms_t t_ms=time_shield::ts_ms()) noexcept
Alias for days_since_epoch function.
TIME_SHIELD_CONSTEXPR int count_workdays_in_month(year_t year, int month) noexcept
Counts workdays within a month.
TIME_SHIELD_CONSTEXPR T hour24_to_12(T hour) noexcept
Converts a 24-hour format hour to a 12-hour format.
constexpr T1 ms_to_hour(T2 ts) noexcept
Converts a timestamp from milliseconds to hours.
constexpr T to_unix_year(ts_t ts) noexcept
Alias for years_since_epoch function.
T to_time_zone(tz_t offset)
Converts an integer to a time zone structure.
TIME_SHIELD_CONSTEXPR ts_t year_begin(ts_t ts=time_shield::ts())
Alias for start_of_year function.
constexpr T uday_ms(ts_ms_t t_ms=time_shield::ts_ms()) noexcept
Alias for days_since_epoch function.
constexpr T unix_year(ts_t ts) noexcept
Alias for years_since_epoch function.
constexpr T get_unix_min(ts_t ts=time_shield::ts())
Alias for min_since_epoch function.
TIME_SHIELD_CONSTEXPR ts_t end_of_last_workday_month(year_t year, int month) noexcept
Returns end-of-day timestamp for the last workday of month.
constexpr T unix_day_to_timestamp(uday_t unix_day) noexcept
Alias for unix_day_to_ts function.
TIME_SHIELD_CONSTEXPR ts_ms_t ts_ms(year_t year, int month, int day)
Alias for to_timestamp_ms.
constexpr ts_ms_t hour_begin_ms(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Alias for start_of_hour_ms function.
TIME_SHIELD_CONSTEXPR ts_t to_ts(year_t year, int month, int day)
Alias for to_timestamp.
constexpr ts_t saturday_begin(ts_t ts=time_shield::ts())
Alias for start_of_saturday function.
constexpr T1 hour_to_sec(T2 ts) noexcept
Converts a timestamp from hours to seconds.
TIME_SHIELD_CONSTEXPR T get_year_ms(ts_ms_t ts_ms=time_shield::ts_ms())
Alias for year_of_ms function.
constexpr T1 get_weekday(year_t year, int month, int day)
Alias for day_of_week_date.
constexpr T sec_of_day_ms(ts_ms_t ts_ms) noexcept
Get the second of the day from milliseconds timestamp.
constexpr double sec_to_fmin(T ts) noexcept
Converts a timestamp from seconds to floating-point minutes.
constexpr ts_t day_start(ts_t ts=time_shield::ts()) noexcept
Alias for start_of_day function.
constexpr ts_t finish_of_hour_sec(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Alias for end_of_hour_sec function.
constexpr T next_day_unix_day(uday_t unix_day) noexcept
Alias for start_of_next_day_from_unix_day function.
TIME_SHIELD_CONSTEXPR int first_workday_day(year_t year, int month) noexcept
Finds the first workday number within a month.
TIME_SHIELD_CONSTEXPR ts_t final_sunday_of_month(ts_t ts=time_shield::ts())
Alias for last_sunday_of_month function.
constexpr T1 day_of_week(year_t year, int month, int day)
Alias for day_of_week_date.
long eod_from_unix_day_ms(long uday)
Alias for end_of_day_from_unix_day_ms.
TIME_SHIELD_CONSTEXPR int workday_index_in_month(year_t year, int month, int day) noexcept
Returns workday position in month starting from 1.
constexpr fts_t hour_to_fsec(T hr) noexcept
Converts a timestamp from hours to floating-point seconds.
TIME_SHIELD_CONSTEXPR ts_t year_start_date(T year)
Alias for start_of_year_date function.
TIME_SHIELD_CONSTEXPR ts_t last_day_of_month(ts_t ts=time_shield::ts())
Alias for end_of_month function.
constexpr fts_t min_to_fsec(T min) noexcept
Converts a timestamp from minutes to floating-point seconds.
TIME_SHIELD_CONSTEXPR ts_t year_start(ts_t ts=time_shield::ts())
Alias for start_of_year function.
constexpr T1 ms_to_sec(T2 ts_ms) noexcept
Converts a timestamp from milliseconds to seconds.
constexpr T start_of_day_from_unix_day(uday_t unix_day) noexcept
Alias for unix_day_to_ts function.
T to_dt_ms(ts_ms_t ts)
Alias for to_date_time_ms function.
TIME_SHIELD_CONSTEXPR ts_ms_t year_end_ms(ts_ms_t ts_ms=time_shield::ts_ms())
Alias for end_of_year_ms function.
constexpr ts_t day_end(ts_t ts=time_shield::ts()) noexcept
Alias for end_of_day function.
TIME_SHIELD_CONSTEXPR ts_ms_t start_of_last_workday_month_ms(year_t year, int month) noexcept
Returns start-of-day millisecond timestamp for the last workday of month.
constexpr ts_ms_t next_day_start_ms(ts_ms_t ts_ms, T days=1) noexcept
Alias for start_of_next_day_ms function.
constexpr T uday_to_ts(uday_t unix_day) noexcept
Alias for unix_day_to_ts function.
constexpr T next_day_unix_day_ms(uday_t unix_day) noexcept
Alias for start_of_next_day_from_unix_day_ms function.
constexpr ts_t week_begin(ts_t ts=time_shield::ts())
Alias for start_of_week function.
constexpr T hour_in_day(ts_t ts=time_shield::ts()) noexcept
Alias for hour_of_day function.
constexpr T umin(ts_t ts=time_shield::ts())
Alias for min_since_epoch function.
constexpr T1 min_to_ms(T2 ts) noexcept
Converts a timestamp from minutes to milliseconds.
constexpr T day_of_week_ms(ts_ms_t ts_ms)
Alias for weekday_of_ts_ms function.
constexpr T get_unix_year(ts_t ts) noexcept
Alias for years_since_epoch function.
TIME_SHIELD_CONSTEXPR T1 final_sunday_month_day(T2 year, T3 month)
Alias for last_sunday_month_day function.
constexpr T1 ms_to_min(T2 ts) noexcept
Converts a timestamp from milliseconds to minutes.
constexpr T to_unix_min(ts_t ts=time_shield::ts())
Alias for min_since_epoch function.
constexpr T unix_min(ts_t ts=time_shield::ts())
Alias for min_since_epoch function.
constexpr T unix_day_to_timestamp_ms(uday_t unix_day) noexcept
Alias for unix_day_to_ts_ms function.
constexpr ts_t day_start_sec(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Alias for start_of_day_sec function.
constexpr T1 sec_to_min(T2 ts) noexcept
Converts a timestamp from seconds to minutes.
TIME_SHIELD_CONSTEXPR T num_days_in_year_ts(ts_t ts=time_shield::ts())
Get the number of days in the current year.
TIME_SHIELD_CONSTEXPR ts_ms_t to_ts_ms(year_t year, int month, int day)
Alias for to_timestamp_ms.
constexpr T unixday(ts_t ts=time_shield::ts()) noexcept
Alias for days_since_epoch function.
constexpr T days(ts_t start, ts_t stop) noexcept
Alias for days_between function.
constexpr T1 sec_to_hour(T2 ts) noexcept
Converts a timestamp from seconds to hours.
TIME_SHIELD_CONSTEXPR ts_t start_of_last_workday_month(year_t year, int month) noexcept
Returns start-of-day timestamp for the last workday of month.
int min_in_hour(long ts)
Alias for min_of_hour.
constexpr ts_t next_day_start(ts_t ts, T days=1) noexcept
Alias for start_of_next_day function.
constexpr 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.
constexpr T1 sec_to_ms(T2 ts) noexcept
Converts a timestamp from seconds to milliseconds.
ts_ms_t year_begin_ms(ts_ms_t ts_ms=time_shield::ts_ms())
Alias for start_of_year_ms function.
constexpr T get_days(ts_t start, ts_t stop) noexcept
Alias for days_between function.
constexpr 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.
constexpr ts_t hour_begin_sec(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Alias for start_of_hour_sec function.
TIME_SHIELD_CONSTEXPR T year(ts_t ts=time_shield::ts())
Alias for year_of function.
MqlDateTime to_mql_dt(long ts)
Alias for to_date_time_mql.
TimeZoneStruct to_tz_struct(int offset)
Alias for to_time_zone.
constexpr T1 days_in_year(T2 year) noexcept
Alias for num_days_in_year function.
constexpr ts_t day_end_sec(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Alias for end_of_day_sec function.
T1 to_dt(T2 ts)
Alias for to_date_time function.
TIME_SHIELD_CONSTEXPR ts_t month_begin(ts_t ts=time_shield::ts())
Alias for start_of_month function.
TIME_SHIELD_CONSTEXPR T h24_to_h12(T hour) noexcept
Alias for hour24_to_12 function.
constexpr T unix_day_to_ts(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp in seconds.
constexpr T unix_day_to_ts_ms(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp in milliseconds.
constexpr ts_t finish_of_hour(ts_t ts=time_shield::ts()) noexcept
Alias for end_of_hour function.
TIME_SHIELD_CONSTEXPR ts_ms_t end_of_first_workday_month_ms(year_t year, int month) noexcept
Returns end-of-day millisecond timestamp for the first workday of month.
constexpr ts_t hour_begin(ts_t ts=time_shield::ts()) noexcept
Alias for start_of_hour function.
constexpr fts_t ms_to_fsec(T ts_ms) noexcept
Converts a timestamp from milliseconds to floating-point seconds.
TIME_SHIELD_CONSTEXPR T to_year_ms(ts_ms_t ts_ms=time_shield::ts_ms())
Alias for year_of_ms function.
TIME_SHIELD_CONSTEXPR T year_ms(ts_ms_t ts_ms=time_shield::ts_ms())
Alias for year_of_ms function.
constexpr T1 day_of_week_dt(const T2 &date)
Alias for weekday_of_date.
constexpr T unixday_to_ts(uday_t unix_day) noexcept
Alias for unix_day_to_ts function.
constexpr T next_day_unixday(uday_t unix_day) noexcept
Alias for start_of_next_day_from_unix_day function.
TIME_SHIELD_CONSTEXPR ts_t start_of_first_workday_month(year_t year, int month) noexcept
Returns start-of-day timestamp for the first workday of month.
constexpr T get_unixday_ms(ts_ms_t t_ms=time_shield::ts_ms()) noexcept
Alias for days_since_epoch function.
constexpr T uday(ts_t ts=time_shield::ts()) noexcept
Alias for days_since_epoch function.
constexpr T days_in_year_ts(ts_t ts=time_shield::ts())
Alias for num_days_in_year_ts function.
MqlDateTime to_date_time_mql(long ts)
Convert a timestamp to the standard MqlDateTime structure.
@ FEB
February.
Definition enums.hpp:95
@ SAT
Saturday.
Definition enums.hpp:34
@ THU
Thursday.
Definition enums.hpp:32
bool is_last_workday_of_month(const std::string &str)
Parse an ISO8601 string and check if it is the last workday of its month (seconds).
bool is_within_last_workdays_of_month_ms(const std::string &str, int count)
Parse ISO8601 string and check if it is within last N workdays of its month (milliseconds).
bool is_first_workday_of_month_ms(const std::string &str)
Parse an ISO8601 string and check if it is the first workday of its month (millisecond precision).
bool is_within_first_workdays_of_month_ms(const std::string &str, int count)
Parse an ISO8601 string and check if it falls within the first N workdays of its month (millisecond p...
bool is_workday(const std::string &str)
Parse ISO8601 string and check if it falls on a workday (seconds precision).
bool sec_of_day(const std::string &str, T &sec)
Parse time of day string to seconds of day.
bool is_last_workday_of_month_ms(const std::string &str)
Parse an ISO8601 string and check if it is the last workday of its month (millisecond).
bool is_within_first_workdays_of_month(const std::string &str, int count)
Parse an ISO8601 string and check if it falls within the first N workdays of its month.
bool is_first_workday_of_month(const std::string &str)
Parse ISO8601 string and check if it is the first workday of its month (seconds).
bool is_within_last_workdays_of_month(const std::string &str, int count)
Parse ISO8601 string and check if it is within last N workdays of its month (seconds).
TimeZoneStruct to_time_zone_struct(tz_t offset)
Converts an integer to a TimeZoneStruct.
TIME_SHIELD_CONSTEXPR T day_of_month(ts_t ts=time_shield::ts())
Get the day of the month.
constexpr ts_t end_of_hour(ts_t ts=time_shield::ts()) noexcept
Get the timestamp at the end of the hour.
TIME_SHIELD_CONSTEXPR fts_t dt_to_ftimestamp(const T &date_time)
Converts a date-time structure to a floating-point timestamp.
constexpr ts_t start_of_saturday(ts_t ts=time_shield::ts())
Get the timestamp of the start of Saturday.
constexpr ts_t end_of_week(ts_t ts=time_shield::ts())
Get the timestamp of the end of the week.
constexpr ts_t start_of_period(T p, ts_t ts=time_shield::ts())
Get the timestamp of the start of the period.
TIME_SHIELD_CONSTEXPR ts_ms_t end_of_year_ms(ts_ms_t ts_ms=time_shield::ts_ms())
Get the timestamp in milliseconds of the end of the year.
T to_date_time_ms(ts_ms_t ts)
Converts a timestamp in milliseconds to a date-time structure with milliseconds.
constexpr ts_t start_of_min(ts_t ts=time_shield::ts()) noexcept
Get the timestamp of the beginning of the minute.
TIME_SHIELD_CONSTEXPR T month_of_year(ts_t ts) noexcept
Get the month of the year.
TIME_SHIELD_CONSTEXPR T1 last_sunday_month_day(T2 year, T3 month)
Get the day of the last Sunday of the given month and year.
constexpr ts_ms_t end_of_hour_ms(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Get the timestamp at the end of the hour in milliseconds.
constexpr ts_ms_t end_of_day_ms(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Get the timestamp at the end of the day in milliseconds.
TIME_SHIELD_CONSTEXPR ts_t end_of_year(ts_t ts=time_shield::ts())
Get the end-of-year timestamp.
constexpr ts_ms_t start_of_day_ms(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Get the start of the day timestamp in milliseconds.
constexpr T min_of_hour(ts_t ts=time_shield::ts()) noexcept
Get minute of hour. This function returns a value between 0 to 59.
constexpr ts_t start_of_hour(ts_t ts=time_shield::ts()) noexcept
Get the timestamp at the start of the hour.
constexpr T hour_of_day(ts_t ts=time_shield::ts()) noexcept
Get hour of day. This function returns a value between 0 to 23.
TIME_SHIELD_CONSTEXPR ts_t start_of_year(ts_t ts) noexcept
Get the start of the year timestamp.
constexpr ts_t end_of_day(ts_t ts=time_shield::ts()) noexcept
Get the timestamp at the end of the day.
T day_of_year(ts_t ts=time_shield::ts())
Get the day of the year.
TIME_SHIELD_CONSTEXPR ts_t start_of_month(ts_t ts=time_shield::ts())
Get the timestamp at the start of the current month.
constexpr T get_weekday_from_ts_ms(ts_ms_t ts_ms)
Alias for weekday_of_ts_ms.
TIME_SHIELD_CONSTEXPR ts_t dt_to_timestamp(const T &date_time)
Converts a date-time structure to a timestamp.
constexpr ts_ms_t start_of_hour_ms(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Get the timestamp at the start of the hour. This function sets the minute and second to zero.
TIME_SHIELD_CONSTEXPR T1 num_days_in_month_ts(ts_t ts=time_shield::ts()) noexcept
Get the number of days in the month of the given timestamp.
TIME_SHIELD_CONSTEXPR 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 ts_t end_of_month(ts_t ts=time_shield::ts())
Get the last timestamp of the current month.
constexpr ts_t start_of_day(ts_t ts=time_shield::ts()) noexcept
Get the start of the day timestamp.
TIME_SHIELD_CONSTEXPR ts_ms_t start_of_year_ms(ts_ms_t ts_ms=time_shield::ts_ms())
Get the timestamp at the start of the year in milliseconds.
constexpr ts_t end_of_hour_sec(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Get the timestamp at the end of the hour in seconds.
TIME_SHIELD_CONSTEXPR 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 ts_t end_of_min(ts_t ts=time_shield::ts()) noexcept
Get the timestamp of the end of the minute.
constexpr 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.
TIME_SHIELD_CONSTEXPR ts_t last_sunday_of_month(ts_t ts=time_shield::ts())
Get the timestamp of the last Sunday of the current month.
TIME_SHIELD_CONSTEXPR ts_t start_of_year_date(T year)
Get the timestamp of the start of the year.
constexpr 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 ts_t end_of_period(T p, ts_t ts=time_shield::ts())
Get the timestamp of the end of the period.
constexpr 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 ts_t next_day(ts_t ts, T days=1) noexcept
Calculate the timestamp for a specified number of days in the future.
T1 to_date_time(T2 ts)
Converts a timestamp to a date-time structure.
TIME_SHIELD_CONSTEXPR ts_t dt_to_timestamp_ms(const T &date_time)
Converts a date-time structure to a timestamp in milliseconds.
constexpr ts_t start_of_week(ts_t ts=time_shield::ts())
Get the timestamp of the beginning of the week.
constexpr ts_t start_of_day_sec(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Get the start of the day timestamp in seconds.
constexpr T min_of_day(ts_t ts=time_shield::ts()) noexcept
Get minute of day. This function returns a value between 0 to 1439 (minute of day).
constexpr ts_t start_of_prev_day(ts_t ts=time_shield::ts(), T days=1) noexcept
Get timestamp of the start of the previous day.
constexpr T1 num_days_in_month(T2 year, T3 month) noexcept
Get the number of days in a month.
constexpr T get_weekday_from_ts(ts_t ts) noexcept
Alias for weekday_of_ts.
TIME_SHIELD_CONSTEXPR ts_ms_t start_of_year_date_ms(T year)
Get the timestamp in milliseconds of the start of the year.
constexpr ts_t start_of_hour_sec(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Get the timestamp at the start of the hour.
constexpr ts_t end_of_day_sec(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Get the timestamp at the end of the day in seconds.
TIME_SHIELD_CONSTEXPR 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-time structure to a timestamp in milliseconds.
T ns_of_sec() noexcept
Get the nanosecond part of the current second.
T ms_of_sec() noexcept
Get the millisecond part of the current second.
ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
T us_of_sec() noexcept
Get the microsecond part of the current second.
TIME_SHIELD_CONSTEXPR bool is_leap_year(ts_t ts)
Alias for is_leap_year_ts function.
constexpr bool is_leap_year_date(T year) noexcept
Checks if the given year is a leap year.
TIME_SHIELD_CONSTEXPR bool is_leap_year_ts(ts_t ts)
Checks if the given year is a leap year.
Main namespace for the Time Shield library.
Structure to represent date and time.
int ms
Millisecond component of time (0-999)
int hour
Hour component of time (0-23)
int64_t year
Year component of the date.
int day
Day component of the date (1-31).
int min
Minute component of time (0-59)
int mon
Month component of the date (1-12).
int sec
Second component of time (0-59)
Structure to represent time zone information.
Header for time zone structure and related functions (MQL5).
Header with validation functions for dates, times, and timestamps.