TimeToString

Converte um valor contendo hora em segundos decorridos deste 01.01.1970 em uma string de formato "yyyy.mm.dd hh:mi".

string  TimeToString(
   datetime  value,                           // número
   int       mode=TIME_DATE|TIME_MINUTES      // formato de saída
   );

Parâmetros

value

[in]  Hora em segundos a partir de 00:00 1970/01/01.

mode=TIME_DATE|TIME_MINUTES

[in] Modo de entrada de dados adicionais. Pode ser um ou sinalizador combinado:
TIME_DATE obtém como resultado "yyyy.mm.dd",
TIME_MINUTES obtém como resultado "hh:mi",
TIME_SECONDS obêm resultados como "hh:mi:ss".

Valor do Retorno

String.

 

Exemplo:

datetime ExtBarTimeOpen;
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- definimos valor do cronômetro como um 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[])
  {
//--- obtemos o tempo de abertura da barra atual
   ExtBarTimeOpen=time[rates_total-1];
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//--- registramos o tempo de abertura da barra anterior
   static datetime bar_open_time=ExtBarTimeOpen;
//--- calculamos a quantidade de segundos que passaram desde a abertura da barra
   static int seconds=int(TimeCurrent()-ExtBarTimeOpen);
//--- se o tempo de abertura anterior não é igual ao atual, então é uma nova barra
//--- registramos o novo tempo de abertura como o anterior e zeramos os segundos
   if(bar_open_time!=ExtBarTimeOpen)
     {
      bar_open_time=ExtBarTimeOpen;
      seconds=0;
     }
//--- incrementamos e ajustamos a quantidade de segundos passados desde a abertura da barra
   seconds++;
   if(seconds>PeriodSeconds(PERIOD_CURRENT))
      seconds=0;
//--- descrição do tempo de abertura da barra no formato yyyy.mm.dd hh:mi
   string bar_time_open=TimeToString(ExtBarTimeOpen);
//--- descrição do tempo atual no formato yyyy.mm.dd hh:mi:ss
   string time_current=TimeToString(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS);
//--- quantidade de segundos restantes até a abertura da próxima barra
   int    sec_left=PeriodSeconds(PERIOD_CURRENT)-seconds;
//--- descrição do tempo restante até o fechamento da barra atual no formato hh:mm:ss
   string time_left=TimeToString(sec_left,TIME_MINUTES|TIME_SECONDS);
//--- criamos a string de saída
   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);
//--- exibimos no comentário do gráfico o tempo de abertura da barra, o tempo atual,
//--- a quantidade de segundos que passaram desde a abertura da barra atual e os restantes até o seu fechamento, 
//--- o tempo restante até o fechamento da barra atual
   Comment(txt);
   /*
   resultado em 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 em 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 em 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 em 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
   */
  }

Também Veja

StringToTime, TimeToStruct