-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
DateTime.struct.h
423 lines (405 loc) · 11.4 KB
/
DateTime.struct.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2023, 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/>.
*
*/
/**
* @file
* Includes DateTime's structs.
*/
#ifndef __MQL__
// Allows the preprocessor to include a header file when it is needed.
#pragma once
#endif
// Forward declarations.
struct DateTimeStatic;
// Includes.
#include "DateTime.enum.h"
#include "Std.h"
#ifndef __MQLBUILD__
/**
* The date type structure.
*
* @see:
* - https://docs.mql4.com/constants/structures/mqldatetime
* - https://www.mql5.com/en/docs/constants/structures/mqldatetime
*/
struct MqlDateTime {
int year; // Year.
int mon; // Month.
int day; // Day of month.
int hour; // Hour.
int min; // Minute.
int sec; // Second.
int day_of_week; // Zero-based day number of week (0-Sunday, 1-Monday, ... ,6-Saturday).
int day_of_year; // Zero-based day number of the year (1st Jan = 0).
};
#endif
/*
* Struct to provide static date and time methods.
*/
struct DateTimeStatic {
/**
* Returns the current day of the month (e.g. the day of month of the last known server time).
*/
static int Day(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
#ifdef __MQL4__
return ::TimeDay(dt);
#else
MqlDateTime _dt;
TimeToStruct(dt, _dt);
return _dt.day;
#endif
}
/**
* Returns the current zero-based day of the week of the last known server time.
*/
static int DayOfWeek(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
#ifdef __MQL4__
return ::DayOfWeek();
#else
MqlDateTime _dt;
TimeToStruct(dt, _dt);
return _dt.day_of_week;
#endif
}
/**
* Returns the current day of the year (e.g. the day of year of the last known server time).
*/
static int DayOfYear(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
#ifdef __MQL4__
return ::DayOfYear();
#else
MqlDateTime _dt;
TimeToStruct(dt, _dt);
return _dt.day_of_year + 1;
#endif
}
/**
* Returns the hour of the last known server time by the moment of the program start.
*/
static int Hour(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
#ifdef __MQL4__
return ::Hour();
#else
MqlDateTime _dt;
TimeToStruct(dt, _dt);
return _dt.hour;
#endif
}
/**
* Check whether market is within peak hours.
*/
static bool IsPeakHour() {
MqlDateTime dt;
TimeToStruct(::TimeGMT(), dt);
return dt.hour >= 8 && dt.hour <= 16;
}
/**
* Returns the current minute of the last known server time by the moment of the program start.
*/
static int Minute(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
#ifdef __MQL4__
return ::Minute();
#else
MqlDateTime _dt;
TimeToStruct(dt, _dt);
return _dt.min;
#endif
}
/**
* Returns the current month as number (e.g. the number of month of the last known server time).
*/
static int Month(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
#ifdef __MQL4__
return ::Month();
#else
MqlDateTime _dt;
TimeToStruct(dt, _dt);
return _dt.mon;
#endif
}
/**
* Returns the amount of seconds elapsed from the beginning of the current minute of the last known server time.
*/
static int Seconds(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
#ifdef __MQL4__
return ::Seconds();
#else
MqlDateTime _dt;
TimeToStruct(dt, _dt);
return _dt.sec;
#endif
}
/**
* Converts a time stamp into a string of "yyyy.mm.dd hh:mi" format.
*/
static string TimeToStr(datetime value, int mode = TIME_DATE | TIME_MINUTES | TIME_SECONDS) {
#ifdef __MQL4__
return ::TimeToStr(value, mode);
#else // __MQL5__
// #define TimeToStr(value, mode) DateTime::TimeToStr(value, mode)
return ::TimeToString(value, mode);
#endif
}
static string TimeToStr(int mode = TIME_DATE | TIME_MINUTES | TIME_SECONDS) { return TimeToStr(TimeCurrent(), mode); }
/**
* Returns the current time of the trade server.
*/
static datetime TimeTradeServer() {
#ifdef __MQL4__
// Unlike MQL5 TimeTradeServer(),
// TimeCurrent() returns the last known server time.
return ::TimeCurrent();
#else
// The calculation of the time value is performed in the client terminal
// and depends on the time settings of your computer.
return ::TimeTradeServer();
#endif
}
/**
* Returns the current year (e.g. the year of the last known server time).
*/
static int Year(datetime dt = 0) {
if (dt == 0) {
dt = TimeCurrent();
}
#ifdef __MQL4__
return ::Year();
#else
MqlDateTime _dt;
TimeToStruct(dt, _dt);
return _dt.year;
#endif
}
};
struct DateTimeEntry : public MqlDateTime {
int week_of_year;
// Struct constructors.
DateTimeEntry() { Set(); }
DateTimeEntry(datetime _dt) { Set(_dt); }
DateTimeEntry(int _year, int _mon, int _day, int _hour = 0, int _min = 0, int _sec = 0) {
year = _year;
mon = _mon;
day = _day;
hour = _hour;
min = _min;
sec = _sec;
Recalculate();
}
DateTimeEntry(MqlDateTime& _dt) {
Set(_dt);
// In MqlDateTime, 1st Jan is assigned the number value of zero.
day_of_year = day_of_year + 1;
#ifndef __MQL__
throw NotImplementedException();
#endif
}
// Getters.
int GetDayOfMonth() { return day; }
int GetDayOfWeek(bool _recalc = true) {
// Returns the zero-based day of week.
// (0-Sunday, 1-Monday, ... , 6-Saturday).
if (!_recalc) {
return day_of_week;
}
// Calculates day of the week using the Tomohiko Sakamoto Algorithm.
// @see: https://iq.opengenus.org/tomohiko-sakamoto-algorithm/
// @see: https://stackoverflow.com/a/64923433/55075
int _days[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
int _year = mon < 3 ? year - 1 : year;
day_of_week = (_year + _year / 4 - _year / 100 + _year / 400 + _days[mon - 1] + day) % 7;
return day_of_week;
}
// Gets day of the year.
int GetDayOfYear(bool _recalc = false) {
if (!_recalc) {
return day_of_year;
}
int _days_to_month[2][12] = {
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335},
};
// @see: https://stackoverflow.com/a/19111202/55075
day_of_year = _days_to_month[IsLeapYear() ? 1 : 0][mon - 1] + day;
return day_of_year;
}
// Calculates the week of the year based on the day of the year.
// @see: https://stackoverflow.com/a/274913/55075
int GetWeekOfYear(bool _recalc = false) {
if (!_recalc) {
return week_of_year;
}
if (day == 1 && mon == 1) {
return 1;
}
int doy = GetDayOfYear();
int dow = GetDayOfWeek();
DateTimeEntry _dte(year, 1, 1);
int dow1j = _dte.GetDayOfWeek();
week_of_year = (doy + 6) / 7;
// Adjust for being after Saturday of 1st week.
week_of_year = dow < dow1j ? week_of_year + 1 : week_of_year;
return week_of_year;
}
int GetHour() { return hour; }
int GetMinute() { return min; }
int GetMonth() { return mon; }
int GetSeconds() { return sec; }
// int GetWeekOfYear() { return week_of_year; } // @todo
int GetValue(ENUM_DATETIME_UNIT _unit) {
int _result = -1;
switch (_unit) {
case DATETIME_SECOND:
return GetSeconds();
case DATETIME_MINUTE:
return GetMinute();
case DATETIME_HOUR:
return GetHour();
case DATETIME_DAY:
return GetDayOfMonth();
case DATETIME_WEEK:
return -1; // return WeekOfYear(); // @todo
case DATETIME_MONTH:
return GetMonth();
case DATETIME_YEAR:
return GetYear();
default:
break;
}
return _result;
}
unsigned int GetValue(unsigned int _unit) {
if ((_unit & (DATETIME_DAY | DATETIME_WEEK)) != 0) {
return GetDayOfWeek();
} else if ((_unit & (DATETIME_DAY | DATETIME_MONTH)) != 0) {
return GetDayOfMonth();
} else if ((_unit & (DATETIME_DAY | DATETIME_YEAR)) != 0) {
return GetDayOfYear();
} else if ((_unit & (DATETIME_WEEK | DATETIME_YEAR)) != 0) {
return GetWeekOfYear();
}
return GetValue((ENUM_DATETIME_UNIT)_unit);
}
int GetYear() { return year; }
datetime GetTimestamp() { return StructToTime(THIS_REF); }
bool IsLeapYear() { return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); }
// Recalculate
void Recalculate() {
day_of_week = GetDayOfWeek(true);
day_of_year = GetDayOfYear(true);
week_of_year = GetWeekOfYear(true);
}
// Setters.
void Set() {
TimeToStruct(::TimeCurrent(), THIS_REF);
Recalculate();
}
void SetGMT() {
TimeToStruct(::TimeGMT(), THIS_REF);
Recalculate();
}
// Set date and time.
void Set(datetime _time) {
TimeToStruct(_time, THIS_REF);
Recalculate();
}
// Set date and time.
void Set(MqlDateTime& _time) {
THIS_REF = _time;
Recalculate();
}
void SetDayOfMonth(int _value) {
day = _value;
day_of_week = DateTimeStatic::DayOfWeek(); // Zero-based day of week.
day_of_year = DateTimeStatic::DayOfYear(); // Zero-based day of year.
}
void SetDayOfYear(int _value) {
day_of_year = _value - 1; // Sets zero-based day of year.
day = DateTimeStatic::Month(); // Sets day of month (1..31).
day_of_week = DateTimeStatic::DayOfWeek(); // Zero-based day of week.
}
void SetHour(int _value) { hour = _value; }
void SetMinute(int _value) { min = _value; }
void SetMonth(int _value) { mon = _value; }
void SetSeconds(int _value) { sec = _value; }
void SetWeekOfYear(int _value) {
week_of_year = _value;
// day = @todo;
// day_of_week = @todo;
// day_of_year = @todo;
}
void SetValue(ENUM_DATETIME_UNIT _unit, int _value) {
switch (_unit) {
case DATETIME_SECOND:
SetSeconds(_value);
break;
case DATETIME_MINUTE:
SetMinute(_value);
break;
case DATETIME_HOUR:
SetHour(_value);
break;
case DATETIME_DAY:
SetDayOfMonth(_value);
break;
case DATETIME_WEEK:
SetWeekOfYear(_value);
break;
case DATETIME_MONTH:
SetMonth(_value);
break;
case DATETIME_YEAR:
SetYear(_value);
break;
default:
break;
}
}
void SetValue(unsigned short _unit, int _value) {
if ((_unit & (DATETIME_DAY | DATETIME_MONTH)) != 0) {
SetDayOfMonth(_value);
} else if ((_unit & (DATETIME_DAY | DATETIME_YEAR)) != 0) {
SetDayOfYear(_value);
} else {
SetValue((ENUM_DATETIME_UNIT)_unit, _value);
}
}
void SetYear(int _value) { year = _value; }
};