Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
validation.mqh
Go to the documentation of this file.
1//+------------------------------------------------------------------+
2//| validation.mqh |
3//| Time Shield - MQL5 Validation Functions |
4//| Copyright 2025, NewYaroslav |
5//| https://github.com/NewYaroslav/time-shield-cpp |
6//+------------------------------------------------------------------+
7#ifndef __TIME_SHIELD_VALIDATION_MQH__
8#define __TIME_SHIELD_VALIDATION_MQH__
9
16
17#property copyright "Copyright 2025, NewYaroslav"
18#property link "https://github.com/NewYaroslav/time-shield-cpp"
19#property strict
20
21#include "constants.mqh"
22#include "enums.mqh"
23#include "time_struct.mqh"
24#include "date_struct.mqh"
25#include "date_time_struct.mqh"
26#include "time_zone_struct.mqh"
27
28namespace time_shield {
29
33
37 bool is_leap_year_date(const long year) {
38 return ((year & 3) == 0 && ((year % 25) != 0 || (year & 15) == 0));
39 }
40
43 bool check_leap_year(const long year) { return is_leap_year_date(year); }
44
47 bool leap_year(const long year) { return is_leap_year_date(year); }
48
49 //----------------------------------------------------------------------
50
54 bool is_leap_year_ts(const long ts) {
55 const long BIAS_292277022000 = 9223371890843040000LL;
56 const long BIAS_2000 = 946684800LL;
57
58 long y = MAX_YEAR;
59 long secs = -((ts - BIAS_2000) - BIAS_292277022000);
60
61 long n_400_years = secs / SEC_PER_400_YEARS;
62 secs -= n_400_years * SEC_PER_400_YEARS;
63 y -= n_400_years * 400;
64
65 long n_100_years = secs / SEC_PER_100_YEARS;
66 secs -= n_100_years * SEC_PER_100_YEARS;
67 y -= n_100_years * 100;
68
69 long n_4_years = secs / SEC_PER_4_YEARS;
70 secs -= n_4_years * SEC_PER_4_YEARS;
71 y -= n_4_years * 4;
72
73 long n_1_years = secs / SEC_PER_YEAR;
74 secs -= n_1_years * SEC_PER_YEAR;
75 y -= n_1_years;
76
77 y = secs == 0 ? y : y - 1;
78 return is_leap_year_date(y);
79 }
80
83 bool leap_year_ts(const long ts) { return is_leap_year_ts(ts); }
84
87 bool check_leap_year_ts(const long ts) { return is_leap_year_ts(ts); }
88
91 bool is_leap_year(const long ts) { return is_leap_year_ts(ts); }
92
93 //----------------------------------------------------------------------
94
99 bool is_valid_time_zone(const int hour, const int min) {
100 if (hour < 0 || hour > 23) return false;
101 if (min < 0 || min > 59) return false;
102 return true;
103 }
104
107 bool is_valid_tz(const int hour, const int min) {
108 return is_valid_time_zone(hour, min);
109 }
110
115 return is_valid_time_zone(time_zone.hour, time_zone.min);
116 }
117
120 bool is_valid_time_zone(const TimeZoneStruct &time_zone) {
121 return is_valid_time_zone_offset(time_zone);
122 }
123
126 bool is_valid_tz(const TimeZoneStruct &time_zone) {
127 return is_valid_time_zone_offset(time_zone);
128 }
129
130 //----------------------------------------------------------------------
131
138 bool is_valid_time(const int hour, const int min, const int sec, const int ms = 0) {
139 if (hour < 0 || hour > 23) return false;
140 if (min < 0 || min > 59) return false;
141 if (sec < 0 || sec > 59) return false;
142 if (ms < 0 || ms > 999) return false;
143 return true;
144 }
145
149 bool is_valid_time(const TimeStruct &time) {
150 return is_valid_time(time.hour, time.min, time.sec, time.ms);
151 }
152
158 bool is_valid_date(const long year, const int month, const int day) {
159 if (day > 31 && year <= 31)
160 return is_valid_date((long)day, month, (int)year);
161 if (year > MAX_YEAR) return false;
162 if (month < 1 || month > 12) return false;
163 if (day < 1 || day > 31) return false;
164 if (month == FEB) {
165 bool leap = is_leap_year_date(year);
166 if (leap && day > 29) return false;
167 if (!leap && day > 28) return false;
168 } else {
169 switch(month) {
170 case 4:
171 case 6:
172 case 9:
173 case 11:
174 if (day > 30) return false;
175 break;
176 default:
177 break;
178 }
179 }
180 return true;
181 }
182
186 bool is_valid_date(const DateStruct &date) {
187 return is_valid_date(date.year, date.mon, date.day);
188 }
189
200 const long year,
201 const int month,
202 const int day,
203 const int hour = 0,
204 const int min = 0,
205 const int sec = 0,
206 const int ms = 0) {
207 if (!is_valid_date(year, month, day)) return false;
208 if (!is_valid_time(hour, min, sec, ms)) return false;
209 return true;
210 }
211
215 bool is_valid_date_time(const DateTimeStruct &date_time) {
216 if (!is_valid_date(date_time.year, date_time.mon, date_time.day)) return false;
217 if (!is_valid_time(date_time.hour, date_time.min, date_time.sec, date_time.ms)) return false;
218 return true;
219 }
220
221 //----------------------------------------------------------------------
222
226 bool is_day_off(const long ts) {
227 int wd = (int)((ts / SEC_PER_DAY + THU) % DAYS_PER_WEEK);
228 return (wd == SUN || wd == SAT);
229 }
230
233 bool is_weekend(const long ts) { return is_day_off(ts); }
234
238 bool is_day_off_unix_day(const long unix_day) {
239 int wd = (int)((unix_day + THU) % DAYS_PER_WEEK);
240 return (wd == SUN || wd == SAT);
241 }
242
245 bool is_weekend_unix_day(const long unix_day) {
247 }
248
250
251}; // namespace time_shield
252
253#endif // __TIME_SHIELD_VALIDATION_MQH__
Header file with time-related constants.
Header for date structure and related functions (MQL5).
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 DAYS_PER_WEEK
Days per week.
constexpr int64_t SEC_PER_YEAR
Seconds per year (365 days)
Definition constants.hpp:77
constexpr int64_t SEC_PER_100_YEARS
Seconds per 100 years.
Definition constants.hpp:82
constexpr int64_t MAX_YEAR
Maximum representable year.
constexpr int64_t SEC_PER_400_YEARS
Seconds per 400 years.
Definition constants.hpp:83
constexpr int64_t SEC_PER_DAY
Seconds per day.
Definition constants.hpp:76
constexpr int64_t SEC_PER_4_YEARS
Seconds per 4 years.
Definition constants.hpp:80
constexpr T unix_day(ts_t ts=time_shield::ts()) noexcept
Alias for get_unix_day function.
TIME_SHIELD_CONSTEXPR ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
TIME_SHIELD_CONSTEXPR const T year(ts_t ts=ts())
Alias for get_year function.
@ FEB
February.
Definition enums.hpp:94
@ SUN
Sunday.
Definition enums.hpp:27
@ SAT
Saturday.
Definition enums.hpp:33
@ THU
Thursday.
Definition enums.hpp:31
TIME_SHIELD_CONSTEXPR bool is_valid_time_zone_offset(const T &time_zone) noexcept
Check if the time zone is valid.
TIME_SHIELD_CONSTEXPR bool is_day_off(ts_t ts) noexcept
Check if a given timestamp corresponds to a weekend day (Saturday or Sunday).
constexpr const 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_t ts)
Alias for is_leap_year_ts function.
TIME_SHIELD_CONSTEXPR bool is_valid_tz(T hour, T min)
Alias for is_valid_time_zone function.
TIME_SHIELD_CONSTEXPR bool is_valid_time_zone(T hour, T min) noexcept
Check if the time zone is valid.
constexpr const bool check_leap_year(T year) noexcept
Alias for is_leap_year_date function.
TIME_SHIELD_CONSTEXPR bool is_valid_date(T1 year, T2 month, T2 day) noexcept
Checks the correctness of the specified date.
TIME_SHIELD_CONSTEXPR bool is_weekend(ts_t ts) noexcept
Alias for is_day_off function.
TIME_SHIELD_CONSTEXPR bool is_valid_time(T1 hour, T1 min, T1 sec, T2 ms=0) noexcept
Checks the correctness of the specified time.
constexpr const bool leap_year(T year) noexcept
Alias for is_leap_year_date function.
TIME_SHIELD_CONSTEXPR bool leap_year_ts(ts_t ts)
Alias for is_leap_year_ts function.
TIME_SHIELD_CONSTEXPR bool is_valid_date_time(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0, T3 ms=0) noexcept
Checks the correctness of a date and time.
TIME_SHIELD_CONSTEXPR bool is_weekend_unix_day(T unix_day) noexcept
Alias for is_day_off_unix_day function.
TIME_SHIELD_CONSTEXPR bool is_day_off_unix_day(T unix_day) noexcept
Check if a given day (since Unix epoch) corresponds to a weekend day (Saturday or Sunday)....
TIME_SHIELD_CONSTEXPR bool check_leap_year_ts(ts_t ts)
Alias for is_leap_year_ts function.
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 a date.
int32_t mon
Month component of the date (1-12).
int32_t day
Day component of the date (1-31).
int64_t year
Year component of the date.
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.
int16_t ms
Millisecond component of time (0-999)
int16_t hour
Hour component of time (0-23)
int16_t sec
Second component of time (0-59)
int16_t min
Minute component of time (0-59)
Structure to represent time zone information.
int hour
Hour component of time (0-23)
int min
Minute component of time (0-59)
Header for time structure and related functions (MQL5).
Header for time zone structure and related functions (MQL5).