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
861 int workday_index_in_month(long year, int month, int day) {
862 if(!is_workday(year, month, day)) return 0;
863 int days = num_days_in_month(year, month);
864 if(days <= 0) return 0;
865 int index = 0;
866 for(int current = 1; current <= days; ++current) {
867 if(is_workday(year, month, current)) {
868 ++index;
869 if(current == day) return index;
870 }
871 }
872 return 0;
873 }
874
875 //----------------------------------------------------------------------
876 // Workday boundary predicates
877 //----------------------------------------------------------------------
878
879 bool is_first_workday_of_month(long year, int month, int day) {
880 return is_workday(year, month, day) && first_workday_day(year, month) == day;
881 }
882
883 bool is_within_first_workdays_of_month(long year, int month, int day, int count) {
884 if(count <= 0) return false;
885 int total = count_workdays_in_month(year, month);
886 if(count > total) return false;
887 int index = workday_index_in_month(year, month, day);
888 return index > 0 && index <= count;
889 }
890
891 bool is_last_workday_of_month(long year, int month, int day) {
892 return is_workday(year, month, day) && last_workday_day(year, month) == day;
893 }
894
895 bool is_within_last_workdays_of_month(long year, int month, int day, int count) {
896 if(count <= 0) return false;
897 int total = count_workdays_in_month(year, month);
898 if(count > total) return false;
899 int index = workday_index_in_month(year, month, day);
900 return index > 0 && index >= (total - count + 1);
901 }
902
906
907 bool is_within_first_workdays_of_month(const long ts, int count) {
909 }
910
914
915 bool is_within_last_workdays_of_month(const long ts, int count) {
917 }
918
922
923 bool is_within_first_workdays_of_month_ms(const long ts_ms, int count) {
925 }
926
930
931 bool is_within_last_workdays_of_month_ms(const long ts_ms, int count) {
933 }
934
939 if (is_leap_year_date(year)) return (int)DAYS_PER_LEAP_YEAR;
940 return (int)DAYS_PER_YEAR;
941 }
942
945 int days_in_year(long year) { return num_days_in_year(year); }
946
951 if(is_leap_year_ts(ts)) return (int)DAYS_PER_LEAP_YEAR;
952 return (int)DAYS_PER_YEAR;
953 }
954
958
962 long start_of_month(long ts) {
963 return start_of_day(ts) - (day_of_month(ts) - 1) * SEC_PER_DAY;
964 }
965
968 long month_begin(long ts) { return start_of_month(ts); }
969
973 long end_of_month(long ts) {
975 }
976
979 long last_day_of_month(long ts) { return end_of_month(ts); }
980
986 }
987
991
996 int last_sunday_month_day(long year, int month) {
997 int days = num_days_in_month(year, month);
998 return days - day_of_week_date(year, month, days);
999 }
1000
1003 int final_sunday_month_day(long year, int month) { return last_sunday_month_day(year, month); }
1004
1008 long start_of_hour(long ts) {
1009 return ts - (ts % SEC_PER_HOUR);
1010 }
1011
1014 long hour_begin(long ts) { return start_of_hour(ts); }
1015
1020 return start_of_hour(ms_to_sec(ts_ms));
1021 }
1022
1026
1031 return ts_ms - (ts_ms % MS_PER_HOUR);
1032 }
1033
1037
1041 long end_of_hour(long ts) {
1042 return ts - (ts % SEC_PER_HOUR) + SEC_PER_HOUR - 1;
1043 }
1044
1047 long finish_of_hour(long ts) { return end_of_hour(ts); }
1048
1053 return end_of_hour(ms_to_sec(ts_ms));
1054 }
1055
1059
1064 return ts_ms - (ts_ms % MS_PER_HOUR) + MS_PER_HOUR - 1;
1065 }
1066
1070
1074 int hour_of_day(long ts) {
1075 return (int)((ts / SEC_PER_HOUR) % HOURS_PER_DAY);
1076 }
1077
1080 int hour_in_day(long ts) { return hour_of_day(ts); }
1081
1085 long start_of_week(long ts) {
1087 }
1088
1091 long week_begin(long ts) { return start_of_week(ts); }
1092
1096 long end_of_week(long ts) {
1098 }
1099
1102 long finish_of_week(long ts) { return end_of_week(ts); }
1103
1108 return start_of_day(ts) + (SAT - day_of_week(ts)) * SEC_PER_DAY;
1109 }
1110
1113 long saturday_begin(long ts) { return start_of_saturday(ts); }
1114
1118 long start_of_min(long ts) {
1119 return ts - (ts % SEC_PER_MIN);
1120 }
1121
1124 long min_begin(long ts) { return start_of_min(ts); }
1125
1129 long end_of_min(long ts) {
1130 return ts - (ts % SEC_PER_MIN) + SEC_PER_MIN - 1;
1131 }
1132
1135 long finish_of_min(long ts) { return end_of_min(ts); }
1136
1140 int min_of_day(long ts) {
1141 return (int)((ts / SEC_PER_MIN) % MIN_PER_DAY);
1142 }
1143
1147 int min_of_hour(long ts) {
1148 return (int)((ts / SEC_PER_MIN) % MIN_PER_HOUR);
1149 }
1150
1153 int min_in_hour(long ts) { return min_of_hour(ts); }
1154
1159 long start_of_period(int p, long ts) {
1160 return ts - (ts % p);
1161 }
1162
1167 long end_of_period(int p, long ts) {
1168 return ts - (ts % p) + p - 1;
1169 }
1170
1171 //----------------------------------------------------------------------
1172 // UNIX day and minute helpers
1173 //----------------------------------------------------------------------
1174
1180 long date_to_unix_day(const long year, const int month, const int day) {
1181 const long adj_y = year - (month <= 2 ? 1 : 0);
1182 const long adj_m = month <= 2 ? month + 9 : month - 3;
1183 const long era = (adj_y >= 0 ? adj_y : adj_y - 399) / 400;
1184 const long yoe = adj_y - era * 400;
1185 const long doy = (153 * adj_m + 2) / 5 + day - 1;
1186 const long doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
1187 return era * 146097 + doe - 719468;
1188 }
1189
1193 long get_unix_day(long ts) {
1194 return ts / SEC_PER_DAY;
1195 }
1196
1199 long unix_day(long ts) { return get_unix_day(ts); }
1200
1203 long get_unixday(long ts) { return get_unix_day(ts); }
1204
1207 long unixday(long ts) { return get_unix_day(ts); }
1208
1211 long uday(long ts) { return get_unix_day(ts); }
1212
1217 int get_days_difference(long start, long stop) {
1218 return (int)((stop - start) / SEC_PER_DAY);
1219 }
1220
1223 int get_days(long start, long stop) { return get_days_difference(start, stop); }
1224
1227 int days(long start, long stop) { return get_days_difference(start, stop); }
1228
1233 return get_unix_day(ms_to_sec(ts_ms));
1234 }
1235
1238 long unix_day_ms(long ts_ms) { return get_unix_day_ms(ts_ms); }
1239
1243
1246 long unixday_ms(long ts_ms) { return get_unix_day_ms(ts_ms); }
1247
1250 long uday_ms(long ts_ms) { return get_unix_day_ms(ts_ms); }
1251
1256 return uday * SEC_PER_DAY;
1257 }
1258
1262
1266
1270
1274
1279 return uday * MS_PER_DAY;
1280 }
1281
1285
1289
1293
1297
1302 return uday * SEC_PER_DAY + SEC_PER_DAY - 1;
1303 }
1304
1309 return uday * MS_PER_DAY + MS_PER_DAY - 1;
1310 }
1311
1315
1319
1326
1333
1337
1341
1345
1349
1353
1357 long get_unix_min(long ts) {
1358 return ts / SEC_PER_MIN;
1359 }
1360
1363 long unix_min(long ts) { return get_unix_min(ts); }
1364
1367 long to_unix_min(long ts) { return get_unix_min(ts); }
1368
1371 long umin(long ts) { return get_unix_min(ts); }
1372
1376 int sec_of_day(long ts) {
1377 return (int)(ts % SEC_PER_DAY);
1378 }
1379
1384 return sec_of_day(ms_to_sec(ts_ms));
1385 }
1386
1392 int sec_of_day(int hour, int min, int sec) {
1393 return hour * (int)SEC_PER_HOUR + min * (int)SEC_PER_MIN + sec;
1394 }
1395
1400 return to_time_zone_struct(offset);
1401 }
1402
1405 TimeZoneStruct to_tz_struct(int offset) { return to_time_zone(offset); }
1406
1408
1409}; // namespace time_shield
1410
1411#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 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_timestamp_ms function.
constexpr ts_t min_begin(ts_t ts=time_shield::ts()) noexcept
Alias for start_of_min function.
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. This function sets the minute and second to 59.
constexpr T unix_day(ts_t ts=time_shield::ts()) noexcept
Alias for get_unix_day 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.
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 T get_unixday(ts_t ts=time_shield::ts()) noexcept
Alias for get_unix_day function.
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 T get_year(ts_t ts=time_shield::ts())
Get the year from the timestamp.
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.
TIME_SHIELD_CONSTEXPR ts_t year_begin_ms(ts_t ts_ms=time_shield::ts_ms())
Alias for start_of_year_ms function.
TIME_SHIELD_CONSTEXPR ts_ms_t year_begin_date_ms(T year)
Alias for start_of_year_date_ms function.
TIME_SHIELD_CONSTEXPR bool is_first_workday_of_month_ms(ts_ms_t ts_ms) noexcept
Check whether a timestamp in milliseconds falls on the first workday of its month.
constexpr T get_unix_day(ts_t ts=time_shield::ts()) noexcept
Get UNIX day.
constexpr T next_day_unixday_ms(uday_t unix_day) noexcept
Alias for start_of_next_day_from_unix_day_ms function.
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 bool is_within_last_workdays_of_month_ms(ts_ms_t ts_ms, int count) noexcept
Check whether a timestamp in milliseconds falls within the last N workdays of its month.
constexpr T get_days_difference(ts_t start, ts_t stop) noexcept
Get the number of days between two timestamps.
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 last_sunday_month_day(T2 year, T3 month)
Get the day of the last Sunday of the given month and year.
constexpr T unixday_ms(ts_ms_t t_ms=time_shield::ts_ms()) noexcept
Alias for get_unix_day_ms function.
constexpr ts_t finish_of_min(ts_t ts=time_shield::ts()) noexcept
Alias for end_of_min function.
TIME_SHIELD_CONSTEXPR bool is_within_first_workdays_of_month(year_t year, int month, int day, int count) noexcept
Check whether a date is within the first N workdays of its month.
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.
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.
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 int last_workday_day(year_t year, int month) noexcept
Compute the calendar day number of the last workday in a month.
constexpr T unixday_to_ts_ms(uday_t unix_day) noexcept
Alias for unix_day_to_timestamp_ms function.
constexpr T sec_of_day(ts_t ts=time_shield::ts()) noexcept
Get the second of the day.
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_timestamp_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 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 bool is_within_first_workdays_of_month_ms(ts_ms_t ts_ms, int count) noexcept
Check whether a timestamp in milliseconds falls within the first N workdays of its month.
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 ts_t end_of_year(ts_t ts=time_shield::ts())
Get the end-of-year timestamp.
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 get_year function.
constexpr T get_unix_day_ms(ts_ms_t t_ms=time_shield::ts_ms()) noexcept
Get UNIX day from milliseconds timestamp.
constexpr ts_t finish_of_week(ts_t ts=time_shield::ts())
Alias for end_of_week function.
TIME_SHIELD_CONSTEXPR bool is_last_workday_of_month(year_t year, int month, int day) noexcept
Check whether a date is the last workday of its month.
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 get_unix_day_ms function.
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.
TIME_SHIELD_CONSTEXPR int count_workdays_in_month(year_t year, int month) noexcept
Count the number of workdays contained within a month.
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.
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 ts_t start_of_hour(ts_t ts=time_shield::ts()) noexcept
Get the timestamp at the start of the hour.
constexpr T to_unix_year(ts_t ts) noexcept
Alias for get_unix_year function.
T to_time_zone(tz_t offset)
Converts an integer to a time zone structure.
TIME_SHIELD_CONSTEXPR bool is_first_workday_of_month(year_t year, int month, int day) noexcept
Check whether a date is the first workday of its month.
constexpr T hour_of_day(ts_t ts=time_shield::ts()) noexcept
Get the hour of the day.
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 get_unix_day_ms function.
constexpr T unix_year(ts_t ts) noexcept
Alias for get_unix_year function.
constexpr T get_unix_min(ts_t ts=time_shield::ts())
Get UNIX minute.
constexpr T unix_day_to_timestamp(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp in seconds.
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 ts_t start_of_year(ts_t ts) noexcept
Get the start of the year timestamp.
TIME_SHIELD_CONSTEXPR T get_year_ms(ts_ms_t ts_ms=time_shield::ts_ms())
Get the year from the timestamp in milliseconds.
TIME_SHIELD_CONSTEXPR bool is_last_workday_of_month_ms(ts_ms_t ts_ms) noexcept
Check whether a timestamp in milliseconds falls on the last workday of its month.
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.
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
Compute the calendar day number of the first workday in 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.
TIME_SHIELD_CONSTEXPR ts_t start_of_month(ts_t ts=time_shield::ts())
Get the timestamp at the start of the current month.
TIME_SHIELD_CONSTEXPR bool is_within_last_workdays_of_month(year_t year, int month, int day, int count) noexcept
Check whether a date is within the last N workdays of its month.
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
Determine the 1-based index of a workday within its month.
constexpr T get_weekday_from_ts_ms(ts_ms_t ts_ms)
Get the weekday from a timestamp in milliseconds.
TIME_SHIELD_CONSTEXPR ts_t dt_to_timestamp(const T &date_time)
Converts a date-time structure to a timestamp.
constexpr fts_t hour_to_fsec(T hr) noexcept
Converts a timestamp from hours to floating-point seconds.
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 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.
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.
constexpr T1 num_days_in_month(ts_t ts=time_shield::ts()) noexcept
Alias for num_days_in_month_ts 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 fts_t to_ftimestamp(const T &date_time)
Alias for dt_to_ftimestamp.
constexpr T start_of_day_from_unix_day(uday_t unix_day) noexcept
Alias for unix_day_to_timestamp function.
TIME_SHIELD_CONSTEXPR ts_t end_of_month(ts_t ts=time_shield::ts())
Get the last timestamp of the current month.
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.
constexpr ts_t start_of_day(ts_t ts=time_shield::ts()) noexcept
Get the start of the day timestamp.
TIME_SHIELD_CONSTEXPR ts_t year_start_ms(ts_t ts_ms=time_shield::ts_ms())
Alias for start_of_year_ms function.
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.
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_timestamp function.
TIME_SHIELD_CONSTEXPR ts_t to_timestamp(const T &date_time)
Alias for dt_to_timestamp function.
constexpr ts_ms_t ts_ms(year_t year, int month, int day)
Alias for to_timestamp_ms.
constexpr T next_day_unix_day_ms(uday_t unix_day) noexcept
Alias for start_of_next_day_from_unix_day_ms function.
constexpr T num_days_in_year_ts(ts_t ts=time_shield::ts())
Get the number of days in the current year.
constexpr ts_t week_begin(ts_t ts=time_shield::ts())
Alias for start_of_week function.
constexpr ts_t end_of_min(ts_t ts=time_shield::ts()) noexcept
Get the timestamp of the end of the minute.
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 get_unix_min function.
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.
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 get_weekday_from_ts_ms function.
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 T get_unix_year(ts_t ts) noexcept
Converts a UNIX timestamp to a 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.
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 get_unix_min function.
constexpr ts_t next_day(ts_t ts, T days=1) noexcept
Calculate the timestamp for a specified number of days in the future.
constexpr T unix_min(ts_t ts=time_shield::ts())
Alias for get_unix_min function.
constexpr T unix_day_to_timestamp_ms(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp in milliseconds.
constexpr ts_t day_start_sec(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Alias for start_of_day_sec function.
T1 to_date_time(T2 ts)
Converts a timestamp to a date-time structure.
constexpr T1 sec_to_min(T2 ts) noexcept
Converts a timestamp from seconds to minutes.
TIME_SHIELD_CONSTEXPR ts_t dt_to_timestamp_ms(const T &date_time)
Converts a date-time structure to a timestamp in milliseconds.
constexpr T unixday(ts_t ts=time_shield::ts()) noexcept
Alias for get_unix_day function.
constexpr T days(ts_t start, ts_t stop) noexcept
Alias for get_days_difference function.
constexpr T1 sec_to_hour(T2 ts) noexcept
Converts a timestamp from seconds to hours.
constexpr T1 day_of_week_date(T2 year, T3 month, T4 day)
Get the day of the week.
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.
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.
constexpr T get_days(ts_t start, ts_t stop) noexcept
Alias for get_days_difference 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 T1 get_weekday_from_date(const T2 &date)
Get the day of the week from a date structure.
constexpr ts_t hour_begin_sec(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Alias for start_of_hour_sec function.
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).
TIME_SHIELD_CONSTEXPR T year(ts_t ts=time_shield::ts())
Alias for get_year 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 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 days_in_year(T2 year) noexcept
Alias for num_days_in_year function.
constexpr ts_ms_t to_ts_ms(year_t year, int month, int day)
Alias for to_timestamp_ms.
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
Alias for unix_day_to_timestamp function.
constexpr T unix_day_to_ts_ms(uday_t unix_day) noexcept
Alias for unix_day_to_timestamp_ms function.
constexpr ts_t finish_of_hour(ts_t ts=time_shield::ts()) noexcept
Alias for end_of_hour function.
constexpr ts_t hour_begin(ts_t ts=time_shield::ts()) noexcept
Alias for start_of_hour function.
constexpr T get_weekday_from_ts(ts_t ts) noexcept
Get the weekday from a timestamp.
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 get_year_ms function.
TIME_SHIELD_CONSTEXPR T year_ms(ts_ms_t ts_ms=time_shield::ts_ms())
Alias for get_year_ms function.
constexpr T1 day_of_week_dt(const T2 &date)
Alias for get_weekday_from_date.
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 T unixday_to_ts(uday_t unix_day) noexcept
Alias for unix_day_to_timestamp function.
constexpr T next_day_unixday(uday_t unix_day) noexcept
Alias for start_of_next_day_from_unix_day function.
constexpr T get_unixday_ms(ts_ms_t t_ms=time_shield::ts_ms()) noexcept
Alias for get_unix_day_ms function.
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 T uday(ts_t ts=time_shield::ts()) noexcept
Alias for get_unix_day function.
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_t to_timestamp_ms(const T &date_time)
Alias for dt_to_timestamp_ms 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.
constexpr T1 num_days_in_year(T2 year) noexcept
Get the number of days in a given year.
TIME_SHIELD_CONSTEXPR ts_ms_t start_of_year_ms(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Get the start of the year timestamp in milliseconds.
@ FEB
February.
Definition enums.hpp:95
@ SAT
Saturday.
Definition enums.hpp:34
@ THU
Thursday.
Definition enums.hpp:32
bool is_workday(const std::string &str)
Parse an ISO8601 string and check if it falls on a workday using second precision.
TimeZoneStruct to_time_zone_struct(tz_t offset)
Converts an integer to a TimeZoneStruct.
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.