This is getting me crazy!(Datetimes)

 

Hello I have a .csv with times as strings in the format:

"2018-04-10 15:36:00 UTC"

I want to extract the time and save it thats it


// Save the string correctly on "var_string"
// Extracting the "UTC" part from the string
string aux_string=StringSubstr(var_string,0,StringLen(var_string)-3);
// Wanting to put the date in a datetime variables
datetime time_var=StringToTime(aux_string);
// ....
                                


After doing this the new variables created comes with 2 hours more!! and thats exactly because I have at the moment GMT+2 in my machine! if I change my timezone, the numbers of hours substracted added change accordingly. What is going on!?

 
Luis Alejandro Diaz Vidal:

Hello I have a .csv with times as strings in the format:

"2018-04-10 15:36:00 UTC"

I want to extract the time and save it thats it



After doing this the new variables created comes with 2 hours more!! and thats exactly because I have at the moment GMT+2 in my machine! if I change my timezone, the numbers of hours substracted added change accordingly. What is going on!?

You have to make your own function to parse the date-string.

void OnStart()
{
   string excel_date = "2018-04-10 15:36:00 UTC";
   Alert(TimeToString(parseExcelDateString(excel_date)));
}

datetime parseExcelDateString(const string date)
{
   MqlDateTime res;
   res.year = (int)StringSubstr(date,0,4);
   res.mon = (int)StringSubstr(date,5,2);
   res.day = (int)StringSubstr(date,8,2);
   res.hour = (int)StringSubstr(date,11,2);
   res.min = (int)StringSubstr(date,14,2);
   res.sec = (int)StringSubstr(date,17,2);
   return StructToTime(res);
}
 
nicholishen:

You have to make your own function to parse the date-string.

Yeah I did that, but why that was happening?

 
Luis Alejandro Diaz Vidal:

Yeah I did that, but why that was happening?

Because MQL and excel parse their date strings differently.  They aren't compatible. 

 
Luis Alejandro Diaz Vidal:

Yeah I did that, but why that was happening?

You should check the documentation :

The function converts a string containing time or date in "yyyy.mm.dd [hh:mi]" format into datetime type.

Your CSV string is using '-' separator, so the conversion function can't recognize it.

StringToTime - Conversion Functions - MQL4 Reference
StringToTime - Conversion Functions - MQL4 Reference
  • docs.mql4.com
StringToTime - Conversion Functions - MQL4 Reference