-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDateTime.cpp
56 lines (50 loc) · 1.93 KB
/
DateTime.cpp
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
#include "DateTime.h"
std::string DateTime::toZeroPaddedString(unsigned int value, unsigned int targetLength) {
std::string outputString = std::to_string(value);
if (outputString.size() < targetLength) {
std::string appendString(targetLength - outputString.size(), '0');
outputString = appendString + outputString;
}
return outputString;
}
DateTime::DateTime(unsigned int year, unsigned short month, unsigned short day, unsigned short hour,
unsigned short minute) {
this->year = year;
this->month = month;
this->day = day;
this->hour = hour;
this->minute = minute;
}
bool DateTime::operator<(DateTime &other) {
return this->getDateTimeString() < other.getDateTimeString();
}
bool DateTime::operator>(DateTime &other) {
return this->getDateTimeString() > other.getDateTimeString();
}
bool DateTime::operator==(DateTime &other) {
return this->getDateTimeString() == other.getDateTimeString();
}
bool DateTime::operator!=(DateTime &other) {
return this->getDateTimeString() != other.getDateTimeString();
}
bool DateTime::operator<=(DateTime &other) {
return this->getDateTimeString() <= other.getDateTimeString();
}
bool DateTime::operator>=(DateTime &other) {
return this->getDateTimeString() >= other.getDateTimeString();
}
// Function to convert this into a string
DateTime::operator std::string() const {
return std::to_string(year) + "/" + toZeroPaddedString(month, 2) + "/" +
toZeroPaddedString(day, 2) + " " + toZeroPaddedString(hour, 2) + ":" +
toZeroPaddedString(minute, 2);
}
std::string DateTime::getDateString() {
return std::to_string(year) + toZeroPaddedString(month, 2) +
toZeroPaddedString(day, 2);
}
std::string DateTime::getDateTimeString() {
return std::to_string(year) + toZeroPaddedString(month, 2) +
toZeroPaddedString(day, 2) + toZeroPaddedString(hour, 2) +
toZeroPaddedString(minute, 2);
}