/********************************************************************************************
date.cpp
Jim Millard
for CO211/Fall 1999

Implementation of the date class

********************************************************************************************/
#include <iostream.h>
#include "date.h"

bool leapyear(unsigned year); //returns true if argument is a leap year

date::date() {
    day = 1;
    month = 1;
    year = 1;
}


date::date(int d, int m, int y) {
    if (y < 1)
        year = 1;
    else
        year = y;

    if ((m < 1) || (m > 12))
        month = 1;
    else
        month = m;

    if (d < 1)
        day = 1;
    else
        switch (month) {
            //february
            case 2:
                if ( (d <= 28) || ((d == 29) && leapyear(year)) )
                    day = d;
                break;
            //april, june, september, november
            case 4: case 6: case 9: case 11:
                if (d <= 30)
                    day = d;
                break;
            //all the other months
            default:
                if (d <= 31)
                    day = d;
    } //switch
}

void date::print() {
    cout << month << '-' << day << '-' << year;
}

void date::tomorrow() {
    switch (month) {
        //february
        case 2:
            if ((day < 28) || ((day == 28) && leapyear(year)) )
                day++;
            else {
                day = 1;
                month++;
            }
            break;
        //april, june, september, november
        case 4: case 6: case 9: case 11:
            if (day < 30)
                day++;
            else {
                day = 1;
                month++;
            }
            break;
        //december
        case 12:
            if (day < 31)
                day++;
            else {
                day = 1;
                month = 1;
                year++;
            }
            break;
        //all the other months
        default:
            if (day < 31)
                day++;
            else {
                day = 1;
                month++;
            }
    }
}

bool leapyear(unsigned year)
    {
    if (1584 > year) //too early to be a leap year
        return false;
    else if (year%4) //not divisible by 4, so it's not a leap year.
        return false;
    else if ( (!(year%100)) && (year%400)) //divisible by 100, but not by 400
        return false;
    else
        return true;
    }