-
Notifications
You must be signed in to change notification settings - Fork 0
/
Date.h
528 lines (433 loc) · 12.7 KB
/
Date.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// ------------------------
// projects/c++/date/Date.h
// Copyright (C) 2009
// Glenn P. Downing
// ------------------------
#ifndef Date_h
#define Date_h
// --------
// includes
// --------
#include <cassert> // assert
#include <iostream> // ostream
#include <stdexcept> // invalid_argument
#include <string>
// ----------
// namespaces
// ----------
static const std::string months[] =
{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
//number of days on each month
static const int ndiem[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//total of days after each month
static const int tdaem[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
//total of days after each month in leap years
static const int tdaemil[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
namespace Date{
// ----
// Date
// ----
enum Month{
dummy, //0
Jan, //1
Feb, //2
Mar, //3
Apr, //4
May, //5
Jun, //6
Jul, //7
Aug, //8
Sep, //9
Oct, //10
Nov, //11
Dec //12
};
template <typename T>
class Date {
private:
// ----
// data
// ----
// <your data>
T day;
T month;
Month mon;
T year;
T epoch; // starts from 1 1 1600
private:
// -----
// valid
// -----
/**
** this method will be used to check whether
* the input for the day, month and the year is valid
*/
/**
* O(1) in space
* O(1) in time
*/
bool valid () const {
bool yearOk = (year >= 1600);
bool monthOk = (month>0 && month <= 12);
bool dayOk = false;
switch(mon){
case Feb :
if(leapYear())
dayOk = (day>0 && day<=29);
else dayOk = (day>0 && day<=28);
break;
case Apr :
case Jun :
case Sep :
case Nov :
dayOk = (day>0 && day<=30);
break;
default:
dayOk = (day>0 && day<=31);
}
return (dayOk&&monthOk&&yearOk);}
// -----------
// leapyear
// -----------
/**
* O(1) in space
* O(1) in time3
* Checks to see if the year is a leap year
* @param year >= 1600
* @throws
*/
bool leapYear() const{
assert(year>=1600);
bool leap = false;
if((year%4)==0){
leap = true;
if( ((year%100)==0) && !((year%400)==0))
leap = false;
}
return leap;
}
// -----------
// constructor
// -----------
/**
* O(1) in space
* O(1) in time
* @param days >= 0
* Date(0) -> 1 Jan 1600
* you can loop through an array of month days
*/
Date (const T& days) {
assert(days >= 0);
// 146 097 = num of days for every 400 yrs
year = 1600;
T tempDays = days;
if(tempDays>=146097){
year += 400 * (tempDays/146097);
tempDays %= 146097;
}
if(tempDays>=36525){
year += tempDays/36525 * 100;
tempDays %= 36525;
}
if(tempDays>=1461){
year += (std::min(24,(tempDays/1461))) * 4;
tempDays %= 1461;
}
if(tempDays<=366) {}
else{
tempDays -= 366; // reducing the leap year
year += (tempDays/365) + 1; // adding the leap year + remaining years
}
day = 1;
month = -1;
if(leapYear()){
if(tempDays>365){
tempDays = 30;
month = 11;
--year;
}
else if(tempDays==365)
{
tempDays = 30;
month = 11;
}
else{
for(int i = 0; i < 12; i++)
{
if(tempDays >= (tdaemil[i]) && tempDays < (tdaemil[i+1]))
{
tempDays -= tdaemil[i];
month = i;
}
}
}
day = ++tempDays;
}
else
{
if(tempDays==365)
{
tempDays = 31;
month = 11;
--year;
}
else{
while(tempDays>365){
tempDays-= 365; }
if(tempDays==365)
{
tempDays = 31;
month = 11;
--year;
}
else{
for(int i = 0; i < 12; i++)
{
if(tempDays >= (tdaem[i]) && tempDays < (tdaem[i+1]))
{
tempDays -= tdaem[i];
month = i;
}
}
}
}
day = tempDays;
}
if (!valid())
throw std::invalid_argument("Date::Date()");}
// -------
// to_days
// -------
/**
* O(1) in space
* O(1) in time
* @return the number of days since 1 Jan 1600, >= 0
* 1 Jan 1600 -> 0
*/
public:
Date()
{
day = 1;
month = 1;
year = 1600;
}
T to_days () const {
T days = 0;
T tempDays=0;
int years = year - 1600;
bool leap = false;
if(years>=400) //400 years
{
tempDays += 146097 * (years/400);
years %= 400;
}
if(years>=100) //100 years
{
tempDays += years/100 * 36524;
years %= 100;
}
if(years>=4) //4 years
{
tempDays += (std::min(24,(years/4))) * 1461;
years %= 4;
}
if(years == 0)
leap=true;
else if(years==1)
{
tempDays +=366;
}
else{ //more than 1 year left
tempDays +=366;
for(int i = 0; i < 3; i++)
{
if(years > 1)
{
--years;
tempDays +=365;
}
}
}
if(leap)
{
tempDays += tdaemil[month-1];
tempDays += day;
}
else
{
tempDays += tdaem[month-1];
tempDays += day;
}
--tempDays;
assert(days >= 0);
return tempDays;}
// -----------
// constructor
// -----------
/**
* O(1) in space
* O(1) in time
* @param day >= 1 && <= 31
* @param month >= 1 && <= 12
* @param year >= 1600
* @throws std::invalid_argument if the resulting date is invalid
*/
Date (const T& day, const T& month, const T& year) {
// <your code>
this->day = day;
this->month = month;
this->year = year;
mon = static_cast<Month>(month);
if (!valid())
throw std::invalid_argument("Date::Date()");}
// Default copy, destructor, and copy assignment.
// Date (const Date&);
// ~Date ();
// Date& operator = (const Date&);
// -----------
// operator +=
// -----------
/**
* adding number of days to a date
* @param days the number of days to add (may be negative!)
* @return the date resulting from adding days
* @throws std::invalid_argument if the resulting date precedes 1 Jan 1600
*/
Date& operator += (const T& days) {
T theDays = to_days();
const T total = theDays + day;
if(total < 0)
throw std:: invalid_argument("Date+= would be before 1 jan 1600");
try{
*this = Date(total);
return *this;
}
catch (std::invalid_argument& e){
throw std::invalid_argument("Date+= would be before 1 jan 1600");
}
}
// -----------
// operator -=
// -----------
/**
* Calculates dates minus days
* @param days the number of days to subtract (may be negative!)
* @return the date resulting from subtracting days
* @throws std::invalid_argument if the resulting date precedes 1 Jan 1600
*/
Date& operator -= (const T& days) {
// <your code>
T theDays = to_days();
const T total = theDays - day;
if(total < 0)
throw std:: invalid_argument("Date+= would be before 1 jan 1600");
try{
*this = Date(total);
return *this;
}
catch (std::invalid_argument& e){
throw std::invalid_argument("Date+= would be before 1 jan 1600");
}
}
// ---------
// leap_year
// ---------
/**
* O(1) in space
* O(1) in time
*Checks to ensure it's a leapyear
* @param year >= 1600
* @throws std::invalid_argument if the resulting date is invalid
*/
bool leap_year () const {
bool leap = false;
if((year%4)==0){
leap = true;
if( ((year%100)==0) && !((year%400)==0))
leap = false;
}
return leap;
}
//template <typename Z> friend std::ostream& operator ==(const Date<Z>& lhs, const Date<Z>& rhs);
template <typename Z> friend std::ostream& operator <<(std::ostream& lhs, const Date<Z>& rhs);
}; // end of class DATE!!!
// -----------
// operator ==
// -----------
/**
* Checks if two dates are equal to one another
* @param lhs a Date
* @param rhs another Date
* @return boolean if the dates are equal
*/
template <typename Z>
bool operator ==(const Date<Z>& lhs, const Date<Z>& rhs) {
bool check = lhs.to_days() == rhs.to_days();
//check =
return check;
}
// ----------
// operator <
// ----------
/**
* Checks if two dates are less than to one another
* @param lhs a Date to be checked if smaller
* @param rhs another Date
* @return boolean if the dates are equal
*/
template <typename Z>
bool operator < (const Date<Z>& lhs, const Date<Z>& rhs) {
bool check = lhs.to_days() < rhs.to_days();
return check;}
// ----------
// operator +
// ----------
/**
* Adds two dates together
* @param lhs a date object
* @param rhs another Date
* @return the sum of two dates
*/
template <typename Z>
Date<Z> operator + (Date<Z> lhs, const Z& rhs) {
return lhs += rhs;}
// ----------
// operator -
// ----------
/**
* Difference between two dates together
* @param lhs a date object
* @param rhs another Date
* @return the difference of two dates
*/
template <typename Z>
Date<Z> operator - (Date<Z> lhs, const Z& rhs) {
return lhs -= rhs;}
/**
* Difference between two dates together
* @param lhs a date object
* @param rhs another Date
* @return the difference of two dates
*/
template <typename Z>
Z operator - (const Date<Z>& lhs, const Date<Z>& rhs) {
// <your code>
return lhs.to_days() - rhs.to_days();}
// -----------
// operator <<
// -----------
/**
* O(1) in space
* O(1) in time
* @param lhs an ostream
* @param rhs a date
* @return the ostream
* output a string representation of the date (e.g. "3 Feb 2008")
* you can loop through an array of month names
*/
template <typename T>
std::ostream& operator << (std::ostream& lhs, const Date<T>& rhs) {
return lhs << rhs.day << " " << months[rhs.month -1] << " " << rhs.year;
}
} // Date
#endif // Date_h