-
Notifications
You must be signed in to change notification settings - Fork 0
/
datetime.cpp
113 lines (99 loc) · 2.59 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
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
#pragma warning(disable:4996)
#include "datetime.h"
#include <cstdint>
#include <string>
DateTime::DateTime() {
time_t t = time(0);
tm* now = localtime(&t);
this->Year = now->tm_year + 1900;
this->Month = now->tm_mon;
this->Day = now->tm_mday;
this->Hour = now->tm_hour;
this->Minute = now->tm_min;
this->Second = now->tm_sec;
}
DateTime::DateTime(uint16_t year, uint16_t month, uint16_t day, uint16_t hour, uint16_t minute, uint16_t second) {
this->Year = year;
this->Month = month;
this->Day = day;
this->Hour = hour;
this->Minute = minute;
this->Second = second;
}
DateTime::DateTime(uint16_t hour, uint16_t minute, uint16_t second) {
time_t t = time(0);
tm* now = localtime(&t);
this->Year = now->tm_year + 1900;
this->Month = now->tm_mon;
this->Day = now->tm_mday;
this->Hour = hour;
this->Minute = minute;
this->Second = second;
}
std::string DateTime::ToString() {
return std::to_string(this->Year) + "/" + std::to_string(this->Month) + "/" + std::to_string(this->Day) + " " + (this->Hour > 10 ? "" : "0") + std::to_string(this->Hour) + ":" + (this->Minute > 10 ? "" : "0") + std::to_string(this->Minute) + ":" + (this->Second > 10 ? "" : "0") + std::to_string(this->Second);
}
Date DateTime::getDate() {
::Date d;
d.year = this->Year;
d.month = this->Month;
d.day = this->Day;
return d;
}
Time DateTime::getTime() {
::Time t;
t.hour = this->Hour;
t.minute = this->Minute;
t.second = this->Second;
return t;
}
void DateTime::Now() {
time_t t = time(0);
tm* now = localtime(&t);
Year = now->tm_year + 1900;
Month = now->tm_mon;
Day = now->tm_mday;
Hour = now->tm_hour;
Minute = now->tm_min;
Second = now->tm_sec;
}
bool DateTime::operator==(DateTime t) {
if (this->Year != t.Year)
return false;
if (this->Month != t.Month)
return false;
if (this->Day != t.Day)
return false;
if (this->Hour != t.Hour)
return false;
if (this->Minute != t.Minute)
return false;
if (this->Day != t.Day)
return false;
return true;
}
bool DateTime::operator!=(DateTime t) {
return !(*this == t);
}
bool DateTime::operator<(DateTime t) {
if (this->Year != t.Year)
return this->Year < t.Year;
if (this->Month != t.Month)
return this->Month < t.Month;
if (this->Day != t.Day)
return this->Day < t.Day;
if (this->Hour != t.Hour)
return this->Hour < t.Hour;
if (this->Minute != t.Minute)
return this->Minute < t.Minute;
return this->Second < t.Second;
}
bool DateTime::operator<=(DateTime t) {
return ((*this < t) || (*this == t));
}
bool DateTime::operator>(DateTime t) {
return !(*this <= t);
}
bool DateTime::operator>=(DateTime t) {
return ((*this > t) || (*this == t));
}