StringToTime

Überträgt eine Zeichenkette, die eine Zeitangabe oder/und ein Datum im Format "yyyy.mm.dd [hh:mi]" enthält in eine Zahl vom Typ datetime.

datetime  StringToTime(
   const string  time_string      // Zeichenkette eines Datums
   );

Parameter

time_string

[in]  Zeichenkette in einer der folgenden Formate:

  •  "yyyy.mm.dd [hh:mi]"
  •  "yyyy.mm.dd [hh:mi:ss]"
  •  "yyyymmdd [hh:mi:ss]"
  •  "yyyymmdd [hhmiss]"
  •  "yyyy/mm/dd [hh:mi:ss]"
  •  "yyyy-mm-dd [hh:mi:ss]"

Rückgabewert

Ein Wert vom Type datetime mit dem Zahlenwert der Sekunden seit dem 01.01.1970.

Hinweis

Jede Folge von Leer- und Tabulatorzeichen zwischen dem Datum und der Uhrzeit zählt als ein einzelnes Leerzeichen, sodass vor dem Aufruf von StringToTime() keine weitere Verarbeitung des time_string erforderlich ist.

 

Beispiel:

//--- Eingabeparameter
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;    // Please enter the date here as a string
 
//+------------------------------------------------------------------+
//| Skript Programm Start Funktion                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- die Umwandlung der in den Eingaben als String eingegebenen Zeit in einen Datumswert
   datetime time=StringToTime(InpDateStr);
//--- Anzeige der eingegebenen Zeichenkette und der erhaltenen Zeit im 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));
//--- Erstellen einer vertikalen Linie an der Stelle des übergebenen Zeitstempels und Verschieben des Charts an diese Stelle
   if(CreateVLine(time))
      ChartNavigateToTime(time);
   /*
  Ergebnis:
   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'
   */
  }
//+------------------------------------------------------------------+
//| Erstellen eines vertikalen Linienobjekts                         |
//+------------------------------------------------------------------+
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);
  }
//+------------------------------------------------------------------+
//| Verschieben des Charts auf die angegebene Zeit des Balkens       |
//+------------------------------------------------------------------+
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));
  }

Siehe auch

TimeToString, TimeToStruct