Art12900_Structs/TestMqlDateTime.mq5

265 行
24 KiB
MQL5

2026-03-01 22:12:41 +07:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| TestMqlDateTime.mq5 |
//| Copyright 2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
MqlDateTime dt;
TimeCurrent(dt);
MqlDateTimePrint(dt,true);
MqlDateTimePrint(dt,false,14,2);
2026-03-01 22:12:41 +07:00
/* @8<5@ 2K2>40:
GBPUSD H1 [00] Wed, July 19, 2023, 02:00:00
GBPUSD H1 [01] Wed, July 19, 2023, 03:00:00
GBPUSD H1 [02] Wed, July 19, 2023, 04:00:00
GBPUSD H1 [03] Wed, July 19, 2023, 05:00:00
GBPUSD H1 [04] Wed, July 19, 2023, 06:00:00
GBPUSD H1 [05] Wed, July 19, 2023, 07:00:00
GBPUSD H1 [06] Wed, July 19, 2023, 08:00:00
GBPUSD H1 [07] Wed, July 19, 2023, 09:00:00
GBPUSD H1 [08] Wed, July 19, 2023, 10:00:00
GBPUSD H1 [09] Wed, July 19, 2023, 11:00:00
*/
/* @8<5@ 2K2>40:
Wed, July 19, 2023, 09:33:56
Year: 2023
Month: 07 (July)
Day: 19
Hour: 09
Minutes: 33
Seconds: 56
Day of week: Wednesday (3)
Day of year: 199
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B =08<5=>20=85 4=O =545;8 |
//+------------------------------------------------------------------+
string DayWeek(MqlDateTime &date_time)
{
//--- ?@545;O5< =0720=85 4=O =545;8
string dw=EnumToString((ENUM_DAY_OF_WEEK)date_time.day_of_week);
//--- @5>1@07C5< 2A5 ?>;CG5==K5 A8<2>;K 2 =86=89 @538AB@ 8 70<5=O5< ?5@2CN 1C:2C A <0;5=L:>9 =0 703;02=CN
if(dw.Lower())
dw.SetChar(0,ushort(dw.GetChar(0)-0x20));
//--- >72@0I05< ?>;CG5==CN AB@>:C
return dw;
/* @8<5@ 2K2>40:
Wednesday
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B =08<5=>20=85 <5AOF0 |
//+------------------------------------------------------------------+
string Month(MqlDateTime &date_time)
{
//--- ?@545;O5< =0720=85 <5AOF0
switch(date_time.mon)
{
case 1 : return "January";
case 2 : return "February";
case 3 : return "March";
case 4 : return "April";
case 5 : return "May";
case 6 : return "June";
case 7 : return "July";
case 8 : return "August";
case 9 : return "September";
case 10 : return "October";
case 11 : return "November";
case 12 : return "December";
default : return "Undefined";
}
/* @8<5@ 2K2>40:
July
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 3>4 87 AB@C:BC@K MqlDateTime |
//+------------------------------------------------------------------+
string MqlDateTimeYear(MqlDateTime &date_time,const uint header_width=0,const uint indent=0)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="Year:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%-lu",indent,"",w,header,date_time.year);
/* @8<5@ 2K2>40:
Year: 2023
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 <5AOF 87 AB@C:BC@K MqlDateTime |
//+------------------------------------------------------------------+
string MqlDateTimeMonth(MqlDateTime &date_time,const uint header_width=0,const uint indent=0)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="Month:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >;CG05< =0720=85 <5AOF0
string mn=Month(date_time);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%02lu (%s)",indent,"",w,header,date_time.mon,mn);
/* @8<5@ 2K2>40:
Month: 07 (July)
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 45=L 87 AB@C:BC@K MqlDateTime |
//+------------------------------------------------------------------+
string MqlDateTimeDay(MqlDateTime &date_time,const uint header_width=0,const uint indent=0)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="Day:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%02lu",indent,"",w,header,date_time.day);
/* @8<5@ 2K2>40:
Day: 19
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 G0AK 87 AB@C:BC@K MqlDateTime |
//+------------------------------------------------------------------+
string MqlDateTimeHour(MqlDateTime &date_time,const uint header_width=0,const uint indent=0)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="Hour:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%02lu",indent,"",w,header,date_time.hour);
/* @8<5@ 2K2>40:
Hour: 08
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 <8=CBK 87 AB@C:BC@K MqlDateTime |
//+------------------------------------------------------------------+
string MqlDateTimeMin(MqlDateTime &date_time,const uint header_width=0,const uint indent=0)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="Minutes:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%02lu",indent,"",w,header,date_time.min);
/* @8<5@ 2K2>40:
Minutes: 41
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 A5:C=4K 87 AB@C:BC@K MqlDateTime |
//+------------------------------------------------------------------+
string MqlDateTimeSec(MqlDateTime &date_time,const uint header_width=0,const uint indent=0)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="Seconds:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%02lu",indent,"",w,header,date_time.sec);
/* @8<5@ 2K2>40:
Seconds: 23
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 45=L =545;8 87 AB@C:BC@K MqlDateTime |
//+------------------------------------------------------------------+
string MqlDateTimeDayWeek(MqlDateTime &date_time,const uint header_width=0,const uint indent=0,bool descr=true)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="Day of week:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >;CG05< =0720=85 4=O =545;8
string dw=DayWeek(date_time);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%-s (%-lu)",indent,"",w,header,dw,date_time.day_of_week);
/* @8<5@ 2K2>40:
Day of week: Wednesday (3)
*/
}
//+------------------------------------------------------------------+
//|>72@0I05B 2 2845 AB@>:8 =><5@ 4=O 2 3>4C 87 AB@C:BC@K MqlDateTime|
//+------------------------------------------------------------------+
string MqlDateTimeDayYear(MqlDateTime &date_time,const uint header_width=0,const uint indent=0)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="Day of year:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%-lu",indent,"",w,header,date_time.day_of_year);
/* @8<5@ 2K2>40:
Day of year: 199
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 40BC 87 AB@C:BC@K MqlDateTime |
//| 2 D>@<0B5 DW, Month DD, YYYY, HH:MM:SS |
//+------------------------------------------------------------------+
string DateTime(MqlDateTime &date_time)
{
//--- >;CG05< <5AOF 8 ?5@2K5 B@8 A8<2>;0 4=O =545;8
string mn=Month(date_time);
string dw=StringSubstr(DayWeek(date_time),0,3);
//--- >72@0I05< AB@>:C, >BD>@<0B8@>20==CN 2 D>@<0B5 DW, Month DD, YYYY, HH:MM:SS
return StringFormat("%s, %s %02lu, %lu, %02lu:%02lu:%02lu",dw,mn,date_time.day,date_time.year,date_time.hour,date_time.min,date_time.sec);
/* @8<5@ 2K2>40:
Wed, July 19, 2023, 08:41:23
*/
}
//+------------------------------------------------------------------+
//| K2>48B 2 6C@=0; >?8A0=85 2A5E ?>;59 AB@C:BC@K MqlDateTime |
//+------------------------------------------------------------------+
void MqlDateTimePrint(MqlDateTime &date_time,const bool short_entry=true,const uint header_width=0,const uint indent=0)
{
//--- A;8 :@0B:0O 70?8AL - 2K2>48< 2 6C@=0; 40BC-2@5<O 2 D>@<0B5 DW, Month DD, YYYY, HH:MM:SS
if(short_entry)
Print(DateTime(date_time));
/* @8<5@ 2K2>40:
Wed, July 19, 2023, 08:41:23
*/
//--- =0G5
else
{
//--- A>740Q< AB@>:C A >?8A0=85< 2A5E 40==KE AB@C:BC@K A >BABC?0<8 8 7040==>9 H8@8=>9 ?>;O 703>;>2:0
string res=StringFormat("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
MqlDateTimeYear(date_time,header_width,indent),
MqlDateTimeMonth(date_time,header_width,indent),
MqlDateTimeDay(date_time,header_width,indent),
MqlDateTimeHour(date_time,header_width,indent),
MqlDateTimeMin(date_time,header_width,indent),
MqlDateTimeSec(date_time,header_width,indent),
MqlDateTimeDayWeek(date_time,header_width,indent),
MqlDateTimeDayYear(date_time,header_width,indent)
);
//--- K2>48< 2 6C@=0; ?>;CG5==CN AB@>:C
Print(res);
}
/* @8<5@ 2K2>40:
Year: 2023
Month: 07 (July)
Day: 19
Hour: 09
Minutes: 32
Seconds: 25
Day of week: Wednesday (3)
Day of year: 199
*/
}
//+------------------------------------------------------------------+