//------------------------ CSPTime.h --------------------------

//*************************************************************
//*****       Copyright ©2002 - 2004 Micronovae Ltd       *****
//*****                All rights reserved                *****
//*************************************************************


#ifndef _CSPTIME
#define _CSPTIME

#include "CSPConf.h"
#include "CSPLib.h"
#include "CSPString.h"
#include "CSPWString.h"
#include <time.h>


//------------------ CSPTime class ----------------------
class CSPLIB_CLASS_EXPORT CSPTime : public CSPObject
{
	friend class CSPDateTime;
	public:
		enum MODE{ H24=0, AM, PM };

		CSPTime();
		CSPTime( unsigned long ulSecond, unsigned long ulMinute, unsigned long ulHour, unsigned long ulMode = H24);
		CSPTime(const CSPTime& tTime);
		virtual ~CSPTime();
		CSPTime& operator = ( const CSPTime& tTime );
		CSPTime& operator = ( long lSeconds );
		CSPTime& operator += ( long lSeconds );
		CSPTime& operator -= ( long lSeconds );
		CSPTime& operator ++();
		CSPTime& operator --();
		CSPTime operator ++( int );
		CSPTime operator --( int );
		bool operator < ( const CSPTime& tTime ) const;
		bool operator > ( const CSPTime& tTime ) const;
		bool operator <= ( const CSPTime& tTime ) const;
		bool operator >= ( const CSPTime& tTime ) const;
		bool operator == ( const CSPTime& tTime ) const;
		bool operator != ( const CSPTime& tTime ) const;
		long operator - ( const CSPTime& tTime ) const;
		CSPTime operator - ( long lSeconds ) const;
		CSPTime operator + ( long lSeconds ) const;

		void SetTime( unsigned long ulSecond, unsigned long ulMinute, unsigned long ulHour, unsigned long ulMode = H24 );
		unsigned long GetHour() const;
		unsigned long GetMinute() const;
		unsigned long GetSecond() const;
		void SetHour( unsigned long ulHour, unsigned long ulMmode = H24 );
		void SetMinute( unsigned long ulMinute );
		void SetSecond( unsigned long ulSecond );

		CSPString GetString() const;
		CSPWString GetWString() const;

		static CSPTime GetCurTime();
		static CSPTime GetCurGMTTime();
		static bool SetLocale( const CSPString& strLocale );

	protected:
		unsigned long Hour;
		unsigned long Minute;
		unsigned long Second;
};



//------------------ CSPDate class ----------------------
class CSPLIB_CLASS_EXPORT CSPDate : public CSPObject
{
	friend class CSPDateTime;
	public:
		CSPDate();
		CSPDate( unsigned long ulDay, unsigned long ulMonth, long lYear );
		CSPDate( const CSPDate& dDate );
		virtual ~CSPDate();
		CSPDate& operator = ( const CSPDate& dDate );
		CSPDate& operator += ( long lDays );
		CSPDate& operator -= ( long lDays );
		CSPDate operator ++ ( int );
		CSPDate operator -- ( int );
		CSPDate& operator ++ ();
		CSPDate& operator -- ();
		bool operator < ( const CSPDate& dDate ) const;
		bool operator > ( const CSPDate& dDate ) const;
		bool operator <= ( const CSPDate& dDate ) const;
		bool operator >= ( const CSPDate& dDate ) const;
		bool operator == ( const CSPDate& dDate ) const;
		bool operator != ( const CSPDate& dDate ) const;
		long operator - ( const CSPDate& dDate ) const;
		CSPDate operator - ( long lDays ) const;
		CSPDate operator + ( long lDays ) const;

		enum{ SUNDAY = 1, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
		enum{ JANUARY = 1, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };

		void SetDate( unsigned long ulDay, unsigned long ulMonth, long lYear );
		void SetNull();

		long GetYear() const;
		unsigned long GetMonth() const;
		CSPString GetMonthString( bool bFull = true ) const;
		CSPWString GetMonthWString( bool bFull = true ) const;
		unsigned long GetDay() const;
		unsigned long GetWeekDay() const;
		CSPString GetWeekDayString( bool bFull = true ) const;
		CSPWString GetWeekDayWString( bool bFull = true ) const;
		unsigned long GetYearDay() const;
		CSPString GetGMTString() const;
		bool IsNull() const;

		CSPString GetString() const;
		CSPWString GetWString() const;

		static CSPDate GetCurDate();
		static CSPDate GetCurGMTDate();
		static bool IsLeapYear( long year );
		static bool SetLocale( const CSPString& strLocale );

	protected:
		static unsigned long MonthDaysLookup[12];
		long Year;
		unsigned long Month;
		unsigned long YearDay;
		unsigned long MonthDay;
		unsigned long WeekDay;

		static long GetDateLong(unsigned long day,unsigned long month,long year);
		void SetDateFromLong(long lLong);
		CSPString GetWeekDayStr() const;
		CSPString GetMonthStr() const;
		void SetYear( long lYear );
		void SetMonth( unsigned long ulMonth );
		void SetDay( unsigned long ulDay );
};



//------------------ CSPDateTime class ----------------------

class CSPLIB_CLASS_EXPORT CSPDateTime
{
	public:
		CSPDate Date;
		CSPTime Time;

		CSPDateTime();
		CSPDateTime( const CSPDate& dDate, const CSPTime& tTime, int idst = -1);
		CSPDateTime( unsigned long ulSecond, unsigned long ulMinute, unsigned long ulHour, unsigned long ulDay, unsigned long ulMonth, unsigned long ulYear, int idst = -1);
		CSPDateTime( const CSPDateTime& dtDateTime);
		virtual ~CSPDateTime();
		CSPDateTime& operator = ( const CSPDateTime& dtDateTime );
		CSPDateTime& operator = ( const CSPDate& dDate );
		CSPDateTime& operator = ( const CSPTime& tTime );
		CSPDateTime& operator += ( long lSeconds );
		CSPDateTime& operator -= ( long lSeconds );
		bool operator < ( const CSPDateTime& dtDateTime ) const;
		bool operator > ( const CSPDateTime& dtDateTime ) const;
		bool operator <= ( const CSPDateTime& dtDateTime ) const;
		bool operator >= ( const CSPDateTime& dtDateTime ) const;
		bool operator == ( const CSPDateTime& dtDateTime ) const;
		bool operator != ( const CSPDateTime& dtDateTime ) const;		
		CSPDateTime operator - ( long lSeconds ) const;
		CSPDateTime operator + ( long lSeconds ) const;
		void CalculateDST();
		CSPString GetString() const;
		CSPWString GetWString() const;

		CSPString GetGMTString() const;
		static CSPDateTime GetCurDateTime();
		static CSPDateTime GetCurGMTDateTime();
		static bool SetLocale( const CSPString& strLocale );

	protected:

		int iDST;
};

#endif
