Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_parser.mqh
Go to the documentation of this file.
1//+------------------------------------------------------------------+
2//| time_parser.mqh |
3//| Time Shield - MQL5 Time Parser |
4//| Copyright 2025, NewYaroslav |
5//| https://github.com/NewYaroslav/time-shield-cpp |
6//+------------------------------------------------------------------+
7#ifndef __TIME_SHIELD_TIME_PARSER_MQH__
8#define __TIME_SHIELD_TIME_PARSER_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 <time_shield/enums.mqh>
29
30namespace time_shield {
31
35
39 int get_month_number(string month) {
40 if(StringLen(month)==0)
41 return 0;
42 string month_copy = month;
43 string tmp = "";
44 StringConcatenate(tmp,
45 StringToUpper(StringSubstr(month_copy,0,1)),
46 StringToLower(StringSubstr(month_copy,1)));
47 month_copy = tmp;
48 static const string short_names[] = {"Jan","Feb","Mar","Apr","May","Jun",
49 "Jul","Aug","Sep","Oct","Nov","Dec"};
50 static const string full_names[] = {"January","February","March","April","May","June",
51 "July","August","September","October","November","December"};
52 for(int i=0;i<MONTHS_PER_YEAR;++i)
53 {
54 if(month_copy==short_names[i] || month_copy==full_names[i])
55 return i+1;
56 }
57 return 0;
58 }
59
62 int month_of_year(string month) { return get_month_number(month); }
63
64//------------------------------------------------------------------------------
65
70 bool try_get_month_number(string month, int &value) {
71 int res = get_month_number(month);
72 if(res==0)
73 return false;
74 value = res;
75 return true;
76 }
77
80 bool get_month_number(string month, int &value) { return try_get_month_number(month,value); }
81
84 bool month_of_year(string month, int &value) { return try_get_month_number(month,value); }
85
86//------------------------------------------------------------------------------
87
94 bool parse_time_zone(string tz_str, TimeZoneStruct &tz)
95 {
96 if(StringLen(tz_str)==0 || tz_str=="Z")
97 {
98 tz.hour=0; tz.min=0; tz.is_positive=true; return true;
99 }
100 tz.is_positive = ((char)tz_str[0] =='+');
101 tz.hour = (int)StringToInteger(StringSubstr(tz_str,1,2));
102 tz.min = (int)StringToInteger(StringSubstr(tz_str,4,2));
103 return is_valid_time_zone(tz);
104 }
105
108 bool parse_tz(string tz_str, TimeZoneStruct &tz) { return parse_time_zone(tz_str, tz); }
109
110//------------------------------------------------------------------------------
111
117 bool parse_iso8601(string input_str, DateTimeStruct &dt, TimeZoneStruct &tz) {
119 tz = create_time_zone_struct(0,0);
120
121 string date_part=input_str;
122 string time_part="";
123 int posT=StringFind(input_str,"T");
124 if (posT<0) posT=StringFind(input_str," ");
125 if (posT>=0) {
126 date_part=StringSubstr(input_str,0,posT);
127 time_part=StringSubstr(input_str,posT+1);
128 }
129
130 string parts[];
131 int cnt = StringSplit(date_part,'-',parts);
132 if (cnt<3) {
133 cnt = StringSplit(date_part,'/',parts);
134 if (cnt<3) cnt = StringSplit(date_part,'.',parts);
135 }
136 if (cnt<3) return false;
137 dt.year=(long)StringToInteger(parts[0]);
138 dt.mon =(int)StringToInteger(parts[1]);
139 dt.day =(int)StringToInteger(parts[2]);
140
141 if(!is_valid_date(dt))
142 return false;
143
144 if (StringLen(time_part)>0) {
145 string tz_str = "";
146 int zpos = StringFind(time_part,"Z");
147 int ppos = StringFind(time_part,"+");
148 int npos = StringFind(time_part,"-");
149 int tzpos = -1;
150 if (zpos >= 0){ tzpos=zpos; tz_str=StringSubstr(time_part,zpos); }
151 else if(ppos >= 0){ tzpos=ppos; tz_str=StringSubstr(time_part,ppos); }
152 else if(npos > 0){ tzpos=npos; tz_str=StringSubstr(time_part,npos); }
153 if (tzpos >= 0) time_part=StringSubstr(time_part,0,tzpos);
154
155 string tparts[];
156 int tcnt=StringSplit(time_part,':',tparts);
157 if (tcnt < 2)
158 return is_valid_date_time(dt);
159 dt.hour = (int)StringToInteger(tparts[0]);
160 dt.min = (int)StringToInteger(tparts[1]);
161 if (tcnt >= 3) {
162 string sec_part=tparts[2];
163 int dot = StringFind(sec_part, ".");
164 if (dot >= 0) {
165 dt.sec = (int)StringToInteger(StringSubstr(sec_part,0,dot));
166 dt.ms = (int)StringToInteger(StringSubstr(sec_part,dot+1));
167 } else {
168 dt.sec = (int)StringToInteger(sec_part);
169 }
170 }
171 if (StringLen(tz_str)>0) {
172 if (!parse_time_zone(tz_str,tz)) return false;
173 }
174 }
175 return is_valid_date_time(dt);
176 }
177
182 bool str_to_ts(string str, long &ts) {
184 if(!parse_iso8601(str, dt, tz)) return false;
185 ts = to_timestamp(dt) + to_offset(tz);
186 return true;
187 }
188
193 bool str_to_ts_ms(string str, long &ts) {
195 if(!parse_iso8601(str, dt, tz)) return false;
197 return true;
198 }
199
203 bool is_workday(string str) {
204 long ts = 0;
205 if(!str_to_ts(str, ts))
206 return false;
207 return is_workday(ts);
208 }
209
212 bool workday(string str) { return is_workday(str); }
213
217 bool is_workday_ms(string str) {
218 long ts = 0;
219 if(!str_to_ts_ms(str, ts))
220 return false;
221 return is_workday_ms(ts);
222 }
223
226 bool workday_ms(string str) { return is_workday_ms(str); }
227
231 bool is_first_workday_of_month(string str) {
232 long ts = 0;
233 if(!str_to_ts(str, ts))
234 return false;
236 }
237
242 long ts = 0;
243 if(!str_to_ts_ms(str, ts))
244 return false;
246 }
247
252 bool is_within_first_workdays_of_month(string str, int count) {
253 long ts = 0;
254 if(!str_to_ts(str, ts))
255 return false;
257 }
258
263 bool is_within_first_workdays_of_month_ms(string str, int count) {
264 long ts = 0;
265 if(!str_to_ts_ms(str, ts))
266 return false;
268 }
269
273 bool is_last_workday_of_month(string str) {
274 long ts = 0;
275 if(!str_to_ts(str, ts))
276 return false;
278 }
279
284 long ts = 0;
285 if(!str_to_ts_ms(str, ts))
286 return false;
288 }
289
294 bool is_within_last_workdays_of_month(string str, int count) {
295 long ts = 0;
296 if(!str_to_ts(str, ts))
297 return false;
299 }
300
305 bool is_within_last_workdays_of_month_ms(string str, int count) {
306 long ts = 0;
307 if(!str_to_ts_ms(str, ts))
308 return false;
310 }
311
316 long ts = 0;
317 if(!str_to_ts(str, ts))
318 return ERROR_TIMESTAMP;
320 }
321
326 long ts = 0;
327 if(!str_to_ts_ms(str, ts))
328 return ERROR_TIMESTAMP;
330 }
331
335 long end_of_first_workday_month(string str) {
336 long ts = 0;
337 if(!str_to_ts(str, ts))
338 return ERROR_TIMESTAMP;
340 }
341
346 long ts = 0;
347 if(!str_to_ts_ms(str, ts))
348 return ERROR_TIMESTAMP;
350 }
351
356 long ts = 0;
357 if(!str_to_ts(str, ts))
358 return ERROR_TIMESTAMP;
360 }
361
366 long ts = 0;
367 if(!str_to_ts_ms(str, ts))
368 return ERROR_TIMESTAMP;
370 }
371
375 long end_of_last_workday_month(string str) {
376 long ts = 0;
377 if(!str_to_ts(str, ts))
378 return ERROR_TIMESTAMP;
380 }
381
386 long ts = 0;
387 if(!str_to_ts_ms(str, ts))
388 return ERROR_TIMESTAMP;
390 }
391
396 bool str_to_fts(string str, double &ts) {
398 if(!parse_iso8601(str, dt, tz)) return false;
399 ts = to_ftimestamp(dt) + (double)to_offset(tz);
400 return true;
401 }
402
407 long ts(string str) {
408 long v=0;
409 str_to_ts(str,v);
410 return v;
411 }
412
417 long ts_ms(string str) {
418 long v=0;
419 str_to_ts_ms(str,v);
420 return v;
421 }
422
427 double fts(string str) {
428 double v=0.0;
429 str_to_fts(str,v);
430 return v;
431 }
432
433 //--------------------------------------------------------------------------
434
443 int sec_of_day(string str) {
444 string result[];
445 const int k = StringSplit(str, ':', result);
446
447 if (k < 1 || k > 3)
448 return (int)SEC_PER_DAY;
449
450 int h = 0, m = 0, s = 0;
451
452 h = (int)StringToInteger(result[0]);
453 if (k > 1) m = (int)StringToInteger(result[1]);
454 if (k > 2) s = (int)StringToInteger(result[2]);
455
456 if (!is_valid_time(h, m, s)) return (int)SEC_PER_DAY;
457
458 return sec_of_day(h, m, s);
459 }
460
462
463}; // namespace time_shield
464
465#endif // __TIME_SHIELD_TIME_PARSER_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 ERROR_TIMESTAMP
Error timestamp value.
const int64_t MONTHS_PER_YEAR
Months per year.
constexpr int64_t SEC_PER_DAY
Seconds per day.
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_t end_of_first_workday_month(year_t year, int month) noexcept
Returns end-of-day timestamp for the first workday of month.
TIME_SHIELD_CONSTEXPR bool workday(ts_t ts) noexcept
Alias for is_workday(ts_t).
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 ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
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.
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.
TIME_SHIELD_CONSTEXPR bool workday_ms(ts_ms_t ts_ms) noexcept
Alias for is_workday(ts_ms_t).
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.
constexpr T1 sec_to_ms(T2 ts) noexcept
Converts a timestamp from seconds to milliseconds.
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.
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.
bool parse_iso8601(const char *input, std::size_t length, DateTimeStruct &dt, TimeZoneStruct &tz) noexcept
Parse ISO8601 character buffer into DateTimeStruct and TimeZoneStruct.
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 str_to_ts_ms(const std::string &str, ts_ms_t &ts)
Convert an ISO8601 string to a millisecond timestamp (ts_ms_t).
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 parse_time_zone(const char *data, std::size_t length, TimeZoneStruct &tz) noexcept
Parse timezone character buffer into TimeZoneStruct.
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 try_get_month_number(const std::string &month, T &value)
Try get the month number by name, with output parameter.
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 parse_tz(const std::string &tz_str, TimeZoneStruct &tz) noexcept
Alias for parse_time_zone.
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).
T get_month_number(const std::string &month)
Get the month number by name (throwing).
bool str_to_ts(const std::string &str, ts_t &ts)
Convert an ISO8601 string to a timestamp (ts_t).
bool str_to_fts(const std::string &str, fts_t &ts)
Convert an ISO8601 string to a floating-point timestamp (fts_t).
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).
bool is_workday_ms(const std::string &str)
Parse ISO8601 string and check if it falls on a workday (milliseconds precision).
TIME_SHIELD_CONSTEXPR tz_t to_offset(const TimeZoneStruct &tz) noexcept
Alias for time_zone_struct_to_offset.
TIME_SHIELD_CONSTEXPR T month_of_year(ts_t ts) noexcept
Get the month of the year.
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 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.
const DateTimeStruct create_date_time_struct(int64_t year, int mon=1, int day=1, int hour=0, int min=0, int sec=0, int ms=0)
Creates a DateTimeStruct instance.
TimeZoneStruct create_time_zone_struct(int hour, int min, bool is_positive=true)
Creates a TimeZoneStruct instance.
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.
ts_t ts() noexcept
Get the current UTC timestamp in seconds.
ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
fts_t fts() noexcept
Get the current UTC timestamp in floating-point seconds.
TIME_SHIELD_CONSTEXPR bool is_valid_time_zone(T hour, T min) noexcept
Check if the time zone is valid.
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_valid_time(T1 hour, T1 min, T1 sec, T2 ms=0) noexcept
Checks the correctness of the specified time.
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.
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.
int hour
Hour component of time (0-23)
int min
Minute component of time (0-59)
bool is_positive
True if the time zone offset is positive, false if negative.
Header with helper functions for converting between different time representations in MQL5.
Header for time zone structure and related functions (MQL5).
Header with validation functions for dates, times, and timestamps.