0 Views
0 Downloads
0 Favorites
advanced
ÿþ//+------------------------------------------------------------------+

//|                                                     advanced.mq5 |

//|                                        Copyright © 2024, Amr Ali |

//|                             https://www.mql5.com/en/users/amrali |

//+------------------------------------------------------------------+



// optionally adjust performance via #defines before #include



// enable performance mode for library

//#define TIMEUTILS_PERFORMANCE_MODE

#include "TimeUtils.mqh"



int counter = 0;



#define PRINT(A) Print((string)++counter + ". " + #A + " = ", (A))



void OnStart()

{

//+==================================================================+

//| Create datetime From Components                                  |

//+==================================================================+

   PRINT( CreateDateTime(2022, 03, 25) );



//+==================================================================+

//| Break datetime To Components                                     |

//+==================================================================+

   MqlDateTime st[1] = {};

   TimeToStructFast(TimeCurrent(), st[0]);

   ArrayPrint(st);



//+==================================================================+

//| Extract Components of datetime: Sunday, yyyy.mm.dd hh:mm:ss      |

//| Get() Units                                                      |

//+==================================================================+

   datetime t = TimeLocal();

   PRINT( t2s(t, TIME_DATE|TIME_SECONDS) );  // Formats time with the weekday name



   PRINT( GetYear(t)   );

   PRINT( GetMonth(t)  );

   PRINT( GetDay(t)    );

   PRINT( GetHour(t)   );

   PRINT( GetMinute(t) );

   PRINT( GetSecond(t) );



//+==================================================================+

//| Day() Number                                                     |

//+==================================================================+

   PRINT( DayOfWeek(t) );

   PRINT( DayOfYear(t) );

   PRINT( DayIndex(t) );



//+==================================================================+

//| Week() Number                                                    |

//+==================================================================+

   PRINT( WeekOfMonth(t) );

   PRINT( WeekOfYear(t) );

   PRINT( WeekIndex(t) );



   PRINT( WeekOfMonth(t, true) ); // StartsOnMonday = true

   PRINT( WeekOfYear(t, true) ); // StartsOnMonday = true

   PRINT( WeekIndex(t, true) ); // StartsOnMonday = true



//+==================================================================+

//| StartOf() Units                                                  |

//+==================================================================+

   PRINT( StartOfMinute(t) );

   PRINT( StartOfHour(t) );

   PRINT( StartOfDay(t) );

   PRINT( StartOfWeek(t) );

   PRINT( StartOfWeek(t, true) ); // StartsOnMonday = true

   PRINT( StartOfMonth(t) );

   PRINT( StartOfYear(t) );



//+==================================================================+

//| EndOf() Units                                                    |

//+==================================================================+

  PRINT( EndOfMinute(t) );

  PRINT( EndOfHour(t) );

  PRINT( EndOfDay(t) );

  PRINT( EndOfWeek(t) );

  PRINT( EndOfWeek(t, true) ); // StartsOnMonday = true

  PRINT( EndOfMonth(t) );

  PRINT( EndOfYear(t) );



//+==================================================================+

//| SecsElapsedOf() Units                                            |

//+==================================================================+

   PRINT( SecsElapsedOfMinute(t) );

   PRINT( SecsElapsedOfHour(t) );

   PRINT( SecsElapsedOfDay(t) );

   PRINT( SecsElapsedOfWeek(t) );

   PRINT( SecsElapsedOfWeek(t, true) ); // StartsOnMonday = true

   PRINT( SecsElapsedOfMonth(t) );

   PRINT( SecsElapsedOfYear(t) );



//+==================================================================+

//| RoundTo() / Nearest() Units                                      |

//+==================================================================+

   PRINT( RoundToMinute(t) );

   PRINT( RoundToHour(t) );

   PRINT( RoundToDay(t) );

   PRINT( RoundToWeek(t) );

   PRINT( RoundToWeek(t, true) ); // StartsOnMonday = true



//+==================================================================+

//| CeilTo() / Next() Units                                          |

//+==================================================================+

   PRINT( CeilToMinute(t) );

   PRINT( CeilToHour(t) );

   PRINT( CeilToDay(t) );

   PRINT( CeilToWeek(t) );

   PRINT( CeilToWeek(t, true) ); // StartsOnMonday = true



//+==================================================================+

//| Next() Weekday                                                   |

//+==================================================================+

   PRINT( NextSunday(t) );

   PRINT( NextMonday(t) );

   PRINT( NextTuesday(t) );

   PRINT( NextWednesday(t) );

   PRINT( NextThursday(t) );

   PRINT( NextFriday(t) );

   PRINT( NextSaturday(t) );



//+==================================================================+

//| Previous() Weekday                                               |

//+==================================================================+

   PRINT( PreviousSunday(t) );

   PRINT( PreviousMonday(t) );

   PRINT( PreviousTuesday(t) );

   PRINT( PreviousWednesday(t) );

   PRINT( PreviousThursday(t) );

   PRINT( PreviousFriday(t) );

   PRINT( PreviousSaturday(t) );



//+==================================================================+

//| Nth() Weekday Of The Month                                       |

//+==================================================================+

  PRINT( FirstWeekdayOfTheMonth(t, SUNDAY) );

  PRINT( LastWeekdayOfTheMonth(t, SUNDAY) );



//+==================================================================+

//| Add() Units                                                      |

//+==================================================================+

   PRINT( AddSeconds(t, 30) );

   PRINT( AddMinutes(t, 30) );

   PRINT( AddHours(t, 2) );

   PRINT( AddDays(t, 10) );

   PRINT( AddBusinessDays(t, 10) );

   PRINT( AddWeeks(t, 4) );

   PRINT( AddMonths(t, 2) );

   PRINT( AddYears(t, 5) );



//+==================================================================+

//| Sub() Units                                                      |

//+==================================================================+

   PRINT( SubSeconds(t, 30) );

   PRINT( SubMinutes(t, 30) );

   PRINT( SubHours(t, 2) );

   PRINT( SubDays(t, 10) );

   PRINT( SubBusinessDays(t, 10) );

   PRINT( SubWeeks(t, 4) );

   PRINT( SubMonths(t, 2) );

   PRINT( SubYears(t, 5) );



//+==================================================================+

//| DifferenceIn() Units                                             |

//+==================================================================+

   PRINT( DifferenceInCalendarDays(t, AddWeeks(t, 9)) );

   PRINT( DifferenceInBusinessDays(t, AddWeeks(t, 9)) );

   PRINT( DifferenceInCalendarWeeks(t, AddWeeks(t, 9)) );

   PRINT( DifferenceInCalendarWeeks(t, AddWeeks(t, 9), true) ); // StartsOnMonday = true

   PRINT( DifferenceInCalendarMonths(t, AddWeeks(t, 9)) );



//+==================================================================+

//| IsSame() Units                                                   |

//+==================================================================+

   PRINT( IsSameMinute(t, AddHours(t, 25)) );

   PRINT( IsSameHour(t, AddHours(t, 25)) );

   PRINT( IsSameDay(t, AddHours(t, 25)) );

   PRINT( IsSameWeek(t, AddHours(t, 25)) );

   PRINT( IsSameWeek(t, AddHours(t, 25), true) ); // StartsOnMonday = true

   PRINT( IsSameMonth(t, AddHours(t, 25)) );

   PRINT( IsSameYear(t, AddHours(t, 25)) );



//+==================================================================+

//| IsCurrent() Units                                                |

//+==================================================================+

   PRINT( IsCurrentMinute(t) );

   PRINT( IsCurrentHour(t) );

   PRINT( IsCurrentWeek(t) );

   PRINT( IsCurrentWeek(t, true) ); // StartsOnMonday = true

   PRINT( IsCurrentMonth(t) );

   PRINT( IsCurrentYear(t) );

   PRINT( IsToday(t) );

   PRINT( IsTomorrow(t) );

   PRINT( IsYesterday(t) );



//+==================================================================+

//| Misc                                                             |

//+==================================================================+

   PRINT( IsLeapYear(2100) );

   PRINT( DaysInMonth(2024, 2) );

   PRINT( GetNthWeekdayInYearMonth(2024, 1, 1, SUNDAY) );



//+==================================================================+

//| Formating Time to String                                         |

//+==================================================================+

   PRINT( t2s(TimeCurrent()) );

   PRINT( SecondsToString(SecsElapsedOfDay(TimeCurrent())) );

   PRINT( TimeFormat(TimeCurrent(), "YYYY.MM.DD hh:mm") );

   PRINT( TimeFormat(TimeCurrent(), "DDD, YYYY.MM.DD hh:mm:ss") );

   PRINT( TimeFormat(TimeCurrent(), "D MMM YYYY, HH:mm a") );



}

