Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

High-Performance Time Functions (TimeUtils) - MetaTrader 5 Library - MT4/MT5 Resources

author EAcpu | 3 reads | 0 comments |

The library contains more than 80 different functions to handle time variables. The main goal is to provide high performance time functions. Performance mode is disabled by default (can be controlled at compile time via #define). This mode does not require the library to be included into your project, as it will work fine without it.

TIMEUTILS_PERFORMANCE_MODE

Optionally, performance mode can be turned on at compile time via #defines before #include:

 // Enable performance mode for the library
#Define TIMEUTILS_PERFORMANCE_MODE #Include "TimeUtils.mqh"

This redirects all calls to MQL's built-in build-time and structure-to-time functionality to more efficient alternatives.

You can compile the script "performance_mode.mq5" with or without TIMEUTILS_PERFORMANCE_MODE Check the speed differences on your machine. This will be beneficial for high-performance programs that perform heavy or many time-related tasks (for example, scanning all tick history of the H1 bar at the beginning of the trading week or collecting other statistics).

Function names should be self-explanatory; in addition, you can find a short description of a specific function in the TimeUtils.mqh file.

//+======================================================================+
//|Create date and time from component |
//+======================================================================+
DateTimeCreationDateTime (
constant integer year, // year constant integer Monday, // month constant integer day, // day constant integer hour = 0 , // hour constant integer minute = 0 , // minute constant integer second = 0 // seconds ) DateTime CreateDateTime( MqlDateTime &dt_struct); // Fast alternative to StructToTime()

//+======================================================================+
//|Decompose datetime into components |
//+======================================================================+
boolean fast structure time(
datetime det, // The date value to be converted Mql datetime & dt_struct // The structure that takes the value ) //+==================================================================+
//|Extract the components of the date and time: Sunday, yyyy.mm.dd hh:mm:ss |
//| Get() unit |
//+======================================================================+
Integer gets the second ( date time t) Integer gets the minute ( date time t) Integer gets the time ( date time t) Integer gets the day ( date time t) Integer gets the month ( date time t) Integer gets the year ( date time t) //+======================================================================+
//| Day() number |
//+======================================================================+
Integer day of the week ( date time t) Integer day of the year ( date time t) Integer day index ( date time t) //+====================================================================+
//|Week number |
//+======================================================================+
integer week of the month ( const datetime t, boolean start on Monday = false ) integer week of the year ( const datetime t, boolean start on Monday = false ) integer week index ( datetime t, boolean start on Monday = false ) //+==================================================================+
//| StartOf() unit |
//+======================================================================+
datetime start of minute ( datetime t) datetime start of time ( datetime t) datetime start of day ( datetime t) datetime start of week ( datetime t, boolean>starts on monday=Wrongdate timethe beginning of the month(date timet)date timeBeginning of the year(date timet) //+====================================================================+ //| EndOf() unit                                                    | //+========================================================================+ datetime minute end ( datetime t) datetime endtime ( datetime t) datetime end of day ( datetime t) datetime end of week ( datetime t, boolean Monday start = false ) datetime end of month ( datetime t) datetime end of year ( datetime t) //+====================================================================+ //| SecsElapsedOf() unit | //+========================================================================+ Integer minutes seconds elapsed ( datetime t) Integer seconds elapsed ( datetime t) Integer days elapsed seconds ( datetime t) Integer weeks elapsed seconds ( datetime t, boolean Monday start = false ) Integer months elapsed seconds ( datetime t) Integer years elapsed seconds ( datetime t) //+======================================================================+ //| RoundTo() / Nearest() Unit | //+========================================================================+ datetime round to minute ( datetime t) datetime round to hour (datetime t) datetime round to hour ( datetime t) datetime round to week ( datetime t, boolean start Monday = false ) //+======================================================================+ //| CeilTo() / Next() Unit | //+========================================================================+ datetime ceiling to minute( datetime t) datetime ceiling to hour( datetime t) datetime ceiling to day( datetime t) datetime ceiling to week( datetime t, boolean start Monday = false ) //+========================================================================+ //|Next () working day | //+====================================================================================================================================================================+ Date and time next working day ( date and time t, ENUM_DAY_OF_WEEK working day = Sunday ) date time next Sunday (date time t) date time next Monday ( date time t) date time next Tuesday ( date time t) date time next Wednesday ( date time t) date time next Thursday ( date time t) date time next Friday (date time t) date time next Saturday ( date time t) //+======================================================================+ //|Previous page() working day | //+==================================================================================+ Date time Previous working day ( date time t, ENUM_DAY_OF_WEEKworking day = Sunday ) date time last Sunday ( date time t) date time last Monday ( date time t) date time last Tuesday ( date time t) date time last Wednesday ( date time t) date time last Thursday ( date time t) date time last Friday ( date time t) date time last Saturday ( date time t) //+========================================================================================+ //|The N ( )th working day of the month | //+======================================================================+ Date and time The first working day of the month ( date and time t , ENUM_DAY_OF_WEEK working day = Sunday ) Date and time The last working day of the month ( date and time t , ENUM_DAY_OF_WEEK working day = Sunday ) Date and time The Nth working day of this month ( Date and time t, integer Nth, ENUM_DAY_OF_WEEK working day = Sunday) //+========================================================================+ //| Add() unit | // +======================================================================+ datetime add seconds ( datetime t, integer number) datetime add minutes ( datetime t, integer number) datetime add time ( date time t , integer quantity) date time add days ( date time t, integer quantity) date time add weekdays ( date time t, integer quantity) date time add week number ( date time t, integer quantity) date time add month ( date time t, integer quantity) date time add year ( date time t, integer quantity) //+======================================================================+ //| Sub() Unit | //+======================================================================+ datetime subsecond ( datetime t, integer number) datetime subminute ( datetime t, integer number) date time less than hours ( datetime t, integer quantity) datetime subday ( datetime t, integer quantity) datetime subbusiness day (datetime t, integer quantity) datetime subweek ( datetime t, integer quantity) datetime submonth ( datetime t, integer quantity ) datetime subyear ( datetime t, integer quantity ) //+======================================================================+ //| DifferenceIn() unit | //+======================================================================+ Integer calendar day difference( datetime start time, date time end time) integer calendar week difference( date time start time, date time end time) integer calendar week difference(date time start time, date time end time, boolean Monday start = false ) integer calendar month difference( date time start time, date time end time)ment">//+====================================================================+ //| IsSame() unit | //+====================================================================+ The Boolean value is the same minute ( date time t1, date time t2) The Boolean value is the same time ( date time Between t1, date time t2) Boolean value is the same day ( date time t1, date time t2) Boolean value is the same week ( date time t1, date time t2, boolean value starts on Monday = false ) Boolean value is the same month (date time t1, date time t2) Boolean value is the same year ( date time t1, date time t2) //+======================================================================+ //| IsCurrent() Unit | //+====================================================================+ The Boolean value is the current minute ( date time t) The Boolean value is the current time ( date time t) time t) boolean is the current week ( date time t, boolean Monday starts = false ) boolean is the current month ( date time t) boolean is the current year ( date time t) boolean is today (date time t) boolean is tomorrow ( date time t) boolean is yesterday ( date time t) //+======================================================================+ //|Others | //+======================================================================+ The Boolean value is a leap year ( integer year) integer number of days in a month ( integer year , integer month) date and time GetNthWeekdayInYearMonth (iYear, iMonth, Nth, weekday = Sunday ) date and time GetNthSundayInYearMonth (iYear, iMonth, Nth) //+============================================================================+ //| Format time as a string | //+============================================================================================+ string t2s(datetime t, const integer mode = TIME_DATE | TIME_MINUTES ) string seconds to string ( integer seconds) string time format ( date time t, string format = "year.month.day:minute" )


//+------------------------------------------------------------------+
//|Get the formatting time based on the passed token string. |
//|List of all available formats: |
//|Format Output Description |
//| ------- ---------------- ------------------------------------------------------------------ |
//| YY 18 two-digit year |
//| YYYY 2018 four-digit year |
//| M January-December, starting from 1 |
//| MM 01-December, 2 digits |
//| MMM Jan-Dec Month abbreviation, 3 letters |
//| MMMM January to December Full month name |
//| D 1-31 One day in January |
//| DD 01-31 day in month, 2 digits |
//| DDD Sunday to Saturday Abbreviation of a day of the week |
//| DDDD Sunday to Saturday The name of the day of the week |
//| h 0-23 hours |
//| hh 00-23 hours, 2 digits |
//| H 1-12 hours, 12-hour clock |
//| HH 01-12 hours, 12-hour clock, 2 digits |
//| m 0-59 minutes |
//| mm 00-59 minutes, 2 digits |
//| s 0-59 second |
//| ss 00-59 second 2-digit number |
//| A AM PM |
//| a morning |
//+------------------------------------------------------------------+
//|Example format: |
//| "YYYY.MM.DD hh:mm" // "2024.12.08 22:05" (default) |
//| "DDD, YYYY.MM.DD hh:mm:ss" // "Sunday, 2024.12.08 22:05:21" |
//| "D MMMM YYYY, HH:mm a" // "December 8, 2024, 10:05 pm" |
//| "DD/MM/YYYY" // "08/12/2024" |
//+------------------------------------------------------------------+
String time format ( constant date time t, constant string format = "year.month.day:hour:minute" );


The two attached scripts "basic.mq5" and "advanced.mq5" show basic and advanced usage examples.

Example output of the "advanced.mq5" script:

/*
 Example output:

 1. Creation date and time (2022, 03, 25) = 2022.03.25 00:00:00
    [Year] [Monday] [Day] [Hour] [Minute] [Second] [Day of the week] [Year day]
 [0] 2024 12 18 17 27 25 3 352
 2. t2s(t, TIME_DATE|TIME_SECONDS) = Wednesday, 2024.12.18 18:27:25
 3. GetYear(t) = 2024
 4. Get month(t) = 12
 5. GetDay(t) = 18
 6. GetHour(t) = 18
 7. Get minutes(t) = 27
 8. GetSecond(t) = 25
 9. Day of the week (t) = 3
 10. Day in the year (t) = 352
 11. Daily index (t) = 20075
 12. Week of month (t) = 3
 13. Week of year (t) = 51
 14. Weekly index (t) = 2868
 15. WeekOfMonth(t, true) = 4
 16. WeekOfYear(t, true) = 51
 17. WeekIndex(t, true) = 2868
 18. Start time (t) = 2024.12.18 18:27:00
 19. Start time (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. Month start (t) = 2024.12.01 00:00:00
 24. Year start (t) = 2024.01.01 00:00:00
 25. EndOfMinute(t) = 2024.12.18 18:27:59
 26. End time (t) = 2024.12.18 18:59:59
 27. EndOfDay(t) = 2024.12.18 23:59:59
 28. End of week (t) = 2024.12.21 23:59:59
 29. EndOfWeek(t, true) = 2024.12.22 23:59:59
 30. End of month (t) = 2024.12.31 23:59:59
 31. Year-end (t) = 2024.12.31 23:59:59
 32. Seconds elapsed time (t) = 25
 33.SecsElapsedOfHour(t) = 1645
 34.SecsElapsedOfDay(t) = 66445
 35. Seconds per week (t) = 325645
 36. SecsElapsedOfWeek(t, true) = 239245
 37. Number of elapsed seconds per month (t) = 1535245
 38. Almanac seconds (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. Next Sunday (t) = 2024.12.22 00:00:00
 50. Next Monday (t) = 2024.12.23 00:00:00
 51. Next Tuesday (t) = 2024.12.24 00:00:00
 52. Next Wednesday (t) = 2024.12.25 00:00:00
 53. Next Thursday (t) = 2024.12.19 00:00:00
 54. Next Friday (t) = 2024.12.20 00:00:00
 55. Next Saturday (t) = 2024.12.21 00:00:00
 56. Last Sunday (t) = 2024.12.15 00:00:00
 57. Last Monday (t) = 2024.12.16 00:00:00
 58. Last Tuesday (t) = 2024.12.17 00:00:00
 59. Last Wednesday (t) = 2024.12.11 00:00:00
 60. Last Thursday (t) = 2024.12.12 00:00:00
 61. Last Friday (t) = 2024.12.13 00:00:00
 62. Last Saturday (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. Add minutes (t, 30) = 2024.12.18 18:57:25
 67. Add time (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. Add week(t, 4) = 2025.01.15 18:27:25
 71. Add month (t, 2) = 2025.02.18 18:27:25
 72. AddYears(t, 5) = 2029.12.18 18:27:25
 73. Subsecond(t, 30) = 2024.12.18 18:26:55
 74. Subminute(t, 30) = 2024.12.18 17:57:25
 75. Subtime(t, 2) = 2024.12.18 16:27:25
 76. Subday (t, 10) = 2024.12.08 18:27:25
 77. SubBusinessDays(t, 10) = 2024.12.04 18:27:25
 78. Subweek(t, 4) = 2024.11.20 18:27:25
 79. Submonth(t, 2) = 2024.10.18 18:27:25
 80. Subyear(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)) = 286. 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. Number of days in the month (2024, 2) = 29
 104. GetNthWeekdayInYearMonth(2024, 1, 1, Sunday) = 2024.01.07 00:00:00
 105. t2s(TimeCurrent()) = Wednesday, 2024.12.18 17:27
 106.SecondsToString(SecsElapsedOfDay(TimeCurrent())) = 17:27:25
 107. TimeFormat(TimeCurrent(), YYYY.MM.DD hour:minute) = 2024.12.18 17:27
 108. TimeFormat(TimeCurrent(), DDD, YYYY.MM.DD hh:mm:ss) = Wednesday, 2024.12.18 17:27:25
 109. TimeFormat(TimeCurrent(), D MMM YYYY, HH:mm a) = December 18, 2024 05:27 pm

*/


2024.12.03 - v.1.10: Initial release.

2024.12.05 - v.1.15: Added functionality for working days and the N()th working day of the month. Optimize calculations in DaysInMonth() function.

2024.12.09 - v.1.20: Added TimeFormat() function for formatting time based on passed token string.

2024.12.09 - v.1.25: Optimize calculations in TimeToStructFast() function.

2024.12.10 - v.1.30: Optimize TimeFormat(), StartOfYear() and EndOfYear() functions. Updated descriptions of some features.

2024.12.15 - v.1.35: Optimize the GetNthWeekdayInYearMonth() function.

2024.12.18 - v.1.40: Added IsCurrentXXX(), IsToday(), IsTomorrow() and IsYesterday() functions.

2024.12.19 - v.1.45: Optimize AddMonths() and GetNthWeekdayInYearMonth() functions.

2024.12.20 - v.1.50: More code cleanup.

2024.12.21 - v.1.55: Simplified calculations in AddMonths(): Replaced the complex logic of adjusting months and years with a simpler calculation of total months.


Attachment download

📎 timeutils.mqh (124.27 KB)

📎 basic.mq5 (2.38 KB)

📎 advanced.mq5 (27.74 KB)

📎 performance_mode.mq5 (3.21 KB)

Source: MQL5 #53970

Verification code Refresh