This repository has been archived by the owner on Nov 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.cpp
372 lines (320 loc) · 7.83 KB
/
date.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "date.hpp"
#include <stdexcept>
#include "gregorian.hpp"
namespace lab2 {
// --------------------------- PROTECTED -------------------------------------
void Date::convert(Type t) {
// Ref: https://en.wikipedia.org/wiki/Julian_day#Calculation
//////////////////////////////////////////////////////
// Convert the current date to Julian Day (JD)
//////////////////////////////////////////////////////
int JD = julian_day(t);
//////////////////////////////////////////////////////
// Convert JD to (year, month, day) in the calendar
// format specified by convert_to_julian
//////////////////////////////////////////////////////
int f;
int y = 4716;
int j = 1401;
int m = 2;
int n = 12;
int r = 4;
int p = 1461;
int v = 3;
int u = 5;
int s = 153;
int w = 2;
int B = 274277;
int C = -38;
if (this -> type() == J) {
// For the Julian calendar, calculate:
f = JD + j;
}
else if (this -> type() == G) {
// For the Gregorian calendar, calculate:
f = JD + j + (((4*JD+B)/146097)*3)/4+C;
}
else f = -1; // Nor Gregorian or Julian
int e = r*f+v;
int g = (e % p)/r;
int h = u*g+w;
mDay = (h % s)/u+1;
mMonth = ((h/s+m)%n)+1;
mYear = (e/p)-y+(n+m-mMonth)/n;
}
int Date::julian_day(Type t) const {
int a = (14 - mMonth) / 12;
int y = mYear + 4800 - a;
int mm = mMonth + 12*a - 3;
int JD;
if (t == G) {
// For a date in the gregorian calendar
JD = mDay + ((153*mm + 2) / 5) + (365*y) + (y/4) - (y/100) + (y/400) - 32045;
// std::cout << "if - sats " << JD << std::endl;
}
else if (t == J) {
// For a date in the Julian calendar
JD = mDay + ((153*mm + 2) / 5) + (365*y) + (y/4) - 32083;
// std::cout << "else - sats " << JD << std::endl;
}
else return -1; // Nor Gregorian or Julian
return JD;
}
int Date::julian_day() const {
int a = (14 - mMonth) / 12;
int y = mYear + 4800 - a;
int mm = mMonth + 12*a - 3;
int JD;
if (this -> type() == G) {
// For a date in the gregorian calendar
JD = mDay + ((153*mm + 2) / 5) + (365*y) + (y/4) - (y/100) + (y/400) - 32045;
// std::cout << "if - sats " << JD << std::endl;
}
else if (this -> type() == J) {
// For a date in the Julian calendar
JD = mDay + ((153*mm + 2) / 5) + (365*y) + (y/4) - 32083;
// std::cout << "else - sats " << JD << std::endl;
}
else return -1; // Nor Gregorian or Julian
return JD;
}
bool Date::is_leap_year() const {
if (this -> type() == G) {
return mYear % 4 == 0 && (mYear % 100 != 0 || mYear % 400 == 0);
}
return mYear % 4 == 0;
}
Date& Date::add_days(int days_to_add) {
if (days_to_add < 0) {
for (int i = 0; i > days_to_add; --i) {
remove_day();
}
}
else {
for (int i = 0; i < days_to_add; ++i) {
add_day();
}
}
return *this;
}
Date& Date::add_day() {
++mDay;
if (mDay > int(days_this_month()) ) {
mDay = 1;
++mMonth;
if (mMonth > 12) {
mMonth = 1;
++mYear;
}
}
return *this;
}
Date& Date::remove_days(int days_to_remove) {
if (days_to_remove < 0) {
for (int i = 0; i > days_to_remove; --i) {
add_day();
}
}
else {
for (int i = 0; i < days_to_remove ; ++i) {
remove_day();
}
}
return *this;
}
Date& Date::remove_day() {
--mDay;
if (mDay < 1) {
mMonth--;
if (mMonth == 1 || mMonth == 3 || mMonth == 5 || mMonth == 7 || mMonth == 8 || mMonth == 10 || mMonth == 12) {
mDay = 31;
}
else if (mMonth == 4 || mMonth == 6 || mMonth == 9 || mMonth == 11) {
mDay = 30;
}
else if (mMonth == 2) {
mDay = is_leap_year() ? 29 : 28;
}
if (mMonth < 1) {
mMonth = 12;
mDay = 31;
--mYear;
}
}
return *this;
}
// ---------------------- Constructors / destructor ----------------------------
Date::Date(int year, int month, int day)
: mYear(year),
mMonth(month),
mDay(day) {
// Do some error checking here
}
Date::Date (const Date& d) {
mYear = d.year();
mMonth = d.month();
mDay = d.day();
}
int Date::year() const { return mYear; }
unsigned int Date::month() const { return mMonth; }
unsigned int Date::day() const { return mDay; }
unsigned int Date::week_day() const {
// https://en.wikipedia.org/wiki/Julian_day
return (julian_day() % 7) + 1;
}
unsigned int Date::days_per_week() const {
return 7;
}
unsigned int Date::days_this_month() const {
if (mMonth == 1 || mMonth == 3 || mMonth == 5 || mMonth == 7 || mMonth == 8 || mMonth == 10 || mMonth == 12) {
return 31;
}
if (mMonth == 4 || mMonth == 6 || mMonth == 9 || mMonth == 11) {
return 30;
}
if (mMonth == 2) {
if (is_leap_year()) {
return 29;
}
return 28;
}
return -1;
}
std::string Date::week_day_name() const {
int w = week_day();
if (w == 1) return "Monday";
if (w == 2) return "Tuesday";
if (w == 3) return "Wednesday";
if (w == 4) return "Thursday";
if (w == 5) return "Friday";
if (w == 6) return "Saturday";
return "Sunday";
}
std::string Date::month_name() const {
if (mMonth == 1) {
return "January";
}
if (mMonth == 2) {
return "February";
}
if (mMonth == 3) {
return "March";
}
if (mMonth == 4) {
return "April";
}
if (mMonth == 5) {
return "May";
}
if (mMonth == 6) {
return "June";
}
if (mMonth == 7) {
return "July";
}
if (mMonth == 8) {
return "August";
}
if (mMonth == 9) {
return "September";
}
if (mMonth == 10) {
return "October";
}
if (mMonth == 11) {
return "November";
}
if (mMonth == 12) {
return "December";
}
return "Unknown";
}
Date& Date::add_year(int n) {
mYear += n;
if (mMonth == 2 && mDay == 29 && !is_leap_year()) {
mDay--;
}
return *this;
}
Date& Date::add_month(int n) {
int years = n / 12;
int months = n - (years * 12);
add_year(years);
mMonth += months;
if (mMonth < 1) {
mMonth = 12 + mMonth;
--mYear;
}
if (mMonth > 12) {
++mYear;
mMonth -= 12;
}
if (mMonth == 4 || mMonth == 6 || mMonth == 9 || mMonth == 11) {
if (mDay > 30) {
mDay = 30;
}
}
if (mMonth == 2) {
if (is_leap_year()) {
if (mDay > 29) {
mDay = 29;
}
} else {
if (mDay > 28) {
mDay = 28;
}
}
}
return *this;
}
int Date::mod_julian_day() const {
return julian_day() - 2400000.5;
}
std::ostream& operator <<(std::ostream& os, const Date& date) {
os << date.year() << "-" << date.month() << "-" << date.day();
return os;
}
bool operator==(const Date& a, const Date& b) {
return a.mod_julian_day() == b.mod_julian_day();
}
bool operator!=(const Date &a, const Date& b) {
return !(a == b);
}
int operator -(const Date& l, const Date& r) {
return l.julian_day() - r.julian_day();
}
Date Date::operator++(int) {
Date d = *this;
add_day();
return d;
}
Date& Date::operator++() {
return add_day();
}
Date Date::operator--(int) {
Date d = *this;
return remove_day();
return d;
}
Date& Date::operator--() {
return remove_day();
}
Date& Date::operator+=(int days_to_add) {
return add_days(days_to_add);
}
Date& Date::operator-=(int days_to_remove) {
return remove_days(days_to_remove);
}
bool Date::operator>=(const Date& d) const {
return julian_day()>=d.julian_day();
}
bool Date::operator>(const Date& d) const {
return julian_day()>d.julian_day();
}
bool Date::operator<(const Date& d) const {
return julian_day() < d.julian_day();
}
bool Date::operator<=(const Date& d) const {
return julian_day()<=d.julian_day();
}
}