//+------------------------------------------------------------------+





/*

 example output:



 1. CreateDateTime(2022, 03, 25) = 2022.03.25 00:00:00

     [year] [mon] [day] [hour] [min] [sec] [day_of_week] [day_of_year]

 [0]   2024    12    18     17    27    25             3           352

 2. t2s(t, TIME_DATE|TIME_SECONDS) = Wed, 2024.12.18 18:27:25

 3. GetYear(t) = 2024

 4. GetMonth(t) = 12

 5. GetDay(t) = 18

 6. GetHour(t) = 18

 7. GetMinute(t) = 27

 8. GetSecond(t) = 25

 9. DayOfWeek(t) = 3

 10. DayOfYear(t) = 352

 11. DayIndex(t) = 20075

 12. WeekOfMonth(t) = 3

 13. WeekOfYear(t) = 51

 14. WeekIndex(t) = 2868

 15. WeekOfMonth(t, true) = 4

 16. WeekOfYear(t, true) = 51

 17. WeekIndex(t, true) = 2868

 18. StartOfMinute(t) = 2024.12.18 18:27:00

 19. StartOfHour(t) = 2024.12.18 18:00:00

 20. StartOfDay(t) = 2024.12.18 00:00:00

 21. StartOfWeek(t) = 2024.12.15 00:00:00

 22. StartOfWeek(t, true) = 2024.12.16 00:00:00

 23. StartOfMonth(t) = 2024.12.01 00:00:00

 24. StartOfYear(t) = 2024.01.01 00:00:00

 25. EndOfMinute(t) = 2024.12.18 18:27:59

 26. EndOfHour(t) = 2024.12.18 18:59:59

 27. EndOfDay(t) = 2024.12.18 23:59:59

 28. EndOfWeek(t) = 2024.12.21 23:59:59

 29. EndOfWeek(t, true) = 2024.12.22 23:59:59

 30. EndOfMonth(t) = 2024.12.31 23:59:59

 31. EndOfYear(t) = 2024.12.31 23:59:59

 32. SecsElapsedOfMinute(t) = 25

 33. SecsElapsedOfHour(t) = 1645

 34. SecsElapsedOfDay(t) = 66445

 35. SecsElapsedOfWeek(t) = 325645

 36. SecsElapsedOfWeek(t, true) = 239245

 37. SecsElapsedOfMonth(t) = 1535245

 38. SecsElapsedOfYear(t) = 30479245

 39. RoundToMinute(t) = 2024.12.18 18:27:00

 40. RoundToHour(t) = 2024.12.18 18:00:00

 41. RoundToDay(t) = 2024.12.19 00:00:00

 42. RoundToWeek(t) = 2024.12.22 00:00:00

 43. RoundToWeek(t, true) = 2024.12.16 00:00:00

 44. CeilToMinute(t) = 2024.12.18 18:28:00

 45. CeilToHour(t) = 2024.12.18 19:00:00

 46. CeilToDay(t) = 2024.12.19 00:00:00

 47. CeilToWeek(t) = 2024.12.22 00:00:00

 48. CeilToWeek(t, true) = 2024.12.23 00:00:00

 49. NextSunday(t) = 2024.12.22 00:00:00

 50. NextMonday(t) = 2024.12.23 00:00:00

 51. NextTuesday(t) = 2024.12.24 00:00:00

 52. NextWednesday(t) = 2024.12.25 00:00:00

 53. NextThursday(t) = 2024.12.19 00:00:00

 54. NextFriday(t) = 2024.12.20 00:00:00

 55. NextSaturday(t) = 2024.12.21 00:00:00

 56. PreviousSunday(t) = 2024.12.15 00:00:00

 57. PreviousMonday(t) = 2024.12.16 00:00:00

 58. PreviousTuesday(t) = 2024.12.17 00:00:00

 59. PreviousWednesday(t) = 2024.12.11 00:00:00

 60. PreviousThursday(t) = 2024.12.12 00:00:00

 61. PreviousFriday(t) = 2024.12.13 00:00:00

 62. PreviousSaturday(t) = 2024.12.14 00:00:00

 63. FirstWeekdayOfTheMonth(t, SUNDAY) = 2024.12.01 00:00:00

 64. LastWeekdayOfTheMonth(t, SUNDAY) = 2024.12.29 00:00:00

 65. AddSeconds(t, 30) = 2024.12.18 18:27:55

 66. AddMinutes(t, 30) = 2024.12.18 18:57:25

 67. AddHours(t, 2) = 2024.12.18 20:27:25

 68. AddDays(t, 10) = 2024.12.28 18:27:25

 69. AddBusinessDays(t, 10) = 2025.01.01 18:27:25

 70. AddWeeks(t, 4) = 2025.01.15 18:27:25

 71. AddMonths(t, 2) = 2025.02.18 18:27:25

 72. AddYears(t, 5) = 2029.12.18 18:27:25

 73. SubSeconds(t, 30) = 2024.12.18 18:26:55

 74. SubMinutes(t, 30) = 2024.12.18 17:57:25

 75. SubHours(t, 2) = 2024.12.18 16:27:25

 76. SubDays(t, 10) = 2024.12.08 18:27:25

 77. SubBusinessDays(t, 10) = 2024.12.04 18:27:25

 78. SubWeeks(t, 4) = 2024.11.20 18:27:25

 79. SubMonths(t, 2) = 2024.10.18 18:27:25

 80. SubYears(t, 5) = 2019.12.18 18:27:25

 81. DifferenceInCalendarDays(t, AddWeeks(t, 9)) = 63

 82. DifferenceInBusinessDays(t, AddWeeks(t, 9)) = 45

 83. DifferenceInCalendarWeeks(t, AddWeeks(t, 9)) = 9

 84. DifferenceInCalendarWeeks(t, AddWeeks(t, 9), true) = 9

 85. DifferenceInCalendarMonths(t, AddWeeks(t, 9)) = 2

 86. IsSameMinute(t, AddHours(t, 25)) = false

 87. IsSameHour(t, AddHours(t, 25)) = false

 88. IsSameDay(t, AddHours(t, 25)) = false

 89. IsSameWeek(t, AddHours(t, 25)) = true

 90. IsSameWeek(t, AddHours(t, 25), true) = true

 91. IsSameMonth(t, AddHours(t, 25)) = true

 92. IsSameYear(t, AddHours(t, 25)) = true

 93. IsCurrentMinute(t) = false

 94. IsCurrentHour(t) = false

 95. IsCurrentWeek(t) = true

 96. IsCurrentWeek(t, true) = true

 97. IsCurrentMonth(t) = true

 98. IsCurrentYear(t) = true

 99. IsToday(t) = true

 100. IsTomorrow(t) = false

 101. IsYesterday(t) = false

 102. IsLeapYear(2100) = false

 103. DaysInMonth(2024, 2) = 29

 104. GetNthWeekdayInYearMonth(2024, 1, 1, SUNDAY) = 2024.01.07 00:00:00

 105. t2s(TimeCurrent()) = Wed, 2024.12.18 17:27

 106. SecondsToString(SecsElapsedOfDay(TimeCurrent())) = 17:27:25

 107. TimeFormat(TimeCurrent(), YYYY.MM.DD hh:mm) = 2024.12.18 17:27

 108. TimeFormat(TimeCurrent(), DDD, YYYY.MM.DD hh:mm:ss) = Wed, 2024.12.18 17:27:25

 109. TimeFormat(TimeCurrent(), D MMM YYYY, HH:mm a) = 18 Dec 2024, 05:27 pm



*/

Comments