// ***********************************************************
//      Time24 class implementation
// ***********************************************************

#include "simpletime24.h"

// Explicit-value constructor
// Initialize time data
// Assumes h and m > 0
Time24::Time24(int h, int m) : myHour(h), myMinute(m)
{
   // put myHour and myMinute in correct range
   NormalizeTime();
}  // Explicit-value constructor

// Function: AddTime
// Add m minutes to the time
void Time24::AddTime(int m)
{
   // add m to myMinute. myMinute may exceed 59, so normalize
   myMinute += m;
   NormalizeTime();
}  // end AddTime

// Function: Duration
// Returns the difference between this time and t
// Assumes that t is before current time
Time24 Time24::Duration(const Time24& t) const
{
   // convert current time and time t to minutes
   int currTime = myHour * 60 + myMinute;
   int tTime = t.myHour * 60 + t.myMinute;

   // create an anonymous object as the return value
   // will be normalized automatically
   return Time24(0, tTime-currTime);
}  // end Duration

// Function: ReadTime
// Input time in format <hour>:<minute>
// Assumes that hour and minute are not negative
void Time24::ReadTime(istream& in)
{
   char colonSeparator;

   in >> myHour >> colonSeparator >> myMinute;
   // make sure myHour and myMinute are in range
   NormalizeTime();
}  // end ReadTime

// Function: WriteTime
// Output time in the format <hour>:<minute>
void Time24::WriteTime(ostream& out) const
{
   out << (myHour < 10 ? " " : "") << myHour << ':'
       << (myMinute < 10 ? "0" : "") << myMinute;

}  // end WriteTime

// Function: GetHour
// Returns the hour
int Time24::GetHour() const
{
   return myHour;
}  // end GetHour

// Function: GetMinute
// Returns the minute
int Time24::GetMinute() const
{
   return myMinute;
}  // end GetMinute

// Private Function: NormalizeTime
// Set myMinute and myHour within their proper ranges
// Assumes myMinute and myHour are not negative
void  Time24::NormalizeTime()
{
   int extraHours = myMinute / 60;

   // set myMinute in range 0 to 59
   myMinute %= 60;

   // update myHour. set in range 0 to 23
   myHour = (myHour + extraHours) % 24;
}  // end NormalizeTime
