Search code examples
stringmql4

How to grab a time from a string of text in MQL4?


Trying to grab a time-value from a text of string in MQL4.

The string

string start_time = "From 21:17 03-02-2016 GMT Till 01:17 04-02-2016 GMT";

The code I've tried

string EndDate = StrToTime( StringSubstr( start_time,
                                          StringFind( start_time,
                                                      "Till "
                                                      )
                                          + 5,
                                          ( StringFind( start_time,
                                                        "GMT",
                                                        StringFind( start_time,
                                                                    "Till "
                                                                    )
                                                        + 5
                                                        ) - 1
                                            )
                                          - StringFind( start_time,
                                                        "Till "
                                                        )
                                          - 2
                                          )
                            );
datetime exptime = EndDate;

The current output is as shown

2016.02.03 01:17

It is grabbing the From date not the Till date.

Is there an easier way to do this? Or a way anyone knows how to fix it?

Any help would be much appreciated!


Solution

  • Well, let's go step by step:


    Grabbing correctly the "From"-part:

    int startPTR = StringFind( start_time, // __________ "From 21:17 03-02-2016 GMT ...
                               "From "     // __________ |From |.....
                               )           //  points at |F    ^
                               + 5;        //  points    |>>>>5|B after "From " starts ...
    
                                           //             0         1         2
                                           //             .123456789.123456789.123
    int stopPTR  = StringFind( start_time, // __________ "From 21:17 03-02-2016 GMT ...
                               " GMT "     // ________________________________| GMT |..
                               );          // points at _______________________^
    
    string FromTIME = StringSubstring( start_time, // __________ "From 21:17 03-02-2016 GMT ...
                                       startPTR,   // [ 5]
                                       stopPTR     // [21]
                                       - startPTR  //-[ 5]
                                       );          //_________________|21:17 03-02-2016|
    

    Correct conversion from string to datetime

    One shall also check the MQL4 syntax details, as it reports:

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

    Principally the same applies to StrToTime() function. If you are in doubts, why New-MQL4 does have two different functions for the same thing, do not ask me, ask MetaQuotes, Inc.

    So the FromTIME string will finally have to be a little bit re-shuffled so as to meet the stardard StringToTime() function requirements.

    datetime Exp = StrToTime(  StringConcatenate( 
                            /* YYYY            */ StringSubstring( FromTIME, 12, 4 ),
                            /* .               */ ".",
                            /* MM              */ StringSubstring( FromTIME,  9, 2 ),
                            /* .               */ ".",
                            /* DD              */ StringSubstring( FromTIME,  6, 2 ),
                            /* _               */ " ",
                            /* HH:MM           */ StringSubstring( FromTIME,  0, 5 )
                                                  )
                               );