跳至内容

StructToTime

StructToTime

转变结构变量 MqlDateTime日期时间 类型值,然后返回结果值。

datetime  StructToTime(
   MqlDateTime&  dt_struct      // 日期和时间结构
   );

参量

dt_struct

[in] 结构类型变量MqlDateTime。

返回值

日期时间型的值包括从01.01.1970起的每秒钟数据。

示例:

#property script_show_inputs

input int   InpYear  =  0;    // 年
input int   InpMonth =  0;    // 月
input int   InpDay   =  0;    // 日
input int   InpHour  =  0;    // 小时
input int   InpMin   =  0;    // 分钟
input int   InpSec   =  0;    // 秒

//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 调整输入值并将其写入变量
   int year =  (InpYear<1970 ?1970 : InpYear); // 如果输入的年份早于1970,则使用1970
   int month=  (InpMonth<1 ?  1  :  InpMonth > 12  ?  12 :  InpMonth);
   int day  =  (InpDay  <1 ?  1  :  InpDay   > 31  ?  31 :  InpDay);
   int hour =  (InpHour <0 ?  0  :  InpHour  > 23  ?  23 :  InpHour);
   int min  =  (InpMin  <0 ?  0  :  InpMin   > 59  ?  59 :  InpMin);
   int sec  =  (InpSec  <0 ?  0  :  InpSec   > 59  ?  59 :  InpSec);

//--- 在日志中显示输入的值
   PrintFormat("Entered date and time: %04u.%02u.%02u %02u:%02u:%02u", InpYear, InpMonth, InpDay, InpHour, InpMin, InpSec);

//--- 在日志中显示调整后的输入值
   PrintFormat("Corrected date and time: %04u.%02u.%02u %02u:%02u:%02u", year, month, day, hour, min, sec);

//--- 将输入值写入相应的结构字段
   MqlDateTime time_struct={};
   time_struct.year= year;
   time_struct.mon = month;
   time_struct.day = day;
   time_struct.hour= hour;
   time_struct.min = min;
   time_struct.sec = sec;

//--- 将结构中的日期和时间转换为datetime类型的变量,和
   datetime time = StructToTime(time_struct);

//--- 显示从MqlDateTime结构类型变量到datetime类型值的转换结果
   Print("Converted date and time: ",TimeToString(time,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
   /*
   Results if zero default values are entered:
   Entered date and time: 0000.00.00 00:00:00
   Corrected date and time: 1970.01.01 00:00:00
   Converted date and time: 1970.01.01 00:00:00

   results if the wrong day of the current year's February is entered:
   Entered date and time: 2024.02.31 00:00:00
   Corrected date and time: 2024.02.31 00:00:00
   Converted date and time: 2024.03.02 00:00:00
   */
  }