-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathPlatformTime.h
87 lines (73 loc) · 2.81 KB
/
PlatformTime.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2022, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+
/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Includes.
#include "DateTime.enum.h"
/**
* @file
* Platform time retrieval.
*/
#ifndef __MQL__
#pragma once
// Includes.
#include <chrono>
#include <ctime>
#include "DateTime.struct.h"
#endif
class PlatformTime {
static MqlDateTime current_time;
static int64 current_timestamp_s;
static int64 current_timestamp_ms;
public:
static int64 CurrentTimestamp() { return current_timestamp_s; }
static int64 CurrentTimestampMs() { return current_timestamp_ms; }
static MqlDateTime CurrentTime() { return current_time; }
void static Tick() {
#ifdef __MQL__
static int64 _last_timestamp_ms = 0;
current_timestamp_s = ::TimeCurrent(current_time);
current_timestamp_ms = (int64)GetTickCount();
if (_last_timestamp_ms != 0 && current_timestamp_ms < _last_timestamp_ms) {
// Overflow occured (49.7 days passed).
// More info: https://docs.mql4.com/common/gettickcount
current_timestamp_ms += _last_timestamp_ms;
}
_last_timestamp_ms = current_timestamp_ms;
#else
using namespace std::chrono;
current_timestamp_s = (int64)duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
current_timestamp_ms = (int64)duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
using namespace std::chrono;
std::time_t t = current_timestamp_s;
std::tm* now = std::localtime(&t);
current_time.day = now->tm_mday;
current_time.day_of_week = now->tm_wday;
current_time.day_of_year = now->tm_yday;
current_time.hour = now->tm_hour;
current_time.min = now->tm_min;
current_time.mon = now->tm_mon;
current_time.sec = now->tm_sec;
current_time.year = now->tm_year;
#endif
}
};
MqlDateTime PlatformTime::current_time = {0, 0, 0, 0, 0, 0, 0, 0};
int64 PlatformTime::current_timestamp_s = 0;
int64 PlatformTime::current_timestamp_ms = 0;