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
|
#include <iostream>
#include "Chrono.h"
using namespace std;
//definitions
namespace Chrono {
int Date:: getDay() const {return day;}
Month Date::getMonth() const {return month;}
int Date::getYear() const {return year;}
bool leapYear(int y) {
if (y % 4 == 0) return true;
return false;
}
bool isDate(Month m, int d, int y) {
if(d <= 0) return false;
if (m < Month::jan || m > Month::dec) return false;
int daysInMonth = 0;
switch (m) {
case Month::feb:
if (leapYear(y)) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
break;
case Month::apr: case Month::jun: case Month::sep: case Month::nov:
daysInMonth = 30;
break;
case Month::jan: case Month::mar: case Month::may: case Month::jul: case Month::aug: case Month::oct: case Month::dec:
daysInMonth = 31;
}
if (d > daysInMonth) return false;
return true;
}
Month& operator ++(Month& m) { // to add one to month and make sure it comes back to jan after dec
m = (m == Month::dec) ? Month::jan: Month(int(m) + 1);
return m;
}
bool operator ==(Date& a, Date& b) {
return a.getDay() == b.getDay()
&& a.getMonth() == b.getMonth()
&& a.getYear() == b.getYear();
}
bool operator != (Date& a, Date& b) {
return a.getDay() != b.getDay()
&& a.getMonth() != b.getMonth()
&& a.getYear() != b.getYear();
}
ostream& operator <<(ostream& os, Date& d) {
os <<int (d.getMonth()) <<"/"
<<d.getDay() <<"/"
<<d.getYear();
return os;
}
istream& operator >>(istream& is, Date& dd) {
int d, m, y;
char c1, c2;
is >> m >> c1 >> d >> c2 >> y;
if (c1 != '/' || c2 != '/') {
is.clear(ios::failbit); // If wrong format, set the fail bit
return is;
}
dd = Date (Month (m), d, y);
return is;
}
Date::Date(): day(1), year(2001), month(Month::jan) {} // default constructor
Date::Date(Month m, int d, int y) {
if (isDate(m, d, y)) {
month = m;
day = d;
year = y;
}else {
cerr<<"This is not a valid date";
}
}
void Date::addDay(int d) { //add day to date but check if it woulc create a valid date first
if (isDate(month, d, year)) d++;
}
void Date::addMonth(Month m) {// go to next month with overloaded ++ but check month first
if (m < Month::jan || m > Month::dec) {
cerr<<"This is not a valid month"<<endl;
} else {
++m;
}
}
void Date::addYear(int n) {
// check if current year is leap year before adding 1. If so, date is changed to mar 1, year+1
if (month == Month::feb && day == 29 && !leapYear(year + n)) {
month = Month::mar;
day = 1;
}
year += n;
}
}
|