// time.cc  P. Conrad for CISC220, 06J

#include "time.h"

#include <iostream>
#include <iomanip> // for setw() and setfill()


using std::cerr;
using std::endl;

Time_C::Time_C () 
{ 
#ifdef DEBUG_TIME
  cerr << "Entering (20) Foo_C(); " << endl;
#endif

  hour = minute = 0;

#ifdef DEBUG_TIME
  cerr << "Exiting (20)" << endl;
#endif

}


Time_C::Time_C (int theHour, int theMinute) 
{ 
#ifdef DEBUG_TIME
  cerr << "Entering (21) Foo_C(int theHour, int theMinute); " << endl;
#endif

  if (theHour > 23 || 
      theHour < 0 || 
      theMinute > 59 ||
      theMinute < 0 )
    {
      hour = minute = -1;
    }
  else
    {
      hour = theHour; minute = theMinute;
    }

#ifdef DEBUG_TIME
  cerr << "Exiting (21)" << endl;
#endif

}


Time_C::Time_C(const Time_C & orig)
{
#ifdef DEBUG_TIME
  cerr << "Entering (22) Time_C(const Time_C & orig); (copy constructor) " << endl;
#endif

  hour = orig.hour;
  minute = orig.minute;

#ifdef DEBUG_TIME
  cerr << "Exiting (22)  " << endl;
#endif

}
Time_C & Time_C::operator = (const Time_C & right)
{

#ifdef DEBUG_TIME
  cerr << "Entering (23) operator = (const Time_C & right)" << endl;
#endif

  if (&right == this)
    return (*this);


  hour = right.hour;
  minute = right.minute;

  
#ifdef DEBUG_TIME
  cerr << "Exiting (23) " << endl;
#endif

  return (*this);

}


Time_C::~Time_C()
{
#ifdef DEBUG_TIME
  cerr << "Entering (24) ~Time_C()" << endl;
#endif


#ifdef DEBUG_TIME
  cerr << "Exiting (24) ~Time_C()" << endl;
#endif

}

void Time_C::print(std::ostream &out) const
{
  if (badTime())
    out << "xx:xx";
  else
    {
      // Note: std::setfill(0) will not work; it expect a char argument
      // However, std::setfill(0) WILL compile, which can be misleading...
      // C and C++ treat characters and integers as interchangable...
      // But setfill(0) just fills with "null characters"

      out <<  std::setfill('0')  << std::setw(2) << hour
	  << ":" 
	  << std::setfill('0') << std::setw(2) << minute;
    }
}

std::ostream & operator << (std::ostream & right, 
			    const Time_C &left)
{
#ifdef DEBUG_TIME
  cerr << "Entering (25) << (ostream & left, const Time_C & right) " << endl;
#endif

  left.print(right);
  return right;

#ifdef DEBUG_TIME
  cerr << "Exiting (25) " << endl;
#endif

}

