-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateTime.h
83 lines (74 loc) · 2.04 KB
/
DateTime.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
// Implementation of a Date Time class
// Diego Enrique Jiménez Urgell A01652617
// 2/12/2020
class DateTime{
private:
int day;
string month;
vector<int> * hour;
/**
* Auxiliary method designed to parse an hour given in a string. The numbers are stored in a vector of integers
* @param hour A string containg the hour, in the format "HH:MM:SS"
* @return A vector that contains three different integers. The first one for the hour, then minute, and finally seconds.
*/
vector<int> * parseHour(string hour){
vector<int> * hourInt = new vector<int>();
int delim = 0;
string tmp = "";
for(int i = 0; i < 2; i++){
delim = findIndex(hour, 58); // 58 is used because it is the ASCII decimal code of the colon
tmp = hour.substr(0, delim);
hourInt -> push_back(stoi(tmp));
hour = hour.substr(delim + 1);
}
hourInt -> push_back(stoi(hour));
return hourInt;
}
public:
/**
*
*/
DateTime(){
this -> hour = NULL;
this -> day = 0;
this -> month = "";
}
/**
*
* @param m
* @param d
* @param h
*/
DateTime(string m, int d, string h){
this -> hour = parseHour(h);
this -> day = d;
this -> month = m;
}
/**
*
* @param date2
* @return
*/
bool operator == (DateTime * date2){
if (this -> day != date2 -> day && this -> month.compare(date2 -> month) != 0) return false;
bool equal = true;
for(int i = 0; i < 3; i++){
if ((*(this -> hour))[i] != (*(date2 -> hour))[i]){
equal = false;
break;
}
}
return equal;
}
/**
*
* @param os
* @param d
* @return
*/
friend ostream& operator << (ostream &os, DateTime * d){
os << d -> month << " " << d -> day << " ";
os << (*(d -> hour))[0] << ":" << (*(d -> hour))[1] << ":" << (*(d -> hour))[2];
return os;
}
};