Time Shield Library
C++ library for working with time
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_TIME_UTILS_MQH__
8#define __TIME_SHIELD_TIME_UTILS_MQH__
9
16
17#property copyright "Copyright 2025, NewYaroslav"
18#property link "https://github.com/NewYaroslav/time-shield-cpp"
19#property strict
20
21namespace time_shield {
22
26
29 long microseconds() {
30 static bool initialized = false;
31 static long offset = 0;
32 if(!initialized) {
33 long start_ts = TimeGMT();
34 ulong start_mc = GetMicrosecondCount();
35 long next_ts = start_ts;
36 while((next_ts = TimeGMT()) == start_ts) {
37 // wait until the second changes
38 }
39 ulong next_mc = GetMicrosecondCount();
40 offset = next_ts * US_PER_SEC - (long)next_mc;
41 initialized = true;
42 }
43 return (long)GetMicrosecondCount() + offset;
44 }
45
48 long ns_of_sec() {
49 return (long)((microseconds() % US_PER_SEC) * NS_PER_US);
50 }
51
54 int us_of_sec() {
55 return (int)(microseconds() % US_PER_SEC);
56 }
57
60 int ms_of_sec() {
61 return (int)((microseconds() / 1000) % MS_PER_SEC);
62 }
63
66 long ts() {
67 return microseconds() / US_PER_SEC;
68 }
69
72 long timestamp() { return ts(); }
73
76 double fts() {
77 return (double)microseconds() / (double)US_PER_SEC;
78 }
79
82 double ftimestamp() { return fts(); }
83
86 long ts_ms() {
87 return microseconds() / 1000;
88 }
89
92 long timestamp_ms() { return ts_ms(); }
93
96 long now() { return ts_ms(); }
97
100 long ts_us() {
101 return microseconds();
102 }
103
106 long timestamp_us() { return ts_us(); }
107
109
110}; // namespace time_shield
111
112#endif // __TIME_SHIELD_TIME_UTILS_MQH__
constexpr int64_t NS_PER_US
Nanoseconds per microsecond.
Definition constants.hpp:33
constexpr int64_t MS_PER_SEC
Milliseconds per second.
Definition constants.hpp:39
constexpr int64_t US_PER_SEC
Microseconds per second.
Definition constants.hpp:38
const ts_us_t timestamp_us() noexcept
Get the current UTC timestamp in microseconds.
const ts_ms_t now() noexcept
Get the current UTC timestamp in milliseconds.
const ts_t ts() noexcept
Get the current UTC timestamp in seconds.
long microseconds()
Get the number of microseconds since the UNIX epoch.
const T us_of_sec() noexcept
Get the microsecond part of the current second.
const ts_t timestamp() noexcept
Get the current UTC timestamp in seconds.
const fts_t ftimestamp() noexcept
Get the current UTC timestamp in floating-point seconds.
const T ns_of_sec() noexcept
Get the nanosecond part of the current second.
const ts_us_t ts_us() noexcept
Get the current UTC timestamp in microseconds.
const ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
const ts_ms_t timestamp_ms() noexcept
Get the current UTC timestamp in milliseconds.
const fts_t fts() noexcept
Get the current UTC timestamp in floating-point seconds.
const T ms_of_sec() noexcept
Get the millisecond part of the current second.
Main namespace for the Time Shield library.