-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTimeManipulation.mqh
99 lines (85 loc) · 3.3 KB
/
TimeManipulation.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
//+------------------------------------------------------------------+
//| TimeManipulation.mqh |
//| Copyright 2022, Homma.tech |
//| https://www.homma.tech |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, Homma.tech"
#property link "https://www.homma.tech"
#include "NewTypes.mqh"
class CTimeManipulation {
public:
string CTimeManipulation :: IntTimeToString (int time);
int CTimeManipulation :: StringTimeToInt (
string time,
ENUM_TIME_STRING_TO_INT_TYPES selector);
void CTimeManipulation :: TimeStringTokenizer (
string time,
string &tokenizedTime[]);
};
//+------------------------------------------------------------------+
//| Timestamp to string function |
//+------------------------------------------------------------------+
string CTimeManipulation :: IntTimeToString (int time) {
string timeOnString;
int temp;
temp = time / 3600;
if(temp < 10)
StringAdd(timeOnString, ("0" + IntegerToString(temp) + ":"));
else
StringAdd(timeOnString, (IntegerToString(temp) + ":"));
time -= temp * 3600;
temp = time / 60;
if(temp < 10)
StringAdd(timeOnString, ("0" + IntegerToString(temp) + ":"));
else
StringAdd(timeOnString, (IntegerToString(temp) + ":"));
time -= temp * 60;
if(time < 10)
StringAdd(timeOnString, ("0" + IntegerToString(time)));
else
StringAdd(timeOnString, IntegerToString(time));
return timeOnString;
}
//+------------------------------------------------------------------+
//| String to timestamp function |
//+------------------------------------------------------------------+
int CTimeManipulation :: StringTimeToInt (string time,
ENUM_TIME_STRING_TO_INT_TYPES selector) {
string tokenizedTime[];
int absoluteValueTime = 0;
TimeStringTokenizer(time, tokenizedTime);
switch (selector) {
case HHMMSS:
absoluteValueTime = (int)(StringToInteger(tokenizedTime[0]) * 3600);
absoluteValueTime += (int)(StringToInteger(tokenizedTime[1]) * 60);
absoluteValueTime += (int)StringToInteger(tokenizedTime[2]);
break;
case HHMM:
absoluteValueTime = (int)(StringToInteger(tokenizedTime[0]) * 3600);
absoluteValueTime += (int)(StringToInteger(tokenizedTime[1]) * 60);
break;
case MMSS:
absoluteValueTime = (int)(StringToInteger(tokenizedTime[0]) * 60);
absoluteValueTime += (int)StringToInteger(tokenizedTime[1]);
break;
case HH:
absoluteValueTime = (int)(StringToInteger(tokenizedTime[0]) * 3600);
break;
case MM:
absoluteValueTime = (int)(StringToInteger(tokenizedTime[0]) * 60);
break;
case SS:
absoluteValueTime = (int)StringToInteger(tokenizedTime[0]);
break;
}
return absoluteValueTime;
}
//+------------------------------------------------------------------+
//| Time tokenizer function |
//+------------------------------------------------------------------+
void CTimeManipulation :: TimeStringTokenizer (
string time,
string &tokenizedTime[]) {
StringSplit(time, ':', tokenizedTime);
}
//+------------------------------------------------------------------+