// clock.h: interface for the Clock class.
//
//////////////////////////////////////////////////////////////////////

#ifndef CLOCK_H
#define CLOCK_H

#include <time.h>
#include "types.h"

#define PC104_TIMEBASE	1193180		// Clock frequency in Hz of PC-104 cpu board timer
#define SECS_PER_DAY	86400L		// Seconds per day
#define YEAR0			1970		// Start year for TimeOfDay
#define TCKS_PER_SEC	60			// Default tcks per second
									// Must be common multiple of on-board cpu board timer
									// Don't make this > 1000; will cause thrashing on vxworks

typedef Nat32	gmTime;				// Greenwich mean time. Seconds since 00:00:00 of 1/1/70

typedef struct				
{							// DateTime Struct
    Byte	dt_hr;			// hours since midnight - [0,23]	
    Byte	dt_min;			// minutes after the hour - [0,59]
    Byte	dt_sec;			// seconds after the minute - [0,59]
    Nat16	dt_yr;			// years since 1900
    Byte	dt_mo;			// months since January - [1,12] 
    Nat16	dt_yday;		// days since January 1 - [0,365] 
    Byte	dt_mday;		// day of the month - [1,31] 
	Byte	dt_wday;		// day of the week = [1,7]
} DateTime;				

typedef enum
{
	localTime,
	greenwichTime
}TimeFormat;

// forward declaration for Clock to use
class Time;

class Clock  
{
public:	
	// Returns the elapsed system clock in clock ticks. To convert to seconds
	// divide by getSystemClkTck. This is the elapsed ticks since the
	// start of program execution.  This is *NOT* an accurate method
	// determining elapsed time for more than a few minutes 
	// Use Greenwich time, or local time for accurate long duration time measurements.
	static Int32 getSystemClock();

	// Returns systems clock ticks per second
	static Int32 getSystemClkTck();

	// Returns local (PST) time in Time object
	static Time getLocalTime();

	// Returns Greenwich mean time in a Time object
	// Seconds since 00:00:00 of 1/1/70
	static Time getgmDateTime();

	// Returns converted <gmttime> in a Time object.
	// Seconds since 00:00:00 of 1/1/70
	static Time convertgmToTime(Nat32 gmttime);

	// Returns Greenwich mean time. Seconds since 00:00:00 of 1/1/70
	static gmTime getgmTime(DateTime *dtp = 0);

	static void initialize(void);

	// Constructor. Initializes the real-time clock.
	Clock();

	// Destructor. Currently does nothing.
	virtual ~Clock();
};
/*----------------------------------------------------------------
CLASS
	TimeWrapper

DESCRIPTION
	This class provides a common interface that is mapped onto
	_VXWORKS_ clock/time library functions, and _WIN32_ clock/time
	library functions.
----------------------------------------------------------------*/
class TimeWrapper
{
	friend class Clock;
protected:
	//Construction/destruction
	TimeWrapper();
	~TimeWrapper();
	//Return the local PST time 
	static void getLocalTime(DateTime *dt);
	//Returns the Greenwich mean time in a DateTime struct <dt> format
	static void getgmDateTime(DateTime *dt);	
	//Returns the Greenwich mean time.  Seconds since 00:00:00 of 1/1/70
	static gmTime getgmTime(DateTime *dtp);
	//Returns the system clock in clock ticks.
	static Int32 getSystemClock();
	//Returns the system clock ticks per second.
	static Int32 getSystemClkTck();
	//**Applies to Vxworks only***
	//Sets the system clock ticks per second. If not a least common multiple of
	//the time base frequency of the clock used with the system timer, will
	//set to the next higher multiple (*NOTE* Hardware dependent).
	static void setSystemClkTck(Int16 tcksPerSecond);
	//Returns the least common multiple according to Euclid's algorithm
	static Int16 Euclid(Int16 a, Int16 b);
	// Calculate number days since epoch (1/1/70)
	// <year> = Year - YEAR0, month, day of month
	// <month> =  days since January 1 - [0,365] 
	// The (<year> + 2)/4 term accounts for leap years, the
	// first of which is 1972 (<year> - YEAR0 == 2)
	static Nat16 epochDays( Nat16 year, Nat16 month, Nat16 yearday );		
	static Nat16 juldate_from_month_day(int *julday, int month, int day, int year);
};
/*----------------------------------------------------------------
CLASS
	Time

DESCRIPTION
	This class contains a local time/day and a 
	interface to all time functions
----------------------------------------------------------------*/
class Time
{	
	friend class Clock;
public:		
	Time();
	Time(DateTime *dt);
	virtual ~Time();
	Boolean isDay();
	Int16 second() const;
	Int16 minute() const;
	Int16 hour() const;
	Int16 yearday() const;
	Int16 monthday() const;
	Int16 weekday() const;
	Int16 month() const;
	Int16 year() const;
	 // Copy operator
	Time &operator = (const Time &src);
	DateTime *datetime();
private:
	DateTime _datetime;
};

class Timer
{
public:
	Timer();
	~Timer();
	void start();
	void stop();
	void reset();
	Int32 elapsedsec();
	Int32 elapsedmin();
	Int32 elapsedmsec();
private:
	Int32 _startTime;
	Int32 _stopTime;
};


#endif // !defined(CLOCK_H)
