StringToTime

Transforme la chaîne de caracères contenant une heure et/ou unde date au format "aaaa.mm.jj [hh:mi]" en un nombre de type datetime.

datetime  StringToTime(
   const string  time_string      // chaîne de caractères contenant une date
   );

Paramètres

time_string

[in]  Chaîne de caractères dans un des formats suivant :

  • "aaaa.mm.jj [hh:mi]"
  • "aaaa.mm.jj [hh:mi:ss]"
  • "aaaammjj [hh:mi:ss]"
  • "aaaammjj [hhmiss]"
  • "aaaa/mm/jj [hh:mi:ss]"
  • "aaaa-mm-jj [hh:mi:ss]"

Valeur de Retour

Valeur de type datetime contenant le nombre de secondes passées depuis le 01.01.1970.

Note

Les caractères espace et tabulation situés entre la date et l'heure sont considérés comme un seul espace pour éviter tout traitement supplémentaire du paramètre time_string avant d'appeler StringToTime().

 

Exemple :

//--- paramètres d'entrée
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;    // Veuillez saisir la date ici sous forme de chaîne
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- convertit l'heure saisie dans les entrées sous forme de chaîne en une valeur datetime
   datetime time=StringToTime(InpDateStr);
//--- affiche la chaîne saisie et l'heure obtenue dans le 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));
//--- crée une ligne verticale sur la date-heure reçue et déplace le graphique vers cet emplacement
   if(CreateVLine(time))
      ChartNavigateToTime(time);
   /*
  résultat :
   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'
   */
  }
//+------------------------------------------------------------------+
//| Crée un objet ligne 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);
  }
//+------------------------------------------------------------------+
//| Déplace le graphique à l'heure d'ouverture de la barre spécifiée |
//+------------------------------------------------------------------+
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));
  }

Voir aussi

TimeToString, TimeToStruct