TimeToString

Conversione di un valore contenente tempo in secondi trascorsi dal 01.01.1970 in una stringa di formato "aaaa.mm.gg hh:mi".

string  TimeToString(
   datetime  value,                           // numero
   int       mode=TIME_DATE|TIME_MINUTES      // formato di output
   );

Parametri

valore

[in] Tempo in secondi da 00:00 del 1970/01/01.

mode=TIME_DATE|TIME_MINUTES

[in] Modalità di input dati addizionale. Può essere una flag o flag combinate:
TIME_DATE ottiene come risultato "aaaa.mm.gg",
TIME_MINUTES ottiene risultato come "hh:mi",
TIME_SECONDS ottiene risultati come "hh: mi: ss".

Valore restituito

String.

 

Esempio:

datetime ExtBarTimeOpen;
 
//+--------------------------------------------------------------------------------+
//| Custom indicator initialization function                                       |
//+--------------------------------------------------------------------------------+
int OnInit()
  {
//--- impostare il timer su un secondo
   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[])
  {
//--- ottieni l'orario di apertura della barra corrente
   ExtBarTimeOpen=time[rates_total-1];
//-- valore di ritorno di prev_calculated per la prossima chiamata
   return(rates_total);
  }
//+--------------------------------------------------------------------------------+
//| Timer function                                                                 |
//+--------------------------------------------------------------------------------+
void OnTimer()
  {
//--- imposta l'orario di apertura della barra precedente
   static datetime bar_open_time=ExtBarTimeOpen;
//--- conta il numero di secondi trascorsi dall'apertura della barra
   static int seconds=int(TimeCurrent()-ExtBarTimeOpen);
//--- se l'orario di apertura precedente non è uguale a quello corrente, allora questa è una nuova barra
//-- scrivi il nuovo orario di apertura come quello precedente e imposta i secondi a zero
   if(bar_open_time!=ExtBarTimeOpen)
     {
      bar_open_time=ExtBarTimeOpen;
      seconds=0;
     }
//-- aumentare e regolare il numero di secondi trascorsi dall'apertura della barra
   seconds++;
   if(seconds>PeriodSeconds(PERIOD_CURRENT))
      seconds=0;
//--- tempo di apertura della barra come aaaa.mm.gg hh:mi
   string bar_time_open=TimeToString(ExtBarTimeOpen);
//---ora attuale come aaaa.mm.gg hh:mi:ss
   string time_current=TimeToString(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS);
//--- numero di secondi mancanti fino all'apertura di una nuova barra
   int    sec_left=PeriodSeconds(PERIOD_CURRENT)-seconds;
//-- tempo rimanente fino alla chiusura della barra attuale come hh:mi:ss
   string time_left=TimeToString(sec_left,TIME_MINUTES|TIME_SECONDS);
//--- crea la stringa di output
   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);
//--- visualizzare l'ora di apertura della barra e l'ora corrente,
//--- il numero di secondi trascorsi da quando la barra corrente si è aperta e il rimanente fino alla chiusura e
//--- il tempo rimanente fino alla chiusura della barra attuale nel commento
   Comment(txt);
   /*
   result on 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
 
   result on 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
 
   result on 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
   
   result on 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
   */
  }

Vedi anche

StringToTime, TimeToStruct