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
21#include "constants.mqh"
22
23
24namespace 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_TIME_UTILS_MQH__
Header file with time-related constants.
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.