Time Shield Library
C++ library for working with time
Toggle main menu visibility
Loading...
Searching...
No Matches
time_utils.mqh
Go to the documentation of this file.
1
//+------------------------------------------------------------------+
2
//| time_utils.mqh |
3
//| Time Shield - MQL5 Time Utilities |
4
//| Copyright 2025, NewYaroslav |
5
//| https://github.com/NewYaroslav/time-shield-cpp |
6
//+------------------------------------------------------------------+
7
#ifndef TIME_SHIELD_MQL5_HEADER_TIME_SHIELD_TIME_UTILS_MQH_INCLUDED
8
#define TIME_SHIELD_MQL5_HEADER_TIME_SHIELD_TIME_UTILS_MQH_INCLUDED
9
16
17
#property copyright "Copyright 2025, NewYaroslav"
18
#property link "https://github.com/NewYaroslav/time-shield-cpp"
19
#property strict
20
21
#include "
constants.mqh
"
22
23
24
namespace
time_shield
{
25
29
32
long
microseconds
() {
33
static
bool
initialized =
false
;
34
static
long
offset = 0;
35
if
(!initialized) {
36
long
start_ts = TimeGMT();
37
ulong start_mc = GetMicrosecondCount();
38
long
next_ts = start_ts;
39
while
((next_ts = TimeGMT()) == start_ts) {
40
// wait until the second changes
41
}
42
ulong next_mc = GetMicrosecondCount();
43
offset = next_ts *
US_PER_SEC
- (long)next_mc;
44
initialized =
true
;
45
}
46
return
(
long
)GetMicrosecondCount() + offset;
47
}
48
51
long
ns_of_sec
() {
52
return
(
long
)((
microseconds
() %
US_PER_SEC
) *
NS_PER_US
);
53
}
54
57
int
us_of_sec
() {
58
return
(
int
)(
microseconds
() %
US_PER_SEC
);
59
}
60
63
int
ms_of_sec
() {
64
return
(
int
)((
microseconds
() / 1000) %
MS_PER_SEC
);
65
}
66
69
long
ts
() {
70
return
microseconds
() /
US_PER_SEC
;
71
}
72
75
long
timestamp
() {
return
ts
(); }
76
79
double
fts
() {
80
return
(
double
)
microseconds
() / (double)
US_PER_SEC
;
81
}
82
85
double
ftimestamp
() {
return
fts
(); }
86
89
long
ts_ms
() {
90
return
microseconds
() / 1000;
91
}
92
95
long
timestamp_ms
() {
return
ts_ms
(); }
96
99
long
now
() {
return
ts_ms
(); }
100
103
long
ts_us
() {
104
return
microseconds
();
105
}
106
109
long
timestamp_us
() {
return
ts_us
(); }
110
112
113
};
// namespace time_shield
114
115
#endif
// TIME_SHIELD_MQL5_HEADER_TIME_SHIELD_TIME_UTILS_MQH_INCLUDED
constants.mqh
Header file with time-related constants.
time_shield::NS_PER_US
constexpr int64_t NS_PER_US
Nanoseconds per microsecond.
Definition
constants.hpp:71
time_shield::MS_PER_SEC
constexpr int64_t MS_PER_SEC
Milliseconds per second.
Definition
constants.hpp:77
time_shield::US_PER_SEC
constexpr int64_t US_PER_SEC
Microseconds per second.
Definition
constants.hpp:76
time_shield::timestamp_us
ts_us_t timestamp_us() noexcept
Get the current UTC timestamp in microseconds.
Definition
time_utils.hpp:274
time_shield::now
ts_ms_t now() noexcept
Get the current UTC timestamp in milliseconds.
Definition
time_utils.hpp:258
time_shield::ns_of_sec
T ns_of_sec() noexcept
Get the nanosecond part of the current second.
Definition
time_utils.hpp:183
time_shield::ts
ts_t ts() noexcept
Get the current UTC timestamp in seconds.
Definition
time_utils.hpp:210
time_shield::microseconds
long microseconds()
Get the number of microseconds since the UNIX epoch.
Definition
time_utils.mqh:32
time_shield::timestamp
ts_t timestamp() noexcept
Get the current UTC timestamp in seconds.
Definition
time_utils.hpp:218
time_shield::ftimestamp
fts_t ftimestamp() noexcept
Get the current UTC timestamp in floating-point seconds.
Definition
time_utils.hpp:234
time_shield::ts_us
ts_us_t ts_us() noexcept
Get the current UTC timestamp in microseconds.
Definition
time_utils.hpp:266
time_shield::ms_of_sec
T ms_of_sec() noexcept
Get the millisecond part of the current second.
Definition
time_utils.hpp:203
time_shield::ts_ms
ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
Definition
time_utils.hpp:242
time_shield::us_of_sec
T us_of_sec() noexcept
Get the microsecond part of the current second.
Definition
time_utils.hpp:193
time_shield::timestamp_ms
ts_ms_t timestamp_ms() noexcept
Get the current UTC timestamp in milliseconds.
Definition
time_utils.hpp:250
time_shield::fts
fts_t fts() noexcept
Get the current UTC timestamp in floating-point seconds.
Definition
time_utils.hpp:226
time_shield
Main namespace for the Time Shield library.
MQL5
Include
time_shield
time_utils.mqh
Generated by
1.17.0