
#ifdef GNU
#define std
#include <iostream.h>
#else
#include <iostream>
#endif

#include "Date.h"

// The number of days in each month --- ignoring leap years!
//
int days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

// Constant array indicating the number of days in the year prior to
// the start of each month.  Used for converting from month/day to the
// Julian day.
//
int month_start[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273,
		      304, 334 };



Date::Date( int month, int day ) : month_(month), day_(day)
{ }

Date::Date( const Date& prev_date )
  : month_(prev_date.month_), day_(prev_date.day_)
{ }

Date::Date( int julian_day )
{
  day_ = julian_day;
  month_ = 1;
  while ( day_ > days_in_month[month_-1] ) {
    day_ -= days_in_month[month_-1];
    ++ month_;
  }
}


// Destructor does nothing.
//
Date::~Date() {}


// Convert the date to the Julian Day.
//
int
Date::JulianDay() const
{
  return month_start[month_-1] + day_;
}


// Addition operator.  Don't try to handle a negative number of days.
//
Date
operator+ ( const Date& date, int ndays )
{
  if ( ndays < 0 ) {
    std::cerr << "Error in Date::operator+:  negative number of days.\n";
    return Date(1,1);
  }
  int new_month = date.month_;
  int new_day = date.day_ + ndays;

  while ( new_day > days_in_month[new_month-1] ) {
    new_day -= days_in_month[new_month-1];
    new_month ++ ;
    if ( new_month > 12 ) new_month=1;
  }
  return Date( new_month, new_day );
}
  

// Addition operator. 
Date
operator+ ( int ndays, const Date& date )
{
  return date+ndays;
}

 
bool
operator< ( const Date& left, const Date& right )
{
  return left.month_ < right.month_ ||
    (left.month_ == right.month_ && left.day_ < right.day_);
}

bool
operator> ( const Date& left, const Date& right )
{
  return left.month_ > right.month_ ||
    (left.month_ == right.month_ && left.day_ > right.day_);
}

std::ostream&
operator<< ( std::ostream& ostr, const Date& date )
{
  ostr << date.month_ << "/" << date.day_;
  return ostr;
}
