/*********************************************************************
 * This file is part of the cpplibs suite.
 *
 * Copyright (C) 2001 Topi Mäenpää
 * All rights reserved.
 *
 * This program is free software. You can redistribute and/or modify
 * it under the terms of the free software licence found in the
 * accompanying file "COPYING". The licence terms must always be
 * redistributed with this source file. The above copyright notice
 * must be reproduced in all modified and unmodified copies of this
 * source file.
 *
 * $Revision: 1.2 $
 *********************************************************************/

#include <Timer.h>

namespace util
{
	double Timer::getTime()
	{
		struct timespec t;
		clock_gettime(CLOCK_REALTIME,&t);
		return (t.tv_sec - _tmeStart.tv_sec + double(t.tv_nsec)/1000000000.0 - double(_tmeStart.tv_nsec)/1000000000.0);
	}

	void Timer::getTime(int& secs, long& nanosecs)
	{
		struct timespec t;
		clock_gettime(CLOCK_REALTIME,&t);

		if (t.tv_nsec >= _tmeStart.tv_nsec)
			{
				secs = t.tv_sec - _tmeStart.tv_sec;
				nanosecs = t.tv_nsec - _tmeStart.tv_nsec;
			}
		else
			{
				secs = t.tv_sec - _tmeStart.tv_sec - 1;
				nanosecs = 1000000000L + t.tv_nsec - _tmeStart.tv_nsec;
			}
	}
}
