/*
 * The UDS Collection
 * Copyright (C) 2000, 2001  Julian v. Bock
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *	(See file COPYING for more information)
 */

#ifndef _uds_time_hh
#define _uds_time_hh


// includes

#include <sys/time.h>
#include <time.h>


namespace uds
{

/** timeval wrapper
	\author Julian v. Bock */
class TimeVal
{
public:
	/** constructor */
	TimeVal() {}
	
	/** \param s seconds
		\param u microseconds */
	TimeVal( int s, int u = 0 ) : sec( s ), usec( u ) {}
	
	/** \param t time in seconds */
	TimeVal( double t );
	
	/** destructor */
	~TimeVal() {}
	
	/** converts the time to a double
		\return time */
	double
	FTime() const { return 1e-6 * usec + sec; }
	
	/** \return timeval reference */
	const timeval&
	to_timeval() const { return *reinterpret_cast< const timeval* >( this ); }
	
	/** \return timeval reference */
	timeval&
	to_timeval() { return *reinterpret_cast< timeval* >( this ); }
	
	/** \return time as 64 bit signed integer */
	long long
	to_int64() const;
	
	/** sets the time to the current time */
	void
	Now() { gettimeofday( reinterpret_cast< timeval* >( this ), 0 ); }
	
	TimeVal&
	operator += ( const TimeVal& rhs );
	
	TimeVal&
	operator -= ( const TimeVal& rhs );
	
	/// seconds
	int sec;
	/// microseconds
	int usec;
};

inline TimeVal
operator + ( const TimeVal& lhs, const TimeVal& rhs )
{
	return TimeVal( lhs ) += rhs;
}

inline TimeVal
operator - ( const TimeVal& lhs, const TimeVal& rhs )
{
	return TimeVal( lhs ) -= rhs;
}


/** \return current time as TimeVal */
inline TimeVal
Time()
{
	TimeVal tv;
	tv.Now();
	return tv;
}

/** \return current time in microseconds as signed long long */
inline long long
Time_int64()
{
	return Time().to_int64();
}


/** timespec wrapper
	\author Julian v. Bock */
class TimeSpec
{
public:
	/** constructor */
	TimeSpec() {}
	
	/** \param s seconds
		\param n nanoseconds */
	TimeSpec( int s, int n = 0 ) : sec( s ), nsec( n ) {}
	
	/** \param t time in seconds */
	TimeSpec( double t );
	
	/** destructor */
	~TimeSpec() {}
	
	/** converts the time to a double
		\return time */
	double
	FTime() const { return 1e-9 * nsec + sec; }
	
	/** \return timespec reference */
	const timespec&
	to_timespec() const { return *reinterpret_cast< const timespec* >( this ); }
	
	/** \return timespec reference */
	timespec&
	to_timespec() { return *reinterpret_cast< timespec* >( this ); }
	
	/** sets the time to the current time */
	void
	Now();
	
	TimeSpec&
	operator += ( const TimeSpec& rhs );
	
	TimeSpec&
	operator -= ( const TimeSpec& rhs );
	
	/// seconds
	int sec;
	/// nanoseconds
	int nsec;
};

inline TimeSpec
operator + ( const TimeSpec& lhs, const TimeSpec& rhs )
{
	return TimeSpec( lhs ) += rhs;
}

inline TimeSpec
operator - ( const TimeSpec& lhs, const TimeSpec& rhs )
{
	return TimeSpec( lhs ) -= rhs;
}

}

#endif // _uds_time_hh
