TimeToString

Conversión del valor que contiene el tiempo en segundos transcurridos desde el 01.01.1970 a la cadena con el formato "yyyy.mm.dd hh:mi".

string  TimeToString(
   datetime  value,                           // número
   int       mode=TIME_DATE|TIME_MINUTES      // formato output
   );

Parámetros

value

[in]  Tiempo en segundos de 00:00 1 de Enero de 1970.

mode=TIME_DATE|TIME_MINUTES

[in]  Modo adicional del output de datos. Puede ser una bandera o bandera combinada:
TIME_DATE obtiene resultado en formato " yyyy.mm.dd " ,
TIME_MINUTES obtiene resultado en formato " hh:mi " ,
TIME_SECONDS obtiene resultado en formato " hh:mi:ss ".

Valor devuelto

Una cadena.

 

Ejemplo:

datetime ExtBarTimeOpen;
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- establecemos el temporizador en un segundo
   EventSetTimer(1);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Comment("");
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//--- obtenemos la hora de apertura de la barra actual
   ExtBarTimeOpen=time[rates_total-1];
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//--- anotamos la hora anterior de apertura de la barra
   static datetime bar_open_time=ExtBarTimeOpen;
//--- calculamos el número de segundos transcurridos desde la apertura de la barra
   static int seconds=int(TimeCurrent()-ExtBarTimeOpen);
//--- si la hora anterior de apertura de la barra no es igual a la actual, significa que tenemos una nueva barra
//--- anotamos la nueva hora de apertura como la anterior y ponemos los segundos a cero
   if(bar_open_time!=ExtBarTimeOpen)
     {
      bar_open_time=ExtBarTimeOpen;
      seconds=0;
     }
//--- aumentamos y corregimos el número de segundos transcurridos desde el momento de la apertura de la barra
   seconds++;
   if(seconds>PeriodSeconds(PERIOD_CURRENT))
      seconds=0;
//--- descripción de la hora de apertura de la barra en formato yyyy.mm.dd hh:mi
   string bar_time_open=TimeToString(ExtBarTimeOpen);
//--- descripción de la barra actual en formato yyyy.mm.dd hh:mi:ss
   string time_current=TimeToString(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS);
//--- número de segundos restante hasta la apertura de una nueva barra
   int    sec_left=PeriodSeconds(PERIOD_CURRENT)-seconds;
//--- descripción de la cantidad de tiempo restante hasta la apertura de una nueva barra en el formato hh:mm:ss
   string time_left=TimeToString(sec_left,TIME_MINUTES|TIME_SECONDS);
//--- creamos una línea de introducción
   string txt=StringFormat("Opening time of the current bar: %s\n"+
                           "Time Current: %s\n"+
                           "Seconds have passed since the bar opened: %d\n"+
                           "Approximately seconds left before bar closes: %d\n"+
                           "Time remaining until bar closes: %s",bar_time_open,time_current,seconds,sec_left,time_left);
//--- mostramos la hora de apertura de la barra, la hora actual en el comentario del gráfico,
//--- el número de segundos transcurridos desde la apertura de la barra actual y los que quedan hasta su cierre, y
//--- la hora hasta el cierre de la barra actual
   Comment(txt);
   /*
   resultado en M1:
   Opening time of the current bar2024.02.22 18:06
   Time Current2024.02.22 18:06:24
   Seconds have passed since the bar opened25
   Approximately seconds left before bar closes35
   Time remaining until bar closes00:00:35
 
   resultado en M5:
   Opening time of the current bar2024.02.22 18:05
   Time Current2024.02.22 18:07:28
   Seconds have passed since the bar opened149
   Approximately seconds left before bar closes151
   Time remaining until bar closes00:02:31
 
   resultado en H1:
   Opening time of the current bar2024.02.22 18:00
   Time Current2024.02.22 18:08:13
   Seconds have passed since the bar opened494
   Approximately seconds left before bar closes3106
   Time remaining until bar closes00:51:46
   
   resultado en D1:
   Opening time of the current bar2024.02.22 00:00
   Time Current2024.02.22 18:11:01
   Seconds have passed since the bar opened65462
   Approximately seconds left before bar closes20938
   Time remaining until bar closes05:48:58
   */
  }

Véase también

StringToTime, TimeToStruct