/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    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.

***************************************************************************/


#ifndef DDTIME_H
#define DDTIME_H

#include <ddbaseobject.h>

using namespace std;

/// ddbasetimeval is the baseclass for ddbasetime and ddbasedate.
/**
	Ddtimeval defines shortcut methods to access the datafields
	of ddbasetime and ddbasedate.
	@see ddbasetime
	@see ddbasedate.
 */

class ddbasetimeval : public ddbaseobject
{

public:


	enum dd_timefields
	{
		tf_millis=0,
		tf_seconds,
		tf_minutes,
		tf_hours,
		tf_days,
		tf_weeks,
		tf_months,
		tf_years,

		tf_dom,
		tf_dow,
		tf_doy,

		tf_size
	};
	
	virtual ~ddbasetimeval()
	{
	
	}
	
	virtual int  get( size_t field ) const		  = 0;
	

	int get_millis() const
	{
		return get( ddbasetimeval::tf_millis );
	}

	int get_seconds() const
	{
		return get( ddbasetimeval::tf_seconds );
	}

	int get_minutes() const
	{
		return get( ddbasetimeval::tf_minutes );
	}

	int get_hours() const
	{
		return get( ddbasetimeval::tf_hours );
	}

	int get_day() const
	{
		return get( ddbasetimeval::tf_days );
	}

	int get_week() const
	{
		return get( ddbasetimeval::tf_weeks );
	}

	int get_month() const
	{
		return get( ddbasetimeval::tf_months );
	}

	int get_year() const
	{
		return get( ddbasetimeval::tf_years );
	}

	// for date
	int get_day_of_month() const
	{
		return get( ddbasetimeval::tf_dom );
	}

	int get_day_of_week() const
	{
		return get( ddbasetimeval::tf_dow );
	}

	int get_day_of_year() const
	{
		return get( ddbasetimeval::tf_doy );
	}

	static const size_t SECS_PER_DAY		= 86400;
	static const size_t MSECS_PER_DAY 	= 86400000;
	static const size_t SECS_PER_HOUR 	= 3600;
	static const size_t MSECS_PER_HOUR	= 3600000;
	static const size_t SECS_PER_MIN		= 60;
	static const size_t MSECS_PER_MIN 	= 60000;

};


/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    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.

***************************************************************************/


/// ddbasetime is a simple representation for daytime and duration.
/**

*/

class ddbasetime : public ddbasetimeval
{

public:

	ddbasetime();
	ddbasetime( time_t local );
	ddbasetime( int h, int m, int s, int ms );
	ddbasetime( const ddbasetime& src );
	ddbasetime& operator=( const ddbasetime& src );
	virtual ~ddbasetime();

	bool operator==( const ddbasetime& src ) const;
	bool operator!=( const ddbasetime& src ) const;
	bool operator<( const ddbasetime& src ) const;
	bool operator<=( const ddbasetime& src ) const;
	bool operator>( const ddbasetime& src ) const;
	bool operator>=( const ddbasetime& src ) const;
	
	
	// impl. of ddbasetimeval interface
	int  		get( size_t field ) const;
	
	bool 		set_hms( int h, int m, int s, int ms=0 );
	bool 		is_valid() const;

	ddbasetime& add_secs( int secs );
	int 				get_secs_to( const ddbasetime& src ) const;
	ddbasetime& add_milli_secs( int ms );
	int 				get_milli_secs_to( const ddbasetime& src ) const;

	void   	start();
	double 	restart();
	double 	elapsed();
	
	ddstring to_string() const;
	ddstring to_string_ms() const;

	static ddbasetime now();
	static bool is_valid( int h, int m, int s, int ms );

protected:

	size_t  _millis;

};
	

/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    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.

***************************************************************************/


/// ddbasedate: a rotten date implementation
/**
	Status: <b>trash</b><br>
*/
	
class ddbasedate : public ddbasetimeval
{

public:

	ddbasedate();
	ddbasedate( const ddbasedate& src );
	ddbasedate( time_t epoch );
	//ddbasedate( char* dateddstring );
	//ddbasedate( tm *_time );
	ddbasedate& operator=( const ddbasedate& src );
	virtual ~ddbasedate();
	
	
	// impl. of ddbasetimeval interface
	int  get( size_t field ) const;
	void set( size_t field, int value );
	void add( size_t field, int value );
	
	bool operator==( const ddbasedate& src ) const;
	bool operator!=( const ddbasedate& src ) const;
	bool operator<( const ddbasedate& src ) const;
	bool operator<=( const ddbasedate& src ) const;
	bool operator>( const ddbasedate& src ) const;
	bool operator>=( const ddbasedate& src ) const;
	
	ddstring to_string() const;
	ddstring to_string( const ddstring& format ) const;
	ddstring to_string_gmt() const;
	ddstring to_string_de() const;
	ddstring to_string_en() const;
	ddstring to_string_mysql() const;
	
	static ddbasedate now();

protected:

	time_t _epoch;

};

#endif
