#include <iostream>
#include <cstring>
#include <cstdlib>
#include "date.h"
using namespace std;
date::date( const char *s)
{
char *p1 = strchr( s, '.');
char *p2 = strrchr( s, '.');
if ( p1 && p2 && p1 != p2 )
{
int y = atoi(s);
int m = atoi(p1);
int d = atoi(p2);
set( y, m, d);
}
}
bool operator<( date d1, date d2)
{
return d1.get_year() < d2.get_year() ||
d1.get_year() == d2.get_year() && d1.get_month() < d2.get_month() ||
d1.get_year() == d2.get_year() && d1.get_month() == d2.get_month()
&& d1.get_day() < d2.get_day();
}
date& date::next()
{
static int day_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
++day;
if ( day-1 == day_in_month[month-1])
{
day = 1;
++month;
}
if ( 13 == month )
{
month = 1;
++year;
}
return *this;
}
date& date::add( int n)
{
for (int i = 0; i < n; ++i)
++ *this;
return *this;
}
void date::read( std::istream& is)
{
// this is not perfect...
is >> year >> month >> day;
}
void date::print( std::ostream& os) const
{
// better to use getters...
os << "[ " << year << ", " << month
<< ", " << day << " ]";
}
istream& operator>>( istream& is, date& d)
{
d.read( is);
return is;
}
ostream& operator<<( ostream& os, const date &d)
{
d.print( os);
return os;
}