#include #include #include "date.h" using namespace std; int date::length() const { static const int a[] = { 0, //dummy 31, //january 29, //february 31, //march 30, //april 31, //may 30, //june 31, //july 31, //august 30, //september 31, //october 30, //november 31 //december }; return a[month]; } void date::install(int m, int d, int y) { year = y; if (m < january || m > december) { cerr << "bad month " << m << "/" << d << "/" << y << "\n"; exit(EXIT_FAILURE); } month = m; if (d < 1 || d > length()) { cerr << "bad day " << m << "/" << d << "/" << y << "\n"; exit(EXIT_FAILURE); } day = d; } date::date() //Initialize to the current date. { const time_t t = time(0); if (t == static_cast(-1)) { cerr << "time failed\n"; exit(EXIT_FAILURE); } const tm *const s = localtime(&t); install(s->tm_mon + 1, s->tm_mday, s->tm_year + 1900); } date& date::operator++() { if (++day > length()) { day = 1; if (++month > december) { month = january; ++year; } } return *this; } date& date::operator--() { if (--day < 1) { if (--month < january) { month = december; --year; } day = length(); } return *this; }