-
Notifications
You must be signed in to change notification settings - Fork 4
/
Datetime.mqh
576 lines (576 loc) · 16.8 KB
/
Datetime.mqh
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//+------------------------------------------------------------------+
//| DateTime.mqh |
//| Copyright 2009-2013, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Structure CDateTime. |
//| Purpose: Working with dates and time. |
//| Extends the MqlDateTime structure. |
//+------------------------------------------------------------------+
struct CDateTime : public MqlDateTime
{
//--- additional information
string MonthName(const int num) const;
string ShortMonthName(const int num) const;
string DayName(const int num) const;
string ShortDayName(const int num) const;
string MonthName(void) const { return(MonthName(mon)); }
string ShortMonthName(void) const { return(ShortMonthName(mon)); }
string DayName(void) const { return(DayName(day_of_week)); }
string ShortDayName(void) const { return(ShortDayName(day_of_week)); }
int DaysInMonth(void) const;
//--- data access
datetime DateTime(void) { return(StructToTime(this)); }
void DateTime(const datetime value) { TimeToStruct(value,this); }
void DateTime(const MqlDateTime& value) { this=value; }
void Date(const datetime value);
void Date(const MqlDateTime &value);
void Time(const datetime value);
void Time(const MqlDateTime &value);
//--- settings
void Sec(const int value);
void Min(const int value);
void Hour(const int value);
void Day(const int value);
void Mon(const int value);
void Year(const int value);
//--- increments
void SecDec(int delta=1);
void SecInc(int delta=1);
void MinDec(int delta=1);
void MinInc(int delta=1);
void HourDec(int delta=1);
void HourInc(int delta=1);
void DayDec(int delta=1);
void DayInc(int delta=1);
void MonDec(int delta=1);
void MonInc(int delta=1);
void YearDec(int delta=1);
void YearInc(int delta=1);
//--- check
void DayCheck(void);
};
//+------------------------------------------------------------------+
//| Gets month name |
//+------------------------------------------------------------------+
string CDateTime::MonthName(const int num) const
{
switch(num)
{
case 1: return("January");
case 2: return("February");
case 3: return("March");
case 4: return("April");
case 5: return("May");
case 6: return("June");
case 7: return("July");
case 8: return("August");
case 9: return("September");
case 10: return("October");
case 11: return("November");
case 12: return("December");
}
//---
return("Bad month");
}
//+------------------------------------------------------------------+
//| Gets short name of month |
//+------------------------------------------------------------------+
string CDateTime::ShortMonthName(const int num) const
{
switch(num)
{
case 1: return("jan");
case 2: return("feb");
case 3: return("mar");
case 4: return("apr");
case 5: return("may");
case 6: return("jun");
case 7: return("jul");
case 8: return("aug");
case 9: return("sep");
case 10: return("oct");
case 11: return("nov");
case 12: return("dec");
}
//---
return("Bad month");
}
//+------------------------------------------------------------------+
//| Gets name of week day |
//+------------------------------------------------------------------+
string CDateTime::DayName(const int num) const
{
switch(num)
{
case 0: return("Sunday");
case 1: return("Monday");
case 2: return("Tuesday");
case 3: return("Wednesday");
case 4: return("Thursday");
case 5: return("Friday");
case 6: return("Saturday");
}
//---
return("Bad day of week");
}
//+------------------------------------------------------------------+
//| Gets short name of week day |
//+------------------------------------------------------------------+
string CDateTime::ShortDayName(const int num) const
{
switch(num)
{
case 0: return("Su");
case 1: return("Mo");
case 2: return("Tu");
case 3: return("We");
case 4: return("Th");
case 5: return("Fr");
case 6: return("Sa");
}
//---
return("Bad day of week");
}
//+------------------------------------------------------------------+
//| Gets number of days in month |
//+------------------------------------------------------------------+
int CDateTime::DaysInMonth(void) const
{
switch(mon)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return(31);
case 2:
return((year%4==0)? 29 : 28);
case 4:
case 6:
case 9:
case 11:
return(30);
}
//---
return(0);
}
//+------------------------------------------------------------------+
//| Sets date |
//+------------------------------------------------------------------+
void CDateTime::Date(const datetime value)
{
MqlDateTime dt;
//--- convert to structure
TimeToStruct(value,dt);
//--- set
Date(dt);
}
//+------------------------------------------------------------------+
//| Sets date |
//+------------------------------------------------------------------+
void CDateTime::Date(const MqlDateTime &value)
{
day =value.day;
mon =value.mon;
year=value.year;
//--- check if day is correct
DayCheck();
}
//+------------------------------------------------------------------+
//| Sets time |
//+------------------------------------------------------------------+
void CDateTime::Time(const datetime value)
{
MqlDateTime dt;
//--- convert to structure
TimeToStruct(value,dt);
//--- set
Time(dt);
}
//+------------------------------------------------------------------+
//| Sets time |
//+------------------------------------------------------------------+
void CDateTime::Time(const MqlDateTime &value)
{
hour=value.hour;
min =value.min;
sec =value.sec;
}
//+------------------------------------------------------------------+
//| Sets seconds |
//+------------------------------------------------------------------+
void CDateTime::Sec(const int value)
{
//--- check and set
if(value>=0 && value<60)
sec=value;
}
//+------------------------------------------------------------------+
//| Sets minutes |
//+------------------------------------------------------------------+
void CDateTime::Min(const int value)
{
//--- check and set
if(value>=0 && value<60)
min=value;
}
//+------------------------------------------------------------------+
//| Sets hours |
//+------------------------------------------------------------------+
void CDateTime::Hour(const int value)
{
//--- check and set
if(value>=0 && value<24)
hour=value;
}
//+------------------------------------------------------------------+
//| Sets day of month |
//+------------------------------------------------------------------+
void CDateTime::Day(const int value)
{
//--- check and set
if(value>0 && value<=DaysInMonth())
{
day=value;
//--- check if day is correct
DayCheck();
}
}
//+------------------------------------------------------------------+
//| Sets month |
//+------------------------------------------------------------------+
void CDateTime::Mon(const int value)
{
//--- check and set
if(value>0 && value<=12)
{
mon=value;
//--- check if day is correct
DayCheck();
}
}
//+------------------------------------------------------------------+
//| Sets year |
//+------------------------------------------------------------------+
void CDateTime::Year(const int value)
{
//--- check and set
if(value>=1970)
{
year=value;
//--- check if day is correct
DayCheck();
}
}
//+------------------------------------------------------------------+
//| Subtracts specified number of seconds |
//+------------------------------------------------------------------+
void CDateTime::SecDec(int delta)
{
//--- if increment is 0 - exit
if(delta==0)
return;
//--- if increment is negative - inverse the operation
if(delta<0)
{
SecInc(-delta);
return;
}
//--- check if subtract from upper number positions
if(delta>60)
{
MinDec(delta/60);
delta%=60;
}
sec-=delta;
if(sec<0)
{
sec+=60;
MinDec();
}
}
//+------------------------------------------------------------------+
//| Adds specified number of seconds |
//+------------------------------------------------------------------+
void CDateTime::SecInc(int delta)
{
//--- if increment is 0 - exit
if(delta==0)
return;
//--- if increment is negative - inverse the operation
if(delta<0)
{
SecDec(-delta);
return;
}
//--- check if add to upper number positions
if(delta>60)
{
MinInc(delta/60);
delta%=60;
}
sec+=delta;
if(sec>=60)
{
sec-=60;
MinInc();
}
}
//+------------------------------------------------------------------+
//| Subtracts specified number of minutes |
//+------------------------------------------------------------------+
void CDateTime::MinDec(int delta)
{
//--- if increment is 0 - exit
if(delta==0)
return;
//--- if increment is negative - inverse the operation
if(delta<0)
{
MinInc(-delta);
return;
}
//--- check if subtract from upper number positions
if(delta>60)
{
HourDec(delta/60);
delta%=60;
}
min-=delta;
if(min<0)
{
min+=60;
HourDec();
}
}
//+------------------------------------------------------------------+
//| Adds specified number of minutes |
//+------------------------------------------------------------------+
void CDateTime::MinInc(int delta)
{
//--- if increment is 0 - exit
if(delta==0)
return;
//--- if increment is negative - inverse the operation
if(delta<0)
{
MinDec(-delta);
return;
}
//--- check if add to upper number positions
if(delta>60)
{
HourInc(delta/60);
delta%=60;
}
min+=delta;
if(min>=60)
{
min-=60;
HourInc();
}
}
//+------------------------------------------------------------------+
//| Subtracts specified number of hours |
//+------------------------------------------------------------------+
void CDateTime::HourDec(int delta)
{
//--- if increment is 0 - exit
if(delta==0)
return;
//--- if increment is negative - inverse the operation
if(delta<0)
{
HourInc(-delta);
return;
}
//--- check if subtract from upper number positions
if(delta>24)
{
DayDec(delta/24);
delta%=24;
}
hour-=delta;
if(hour<0)
{
hour+=24;
DayDec();
}
}
//+------------------------------------------------------------------+
//| Adds specified number of hours |
//+------------------------------------------------------------------+
void CDateTime::HourInc(int delta)
{
//--- if increment is 0 - exit
if(delta==0)
return;
//--- if increment is negative - inverse the operation
if(delta<0)
{
HourDec(-delta);
return;
}
//--- check if add to upper number positions
if(delta>24)
{
DayInc(delta/24);
delta%=24;
}
hour+=delta;
if(hour>=24)
{
hour-=24;
DayInc();
}
}
//+------------------------------------------------------------------+
//| Subtracts specified number of days |
//+------------------------------------------------------------------+
void CDateTime::DayDec(int delta)
{
//--- if increment is 0 - exit
if(delta==0)
return;
//--- if increment is negative - inverse the operation
if(delta<0)
{
DayInc(-delta);
return;
}
//--- uncertain condition, as the number of days in month can differ
while(day<=delta)
{
delta-=day;
MonDec();
day=DaysInMonth();
}
day-=delta;
//--- check if day is correct
DayCheck();
}
//+------------------------------------------------------------------+
//| Adds specified number of days |
//+------------------------------------------------------------------+
void CDateTime::DayInc(int delta)
{
//--- if increment is 0 - exit
if(delta==0)
return;
//--- if increment is negative - inverse the operation
if(delta<0)
{
DayDec(-delta);
return;
}
//--- uncertain condition, as the number of days in month can differ
while(DaysInMonth()-day<delta)
{
delta-=DaysInMonth()-day+1;
MonInc();
day=1;
}
day+=delta;
//--- check if day is correct
DayCheck();
}
//+------------------------------------------------------------------+
//| Subtracts specified number of months |
//+------------------------------------------------------------------+
void CDateTime::MonDec(int delta)
{
//--- if increment is 0 - exit
if(delta==0)
return;
//--- if increment is negative - inverse the operation
if(delta<0)
{
MonInc(-delta);
return;
}
//--- check if subtract from upper number positions
if(delta>12)
{
YearDec(delta/12);
delta%=12;
}
mon-=delta;
if(mon<=0)
{
mon+=12;
YearDec();
}
//--- check if day is correct
DayCheck();
}
//+------------------------------------------------------------------+
//| Adds specified number of months |
//+------------------------------------------------------------------+
void CDateTime::MonInc(int delta)
{
//--- if increment is 0 - exit
if(delta==0)
return;
//--- if increment is negative - inverse the operation
if(delta<0)
{
MonDec(-delta);
return;
}
//--- check if add to upper number positions
if(delta>12)
{
YearInc(delta/12);
delta%=12;
}
mon+=delta;
if(mon>12)
{
mon-=12;
YearInc();
}
//--- check if day is correct
DayCheck();
}
//+------------------------------------------------------------------+
//| Subtracts specified number of years |
//+------------------------------------------------------------------+
void CDateTime::YearDec(int delta)
{
//--- if increment is 0 - exit
if(delta!=0)
{
year-=delta;
//--- check if day is correct
DayCheck();
}
}
//+------------------------------------------------------------------+
//| Adds specified number of years |
//+------------------------------------------------------------------+
void CDateTime::YearInc(int delta)
{
//--- if increment is 0 - exit
if(delta!=0)
{
year+=delta;
//--- check if day is correct
DayCheck();
}
}
//+------------------------------------------------------------------+
//| Checks if day number is correct |
//+------------------------------------------------------------------+
void CDateTime::DayCheck(void)
{
if(day>DaysInMonth())
day=DaysInMonth();
//--- this is required to get day of week and day of year
TimeToStruct(StructToTime(this),this);
}
//+------------------------------------------------------------------+