#include <iostream>
#include <cstring>
#include <cstdlib>
#include "date.h"

using namespace std;

date::fmt date::next_fmt = date::ansi;

ostream& operator<<( ostream& os, date::fmt f)
{
    date::next_fmt = f;
    return os;
}
void date::print( std::ostream& os) const
{
    switch ( next_fmt )
    {
    default:
    case date::ansi:
        os << "[ " << get_year() << ", " << get_month()
        << ", " << get_day()  << " ]"; break;
    case date::us:
        os << "[ " << get_month() << ", " << get_day()
        << ", " << get_year()  << " ]"; break;
    case date::de:
        os << "[ " << get_day() << ", " << get_month()
        << ", " << get_year()  << " ]"; break;
    case date::hu:
        os << "[ " << get_year() << ", " << get_month()
        << ", " << get_day()  << " ]"; break;
    }
}
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;
}
istream& operator>>( istream& is, date& d)
{
    d.read( is);
    return is;
}
ostream& operator<<( ostream& os, const date &d)
{
    d.print( os);
    return os;
}