TimeToString

1970年1月1日からの経過秒を含む値を「yyyy.mm.dd hh:mi 」フォーマットの文字列に変換します。

string  TimeToString(
  datetime  value,                          // 数
  int      mode=TIME_DATE|TIME_MINUTES      // 出力フォーマット
  );

パラメータ

value

[in]  01.01.1970 から経った秒数

mode=TIME_DATE|TIME_MINUTES

[[in] 任意のデータ入力モード。フラグまたはフラグの組み合わせです。
TIME_DATE の結果は "yyyy.mm.dd"、
TIME_MINUTES の結果は "hh:mi"、
TIME_SECONDS の結果は "hh:mi:ss" です。

戻り値

文字列

 

例:

datetime ExtBarTimeOpen;
 
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数                                           |
//+------------------------------------------------------------------+
int OnInit()
 {
//--- タイマーを1秒に設定する
  EventSetTimer(1);
//---
  return(INIT_SUCCEEDED);
 }
//+------------------------------------------------------------------+
//| カスタム指標を初期化解除する関数                                        |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
 {
  Comment("");
 }
//+------------------------------------------------------------------+
//| カスタム指標の反復関数                                                |
//+------------------------------------------------------------------+
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[])
 {
//--- 現在のバーが開いた時間を取得する
  ExtBarTimeOpen=time[rates_total-1];
//--- 次の呼び出しのためにprev_calculatedの値を返す
  return(rates_total);
 }
//+------------------------------------------------------------------+
//| Timer関数                                                         |
//+------------------------------------------------------------------+
void OnTimer()
 {
//--- 前のバーが開いた時間を取得する
  static datetime bar_open_time=ExtBarTimeOpen;
//--- バーが開いてから経過した秒数をカウントする
  static int seconds=int(TimeCurrent()-ExtBarTimeOpen);
//--- 前回の開始時間が現在の開始時間と異なる場合、これは新しいバーである
//--- 新しい開始時刻を前の時刻と同じように書き込み、秒をゼロに設定する
  if(bar_open_time!=ExtBarTimeOpen)
    {
    bar_open_time=ExtBarTimeOpen;
    seconds=0;
    }
//--- バーが開いてから経過した秒数を増やして調整する
  seconds++;
  if(seconds>PeriodSeconds(PERIOD_CURRENT))
    seconds=0;
//--- yyyy.mm.dd hh:miとしてのバーの開いた時刻
  string bar_time_open=TimeToString(ExtBarTimeOpen);
//--- yyyy.mm.dd hh:miとしての現在時刻
  string time_current=TimeToString(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS);
//--- 新しいバーが開くまでの残り秒数
  int   sec_left=PeriodSeconds(PERIOD_CURRENT)-seconds;
/--- 現在のバーが閉じるまでの残り時間(hh:mm:ss)
  string time_left=TimeToString(sec_left,TIME_MINUTES|TIME_SECONDS);
//--- 出力文字列を作成する
  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);
//--- バーの開いた時刻と現在時刻、
//--- 現在のバーが開いてから閉じるまでの経過秒数、
//--- コメント内の現在のバーが閉じるまでの残り時間を表示する
  Comment(txt);
  /*
  result on M1:
  Opening time of the current bar: 2024.02.22 18:06
  Time Current: 2024.02.22 18:06:24
  Seconds have passed since the bar opened: 25
  Approximately seconds left before bar closes: 35
  Time remaining until bar closes: 00:00:35
 
  result on M5:
  Opening time of the current bar: 2024.02.22 18:05
  Time Current: 2024.02.22 18:07:28
  Seconds have passed since the bar opened: 149
  Approximately seconds left before bar closes: 151
  Time remaining until bar closes: 00:02:31
 
  result on H1:
  Opening time of the current bar: 2024.02.22 18:00
  Time Current: 2024.02.22 18:08:13
  Seconds have passed since the bar opened: 494
  Approximately seconds left before bar closes: 3106
  Time remaining until bar closes: 00:51:46
 
  result on D1:
  Opening time of the current bar: 2024.02.22 00:00
  Time Current: 2024.02.22 18:11:01
  Seconds have passed since the bar opened: 65462
  Approximately seconds left before bar closes: 20938
  Time remaining until bar closes: 05:48:58
  */
 }

参照

StringToTimeTimeToStruct