-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDate.cpp
More file actions
200 lines (162 loc) · 4.57 KB
/
Date.cpp
File metadata and controls
200 lines (162 loc) · 4.57 KB
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
/*
Date.cpp
Name: Yuansheng Lu
Student ID: 136654167
E-mail: ylu140@myseneca.ca
*/
#include "Date.h"
#include "general.h"
using namespace std;
namespace sict{
// This function returns a unique integer number based on the date
// This value is used to compare two dates
int Date::value()const {
return year_ * 372 + mon_ * 31 + day_;
}
// Returns the number of days in a month
int Date::mdays()const {
int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, -1 };
int mon = mon_ >= 1 && mon_ <= 12 ? mon_ : 13;
mon--;
return days[mon] + int((mon == 1)*((year_ % 4 == 0) && (year_ % 100 != 0)) || (year_ % 400 == 0));
}
// Set the readErrorCode_ member-variable
void Date::errCode(int errorCode) {
readErrorCode_ = errorCode;
}
Date::Date() {
year_ = 0;
mon_ = 0;
day_ = 0;
readErrorCode_ = NO_ERROR;
}
Date::Date(int year, int mon, int day) {
year_ = year;
mon_ = mon;
day_ = day;
readErrorCode_ = NO_ERROR;
}
// Reads the date in the following format:
// YYYY?MM?DD (e.g. 2016/03/24 or 2016-03-24) from the console
istream& Date::read(istream& istr) {
// Store the input data
// If not in correct format, set the error message to CIN_FAILED
istr >> year_;
if (istr.fail()) {
errCode(CIN_FAILED);
return istr;
}
if (istr.get() != '/' && istr.get() != '-') {
errCode(CIN_FAILED);
return istr;
}
istr >> mon_;
if (istr.fail()) {
errCode(CIN_FAILED);
return istr;
}
if (istr.get() != '/' && istr.get() != '-') {
errCode(CIN_FAILED);
return istr;
}
istr >> day_;
if (istr.fail()) {
errCode(CIN_FAILED);
return istr;
}
// Validate the data received in range
if (year_ < MIN_YEAR || year_ > MAX_YEAR)
errCode(YEAR_ERROR);
else if (mon_ < 1 || mon_ > 12)
errCode(MON_ERROR);
else if (day_ < 1 || day_ > mdays())
errCode(DAY_ERROR);
else
errCode(NO_ERROR);
return istr;
}
// Writes the date in the following format: YYYY/MM/DD
ostream& Date::write(ostream& ostr)const {
ostr << year_ << "/";
if (mon_ >= 0 && mon_ <= 9)
ostr << "0" << mon_ << "/";
else
ostr << mon_ << "/";
if (day_ >= 0 && day_ <= 9)
ostr << "0" << day_;
else
ostr << day_;
return ostr;
}
// Helper operator<< overloaded
ostream& operator<<(ostream& ostr, const Date& d) {
d.write(ostr);
return ostr;
}
// Helper operator>> overloaded
istream& operator>>(istream& istr, Date& d) {
d.read(istr);
return istr;
}
bool Date::operator==(const Date& D)const {
bool rv;
if (this->value() == D.value())
rv = true;
else
rv = false;
return rv;
}
bool Date::operator!=(const Date& D)const {
bool rv;
if (this->value() != D.value())
rv = true;
else
rv = false;
return rv;
}
bool Date::operator<(const Date& D)const {
bool rv;
if (this->value() < D.value())
rv = true;
else
rv = false;
return rv;
}
bool Date::operator>(const Date& D)const {
bool rv;
if (this->value() > D.value())
rv = true;
else
rv = false;
return rv;
}
bool Date::operator<=(const Date& D)const {
bool rv;
if (this->value() <= D.value())
rv = true;
else
rv = false;
return rv;
}
bool Date::operator>=(const Date& D)const {
bool rv;
if (this->value() >= D.value())
rv = true;
else
rv = false;
return rv;
}
// Return the readErrorCode_ member-variable
int Date::errCode()const {
return readErrorCode_;
}
// Returns true if readErrorCode_ is not equal to zero
bool Date::bad()const {
bool rv;
if (errCode() != 0)
rv = true;
else
rv = false;
return rv;
}
}