StringToTime

Trasforma la stringa contenente il tempo e/o la data nel formato "aaaa.mm.gg [oo:mi]" in numero di tipo datetime.

datetime  StringToTime(
   const string  time_string      // data della stringa
   );

Parametri

time_string

[in] Stringa in uno dei formati specificati:

  • "aaaa.mm.gg [oo:mi]"
  • "aaaa.mm.gg [oo:mi:ss]"
  • "aaaammgg [oo:mi:ss]"
  • "aaaammgg [oomiss]"
  • "aaaa/mm/gg [oo:mi:ss]"
  • "aaaa-mm-gg [oo:mi:ss]"

Valore di ritorno

Valore di tipo datetime contenente il numero di secondi trascorsi dal 01.01.1970.

Nota

Qualsiasi sequenza di spazi e caratteri di tabulazione tra data ed ora è considerata come uno spazio singolo per evitare un'ulteriore elaborazione di time_string prima di chiamare StringToTime().

 

Esempio:

//-- parametri di input
input group    "The date can be entered in any of the formats:"
input group    "yyyy.mm.dd [hh:mi], yyyy.mm.dd [hh:mi:ss]"
input group    "yyyymmdd [hh:mi:ss], yyyymmdd [hhmiss]"
input group    "yyyy/mm/dd [hh:mi:ss], yyyy-mm-dd [hh:mi:ss]"
input string   InpDateStr;    // Inserisci qui la data come una stringa
 
//+--------------------------------------------------------------------------------+
//| Script program start function                                                  |
//+--------------------------------------------------------------------------------+
void OnStart()
  {
//--- convertire il tempo inserito negli input come una stringa in un valore datetime
   datetime time=StringToTime(InpDateStr);
//--- visualizza la stringa inserita e il tempo ottenuto nel journal
   PrintFormat("Date entered as a string in the form '%s' is converted to datetime in the form '%s'",
               InpDateStrTimeToString(timeTIME_DATE|TIME_MINUTES|TIME_SECONDS));
//-- crea una linea verticale sulla data-ora ricevuta e sposta il grafico in questa posizione
   if(CreateVLine(time))
      ChartNavigateToTime(time);
   /*
  risultato:
   Date entered as a string in the form '' is converted to datetime in the form '1970.01.01 00:00:00'
   Date entered as a string in the form '2024is converted to datetime in the form '2024.02.24 20:24:00'
   Date entered as a string in the form '202400is converted to datetime in the form '2024.02.24 20:24:00'
   Date entered as a string in the form '20240000is converted to datetime in the form '2024.02.24 00:00:00'
   Date entered as a string in the form '2024022410is converted to datetime in the form '2030.09.06 00:00:00'
   Date entered as a string in the form '20240224 10is converted to datetime in the form '2024.02.24 10:00:00'
   Date entered as a string in the form '20240224 01is converted to datetime in the form '2024.02.24 01:00:00'
   Date entered as a string in the form '20240224 0030is converted to datetime in the form '2024.02.24 23:00:00'
   Date entered as a string in the form '20240224 0100is converted to datetime in the form '2024.02.24 01:00:00'
   */
  }
//+--------------------------------------------------------------------------------+
//| Crea un oggetto linea verticale                                                |
//+--------------------------------------------------------------------------------+
bool CreateVLine(const datetime line_time)
  {
   ResetLastError();
 
   string name=MQLInfoString(MQL_PROGRAM_NAME)+"_VLINE";
   if(!ObjectCreate(0nameOBJ_VLINE0line_time0))
     {
      Print("ObjectCreate() failed. Error code: "GetLastError());
      return(false);
     }
   ObjectSetInteger(0nameOBJPROP_STYLESTYLE_DOT);
   ObjectSetInteger(0nameOBJPROP_SELECTABLEtrue);
 
   return(true);
  }
//+--------------------------------------------------------------------------------+
//| Sposta il grafico all'orario di apertura della barra specificata|
//+--------------------------------------------------------------------------------+
bool ChartNavigateToTime(const datetime time)
  {
   ChartSetInteger(0CHART_AUTOSCROLLfalse);
   ResetLastError();
 
   int bar=iBarShift(_SymbolPERIOD_CURRENTtime);
   if(bar<0)
     {
      PrintFormat("%s: iBarShift() failed. Error code: %d"__FUNCTION__GetLastError());
      return(false);
     }
 
   long first=0;
   if(!ChartGetInteger(0CHART_FIRST_VISIBLE_BAR0first))
     {
      PrintFormat("%s: ChartGetInteger() failed. Error code: %d"__FUNCTION__GetLastError());
      return(false);
     }
 
   return(ChartNavigate(0CHART_CURRENT_POS, (int)first-bar));
  }

Guarda anche

TimeToString, TimeToStruct