ToD.cpp
/**************************************************
 * Implementation of functions for ToD module!
 * Let's store time so that h in [0,12),
 * m in [0,60) and PM true means ... PM of course!
 **************************************************/
#include "ToD.h"

/**************************************************
 * Helper function for my implementation!
 **************************************************/
static string twodig(int i)
{
  string s;
  s = s + char('0' + i/10) + char('0' + i%10);
  return s;
}

/**************************************************
 * Set functions
 **************************************************/
bool set12(ToD &T, int lh, int bh, string ampm)
{
  if (0 < lh && lh <= 12 && 0 <= bh && bh < 60 && (ampm == "AM" || ampm == "PM"))
  {
    T.h = lh % 12;
    T.m = bh;
    T.PM = (ampm == "PM");
    return true;
  }
  else
    return false;
}

bool set24(ToD &T, int lh, int bh)
{
  if (0 <= lh && lh <= 24 && 0 <= bh && bh < 60)
  {
    T.h = lh % 12;
    T.m = bh;
    T.PM = (lh%24 >= 12);
    return true;
  }
  else
    return false;
}

/**************************************************
 * Output functions
 **************************************************/
void print12(ToD T, ostream&)
{
  int H = T.h == 0 ? 12 : T.h;
  cout << H << ':' << twodig(T.m);
  if (T.PM)
    cout << "PM";
  else
    cout << "AM";
}

void print24(ToD T, ostream&)
{
  int H = T.h == 0 ? 12 : T.h;
  if (T.PM) H += 12;
  cout << twodig(H) << ':' << twodig(T.m);
}

/**************************************************
 * Add time functions
 **************************************************/
void addmints(ToD &T, int m)
{
  if (m >= 0)
  {
    int M = 60*T.h + m;
    if (T.PM) M += 60*12;
    T.h = (M/60) % 12;
    T.m = M % 60;
    T.PM = (M/60) % 24 >= 12;
  }
}

void addhours(ToD &T, int h)
{
  addmints(T,h*60);
}