// date.cpp // ---------------------------------------------------------------------- // implements class Date // Do not modify this file. // // CSE 143 // Homework 2, 3 // http://www.cs.washington.edu/education/courses/143/00su/homework/ // 25 Jun 2000; Man-Hing Wong, Ken Yasuhara #include #include #include "date.h" const int NUM_MONTHS = 12; // standard (i.e. not leap year) number of days in each month const int monthLengths[NUM_MONTHS] = { 31, // Jan 28, // Feb 31, // Mar 30, // Apr 31, // May 30, // Jun 31, // Jul 31, // Aug 30, // Sep 31, // Oct 30, // Nov 31, // Dec }; ////////////////////////////////////////////////////////////////////// // constructors Date::Date() { month = 0; day = 0; } Date::Date(int newMonth, int newDay, int newYear) { month = newMonth; day = newDay; year = newYear; } bool Date::isValid() { if (month < 1 || month > NUM_MONTHS) { return false; } // month is valid, check day if (day < 1 || day > daysInMonth(month, year)) { return false; } // day is valid, check year if (year < 1) { return false; } // Date is valid return true; } int Date::getMonth() { return month; } int Date::getDay() { return day; } int Date::getYear() { return year; } int Date::compare(Date otherDay) { if (isValid()) { if (otherDay.isValid()) { // First, check year. if (year < otherDay.year) return 1; if (year > otherDay.year) return -1; // Year is equal, check month. if (month < otherDay.month) return 1; if (month > otherDay.month) return -1; // Year and month are the equal, check day. if (day < otherDay.day) return 1; if (day > otherDay.day) return -1; // This date and otherDay are equal return 0; } return -1; // an invalid otherDay means otherDay is earlier } if (otherDay.isValid()) return 1; // invalid date is earlier than otherDay return 0; // both invalid means they are equal } void Date::addDays(int numDays) { // just ignore if this is not a valid date if (!isValid()) { return; } assert (numDays >= 0); day += numDays; while(day > daysInMonth(month, year)) { // subtract off the number of days in this month and // increment the month to the next one day -= daysInMonth(month, year); month++; // check to for roll-over into the next year if (month > NUM_MONTHS) { assert(month == 13); month = 1; year++; } } assert(0 < day && day <= daysInMonth(month, year)); } int Date::dayDifference(Date d) { int diff = 0; // counter of num of days difference Date thisCopy(month, day, year); // get the current date if (!isValid() || !d.isValid()) { return diff; } Date earlier, later; if (d.compare(thisCopy) >= 0) { earlier = d; later = thisCopy; } else { earlier = thisCopy; later = d; } // loop while earlier != later while (earlier.month != later.month || earlier.day != later.day || earlier.year != later.year) { earlier.addDays(1); // increment thisCopy by 1 day diff++; // increment counter } return diff; } // returns the number of days in the given month of the given year, // taking leap years into account int Date::daysInMonth(int m, int y) { assert(m >= 1 && m <= NUM_MONTHS); int days = monthLengths[m-1]; // get number of days in month m // check if it is a leap year if (year % 4 == 0 && (!(year % 100 == 0) || (year % 400 == 0))) { // It is a leap year, increment days if month is Feb if (m == 2) { return days + 1; } } return days; } void Date::printDate() { cout << month << "/" << day << "/" << year; }