// computeSplitTime.cc
// P. Conrad 03/06/06
// separately compiled computeSplitTime() function

#include <cstring> // strtok
#include <cstdlib> // atoi and atof
using namespace std;

#include "computeSplitTime.h"  // use quotes because it is MY .h fiel


double computeSplitTime(const char *  const splitTimePtr)
{
  

  char splitTimeCopy[1024]; // over-engineered in a big way

  // Later, come back and talk about strcpy vs. strncpy vs. strpcpy
  //  buffer overruns, hacking, security, etc...

  strcpy(splitTimeCopy, splitTimePtr);
  
  // now proceed with the rest of the function...

  double result;


  // use strtok to divide the string into the first part
  // which is in minutes, and the second part which is in seconds.

  // for example, 1:51.9 will be divided into 1\051.9

  char *firstPart;
  char *secondPart;
  
  firstPart = strtok(splitTimeCopy,":");
  secondPart = strtok(NULL,":");

  // compute the result by converting firstPart to a number
  // and multiplying by 60, and add to second part
  // use atof on the second part, becuase it is a double
  // (yeah, go figure... there is no atod... atof returns not
  //    a float, but rather, a double)

  result = atoi(firstPart) * 60 + atof(secondPart);

  return result;
}

int minutesPart(double splitTimeSeconds)
{
  int minutes = (int) (splitTimeSeconds/60);
  return minutes;
}

double secondsPart(double splitTimeSeconds)
{
  // return splitTimeSeconds - (minutesPart(splitTimeSeconds) * 60.0);
  return fmod (splitTimeSeconds, 60.0);
  
}


void printSplitTime(double splitTimeSeconds, ostream & out)
{
  // remember to print out the seconds part with a leading zero.

  out << minutesPart(splitTimeSeconds) 
      << ":" 
      <<  ( ( secondsPart(splitTimeSeconds) < 10   ) ? "0" : "" )
      << secondsPart(splitTimeSeconds);

}







