/*------------------------------------------------------------------------------
 src/lib/dj/djTime_.h
 
 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

namespace dj {

inline Timer::Timer()
{
  _t0.tv_sec = _t0.tv_usec = 0;
  _t1.tv_sec = _t1.tv_usec = 0;
  start();
}

inline double Timer::seconds() const
{
  const timeval t = elapsed();
  return t.tv_sec + t.tv_usec / 1000000.0;
}

inline double Timer::fps() const
{
  const double seconds = this->seconds();
  return (seconds > DBL_EPSILON) ? (1.0 / seconds) : 0.0;
}

inline void _timevalSub(const timeval & a, const timeval & b, timeval * out)
{
  out->tv_sec = a.tv_sec - b.tv_sec;
  out->tv_usec = a.tv_usec - b.tv_usec;
  if (out->tv_usec < 0) {
    --out->tv_sec;
    out->tv_usec += 1000000;
  }
}

inline  timeval Timer::elapsed() const
{
  timeval t;
  _timevalSub(_t1, _t0, &t);
  return t;
}

}
