// date.cc   A simple date class for use with the Baseball database
// P. Conrad, for CISC220, 06J


#include "date.h"

#include <cstring> // for strtok
#include <cstdlib> // for atoi

// Use of const here helps avoid the "inversion error"
// where we accidentally write y = year; m = month; d = day;
// Beleive it or not, that is a common error among beginner and 
// experienced programmers alike.

Date_C::Date_C(const int y, const int m, const int d)
{
  year = y;
  month = m;
  day = d;
}


Date_C::Date_C(const char * const date)
{
  // allocate a temporary copy of date on the heap

  char *tempDate = new char[strlen(date)+1];
  strcpy(tempDate, date);
   
  char *monthPtr = strtok(tempDate,"/");
  char *dayPtr = strtok(NULL,"/");
  char *yearPtr = strtok(NULL,"/");

  year = atoi(yearPtr);
  month = atoi(monthPtr);
  day = atoi(dayPtr);

  delete [] tempDate ; // recycle the temporary copy
  
}


void Date_C::print(std::ostream & out) const
{
  out << month << "/" << day << "/" << year;
}


std::ostream & operator << (std::ostream & left, Date_C const & right)
{
  right.print(left);
  return left;
}

