7#ifndef __TIME_SHIELD_TIME_PARSER_MQH__
8#define __TIME_SHIELD_TIME_PARSER_MQH__
19#property copyright "Copyright 2025, NewYaroslav"
20#property link "https://github.com/NewYaroslav/time-shield-cpp"
40 if(StringLen(month)==0)
42 string month_copy = month;
44 StringConcatenate(tmp,
45 StringToUpper(StringSubstr(month_copy,0,1)),
46 StringToLower(StringSubstr(month_copy,1)));
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"};
54 if(month_copy==short_names[i] || month_copy==full_names[i])
96 if(StringLen(tz_str)==0 || tz_str==
"Z")
101 tz.
hour = (int)StringToInteger(StringSubstr(tz_str,1,2));
102 tz.
min = (int)StringToInteger(StringSubstr(tz_str,4,2));
121 string date_part=input_str;
123 int posT=StringFind(input_str,
"T");
124 if (posT<0) posT=StringFind(input_str,
" ");
126 date_part=StringSubstr(input_str,0,posT);
127 time_part=StringSubstr(input_str,posT+1);
131 int cnt = StringSplit(date_part,
'-',parts);
133 cnt = StringSplit(date_part,
'/',parts);
134 if (cnt<3) cnt = StringSplit(date_part,
'.',parts);
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]);
141 if (StringLen(time_part)>0) {
143 int zpos = StringFind(time_part,
"Z");
144 int ppos = StringFind(time_part,
"+");
145 int npos = StringFind(time_part,
"-");
147 if (zpos >= 0){ tzpos=zpos; tz_str=StringSubstr(time_part,zpos); }
148 else if(ppos >= 0){ tzpos=ppos; tz_str=StringSubstr(time_part,ppos); }
149 else if(npos > 0){ tzpos=npos; tz_str=StringSubstr(time_part,npos); }
150 if (tzpos >= 0) time_part=StringSubstr(time_part,0,tzpos);
153 int tcnt=StringSplit(time_part,
':',tparts);
156 dt.
hour = (int)StringToInteger(tparts[0]);
157 dt.
min = (int)StringToInteger(tparts[1]);
159 string sec_part=tparts[2];
160 int dot = StringFind(sec_part,
".");
162 dt.
sec = (int)StringToInteger(StringSubstr(sec_part,0,dot));
163 dt.
ms = (int)StringToInteger(StringSubstr(sec_part,dot+1));
165 dt.
sec = (int)StringToInteger(sec_part);
168 if (StringLen(tz_str)>0) {
212 long ts(
string str) {
250 const int k = StringSplit(str,
':', result);
255 int h = 0, m = 0, s = 0;
257 h = (int)StringToInteger(result[0]);
258 if (k > 1) m = (int)StringToInteger(result[1]);
259 if (k > 2) s = (int)StringToInteger(result[2]);
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.
const int64_t MONTHS_PER_YEAR
Months per year.
constexpr int64_t SEC_PER_DAY
Seconds per day.
TIME_SHIELD_CONSTEXPR const ts_t to_timestamp_ms(const T &date_time)
Alias for dt_to_timestamp_ms function.
TIME_SHIELD_CONSTEXPR ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
TIME_SHIELD_CONSTEXPR const T month_of_year(ts_t ts) noexcept
Get the month of the year.
constexpr fts_t to_ftimestamp(const T &date_time)
Alias for dt_to_ftimestamp.
constexpr const T sec_of_day(ts_t ts=ts()) noexcept
Get the second of the day.
constexpr T1 sec_to_ms(T2 ts) noexcept
Converts a timestamp from seconds to milliseconds.
TIME_SHIELD_CONSTEXPR const ts_t to_timestamp(const T &date_time)
Alias for dt_to_timestamp function.
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 parse_iso8601(const std::string &input, DateTimeStruct &dt, TimeZoneStruct &tz)
Parse a date and time string in ISO8601 format.
bool try_get_month_number(const std::string &month, T &value)
Get the month number by name, with output parameter.
T get_month_number(const std::string &month)
Get the month number by name.
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 parse_time_zone(const std::string &tz_str, TimeZoneStruct &tz)
Parse a time zone string into a TimeZoneStruct.
bool parse_tz(const std::string &tz_str, TimeZoneStruct &tz)
Alias for parse_time_zone function.
const tz_t to_offset(const TimeZoneStruct &tz)
Alias for time_zone_struct_to_offset function.
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.
const TimeZoneStruct create_time_zone_struct(int hour, int min, bool is_positive=true)
Creates a TimeZoneStruct instance.
const ts_t ts() noexcept
Get the current UTC timestamp in seconds.
const ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
const 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_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.