#include <stdio.h>
#include <stdlib.h>
#include <cassert>
#include "astropulse.h"

namespace Astropulse {

timer::timer() {
  ticks = 0;
  seconds = 0;
  start_clock = clock();
  start_seconds = time(NULL);
  state = off;
}
void timer::start() {
  last_clock = clock();
  last_time = time(NULL);
  assert(state == off);
  state = on;
}
void timer::stop() {
  clock_t ticks_this_time = clock() - last_clock;
  ticks += ticks_this_time;
  long seconds_this_time = time(NULL) - last_time;
  seconds += seconds_this_time;
  assert(state == on);
  state = off;
}
void timer::reset() {
  ticks = 0;
  seconds = 0;
  start_clock = clock();
  start_seconds = time(NULL);
  state = off;
}
clock_t timer::get_ticks() {
  return ticks;
}
clock_t timer::get_total_ticks() {
  return clock() - start_clock;
}
long timer::get_seconds() {
  return seconds;
}
long timer::get_total_seconds() {
  return time(NULL) - start_seconds;
}

void timer::print_total_time() {
  stop();
  long minutes = seconds / 60;
  int hours = minutes / 60;
  int days = hours / 24;
  printf("Time since start: %d days, %d hours, %ld minutes, %ld seconds\n",
         days, hours % 24, minutes % 60, seconds % 60);
  start();
}

}

