/*
 * @topic T00292 <span style="background-color: yellow;">New: </span>Serial Date class (Spring 2014)
 * @brief A mockup of serial date functions -- replace them by real functions from Assignment a2
*/

#include <sstream>
#include <string>
#include "serial_date_utility.h"

using namespace std;

int serial_julian_date( int Month, int Day, int Year )
{
    // TODO -- you have to replace this by the real implementation
    // Let's save our date in YYYYMMDD format.
    return Year * 10000 + Month * 100 + Day;
}

int serial_2_month( int nDate )
{
    // TODO -- you have to replace this by the real implementation
    stringstream buffer;
    buffer << nDate;
    string str = buffer.str();
    // YYYYMMDD
    // 01234567
    str = str.substr( 4/*position*/, 2/*length*/ ); // "MM"
    return string_2_int( str );
}

int serial_2_day( int nDate )
{
    // TODO -- you have to replace this by the real implementation
    stringstream buffer;
    buffer << nDate;
    string str = buffer.str();
    // YYYYMMDD
    // 01234567
    str = str.substr( 6/*position*/, 2/*length*/ ); // "DD"
    return string_2_int( str );
}

int serial_2_year( int nDate )
{
    // TODO -- you have to replace this by the real implementation
    stringstream buffer;
    buffer << nDate;
    string str = buffer.str();
    // YYYYMMDD
    // 01234567
    str = str.substr( 0/*position*/, 4/*length*/ ); // "YYYY"
    return string_2_int( str );
}

int string_2_int( string str )
{
    stringstream buffer( str ); // "12345"
    int value;
    buffer >> value;
    return value;
}