// ========================================================================================
// IMPORTANT: You should not edit this file
// ========================================================================================

#include <iostream>
#include <iomanip>
#include <cassert>

#include "timestamp.h"

// Add the specified number of seconds to the TimeStamp.
// Overflow seconds to minutes, hours, and days as necessary 
void TimeStamp::addTime(int s) {
  seconds += s;
  while (seconds >= 60) { seconds -= 60; minutes++; }
  while (minutes >= 60) { minutes -= 60; hours++; }
  while (hours >= 24) { hours -= 24; days++; }
}

// Convert each TimeStamp to seconds, then subtract
int elapsed(const TimeStamp &a, const TimeStamp &b) {
  int left  = a.getDays()*24*3600 + a.getHours()*3600 + a.getMinutes()*60 + a.getSeconds();
  int right = b.getDays()*24*3600 + b.getHours()*3600 + b.getMinutes()*60 + b.getSeconds();
  assert (left <= right);
  return right - left;
}

// For sorting TimeStamp objects
bool operator<(const TimeStamp &a, const TimeStamp &b) {
  return (a.getDays() < b.getDays() ||
          (a.getDays() == b.getDays() && a.getHours() < b.getHours()) ||
          (a.getDays() == b.getDays() && a.getHours() == b.getHours()
           && a.getMinutes() < b.getMinutes()) ||
          (a.getDays() == b.getDays() && a.getHours() == b.getHours()
           && a.getMinutes() == b.getMinutes() && a.getSeconds() < b.getSeconds()));
}

// For printing TimeStamp objects
std::ostream& operator<<(std::ostream &ostr, const TimeStamp &a) {
  ostr << std::right << std::setfill('0')
       << std::setw(2) << a.getHours() << ":"
       << std::setw(2) << a.getMinutes() << ":"
       << std::setw(2) << a.getSeconds() << std::setfill(' ');
  return ostr;
}
