//+------------------------------------------------------------------+ //| FormatFunctions.mqh | //| Copyright 2018, Orangetree | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2018, Orangetree" #property link "https://www.mql5.com" //--- Enumerations----------------------------------------------+ enum DATE { yyyycmmcdd, // yyyy.mm.dd yyyymmdd, // yyyymmdd yymmdd, // yymmdd ddmmyy, // ddmmyy ddslmmslyy, // dd/mm/yy mmslddslyy // mm/dd/yy }; enum TIME { hhmmss, // hhmmss hhmm, // hhmm hhdmmdss, // hh:mm:ss hhdmm // hh:mm }; enum CODE { ansi =FILE_ANSI, // ANSI strings unicode =FILE_UNICODE // UNICODE strings }; enum DELIMITER { comma, // comma tab, // tab space, // space semicolon // semicolon }; //-------------------------------------------------------------------+ //---Date formatting-------------------------------------------------+ //2017.01.02 string DateFormat(string str,DATE date) { string res=""; string yy=""; switch(date) { case yyyycmmcdd : //or format res =str; if(StringLen(res)!=10)res=""; // checking date format case yyyymmdd : res =StringSubstr(str,0,4)+"."+StringSubstr(str,4,2)+"."+StringSubstr(str,6,2); if(StringLen(res)!=10)res=""; // checking date format break; case yymmdd : yy =StringSubstr(str,0,2); if(StringToInteger(yy)>=70) yy ="19"+yy; else yy ="20"+yy; res =yy+"."+StringSubstr(str,2,2)+"."+StringSubstr(str,4,2); if(StringLen(res)!=10)res=""; // checking date format break; case ddmmyy : yy =StringSubstr(str,4,2); if(StringToInteger(yy)>=70) yy ="19"+yy; else yy ="20"+yy; res =yy+"."+StringSubstr(str,2,2)+"."+StringSubstr(str,0,2); if(StringLen(res)!=10)res=""; // checking date format break; case ddslmmslyy : yy =StringSubstr(str,6,2); if(StringToInteger(yy)>=70) yy ="19"+yy; else yy ="20"+yy; res =yy+"."+StringSubstr(str,3,2)+"."+StringSubstr(str,0,2); if(StringLen(res)!=10)res=""; // checking date format break; case mmslddslyy : yy =StringSubstr(str,6,2); if(StringToInteger(yy)>=70) yy ="19"+yy; else yy ="20"+yy; res =yy+"."+StringSubstr(str,0,2)+"."+StringSubstr(str,3,2); if(StringLen(res)!=10)res=""; // checking date format break; default : break; } return res; } //-------------------------------------------------------------------+ //---Time formatting-------------------------------------------------+ //17:00:00 string TimeFormat(string str,TIME time) { string res=""; switch(time) { case hhdmmdss : // our format res =str; if(StringLen(res)!=8) res=""; //checking time format break; case hhdmm : res =str+":00"; if(StringLen(res)!=8) res=""; //checking time format break; case hhmmss : res =StringSubstr(str,0,2)+":"+StringSubstr(str,2,2)+":"+StringSubstr(str,4,2); if(StringLen(res)!=8) res=""; //checking time format case hhmm : res =StringSubstr(str,0,2)+":"+StringSubstr(str,2,2)+":00"; if(StringLen(res)!=8) res=""; //checking time format default : break; } return res; } //---Searching for data position boundaries--------------------------+ bool SearchBorders(string str,int pos,int &a,int &b,DELIMITER delim) { // Auxiliary variables int left=0,right=0; int count=0; int start=0; string delimiter=""; //-------------------------------------------------------------------+ switch(delim) { case comma : delimiter =","; break; case tab : delimiter ="\t"; break; case space : delimiter =" "; break; case semicolon : delimiter =";"; break; } while(count!=pos||right!=-1) { right =StringFind(str,delimiter,start); if(right==-1&&count==0){Print("Wrong date");return false;} //Некорректные данные if(right==-1) { right =StringLen(str)-1; a =left; b =right; break; } count++; if(count==pos) { a =left; b =right-1; return true; } left =right+1; start =left; } return true; }