//----------------------------------------------------
/**
	\file		cortex-m4/inc/ctktime.h
	\ingroup	general
	\ingroup	cortex-m4

	\brief		Knihovna pro praci s casem
				<<< CORTEX M4 >>>

	\author		ConTeK s.r.o., Pavel Bazika
	\version	01.01
	\date		02.03.2007
*/
//----------------------------------------------------

#ifndef _CTKTIME_H_
#define _CTKTIME_H_

#include <cortex-m4/inc/ctktypes.h>
#include <time.h>

//----------------------------------------------------
/**
	\brief	Platforme nezavisle definice
*/
//----------------------------------------------------

#define ctkdatetime32_t	time_t		//!< Hlavni typ pro praci s datem a casem
#define ctktime32_t		time_t		//!< Hlavni typ pro praci s casem
//#define ctkdatetime64_t	__time64_t		//!< Hlavni typ pro praci datem a casem
//#define ctktime64_t		__time64_t		//!< Hlavni typ pro praci s casem
#define ctkdate16_t		uint16_t		//!< Hlavni typ pro praci s datem

//----------------------------------------------------
/**
	\brief	Platforme zavisle definice
*/
//----------------------------------------------------
#define ctkdatetime_t	time_t	//!< Hlavni typ pro praci s casem
#define ctktime_t		time_t	//!< Hlavni typ pro praci s casem
#define ctktm_t			struct tm			//!< Struktura se slozkami casu

//----------------------------------------------------
/**
	\brief	Funkce konvertujici 32bitovy cas na 64bitovy

	\param	_t64	Ukazatel na strukturu pro ulozeni vysledku
	\param	_t32	Konvertovany cas

	\return	TRUE v pripade uspechu

	\note	Funkce je reentrantni
*/
//----------------------------------------------------

//bool_t ConvTime32To64(ctktime64_t * const _t64, const ctktime32_t _t32);


//----------------------------------------------------
/**
	\brief	Funkce konvertujici 64bitovy cas na 32bitovy

	\param	_t32	Ukazatel na strukturu pro ulozeni vysledku
	\param	_t64	Konvertovany cas

	\return	TRUE v pripade uspechu, jinak FALSE a _t64 je naplnen 0xFFFFFFFF

	\note	Funkce je reentrantni
*/
//----------------------------------------------------

//bool_t ConvTime64To32(ctktime32_t * const _t32, const ctktime64_t _t64);


//----------------------------------------------------
/**
	\brief	Funkce konvertujici 32bitovy cas na 64bitovy

	\param	_dt64	Ukazatel na strukturu pro ulozeni vysledku
	\param	_dt32	Konvertovany cas

	\return	TRUE v pripade uspechu

	\note	Funkce je reentrantni
*/
//----------------------------------------------------

//bool_t ConvDateTime32To64(ctkdatetime64_t * const _dt64, const ctkdatetime32_t _dt32);


//----------------------------------------------------
/**
	\brief	Funkce konvertujici 64bitovy cas na 32bitovy

	\param	_dt32	Ukazatel na strukturu pro ulozeni vysledku
	\param	_dt64	Konvertovany cas

	\return	TRUE v pripade uspechu

	\note	Funkce je reentrantni
*/
//----------------------------------------------------

//bool_t ConvDateTime64To32(ctkdatetime32_t * const _dt32, const ctkdatetime64_t _dt64);


//----------------------------------------------------
/**
	\brief	Konverzni funkce mezi platforme zavislym a nezavislym casem

	\note	Zdanlive chybna pretypovani jsou zde kvuli warningum prekladace
			Ve skutecnosti se jedna o pretypovani na stejny typ, jako je argument
			Bohuzel nelze rozhodnout o velikosti typu jiz ve fazi preprocessingu
*/
//----------------------------------------------------

static inline bool_t ConvTimeTo32(ctktime32_t * const _t32, const ctktime_t _time)
{
	*_t32 = (ctktime32_t)_time;
	return TRUE;
}

/*static inline bool_t ConvTimeTo64(ctktime64_t * const _t64, const ctktime_t _time)
{
	if (sizeof(ctktime_t) == 4)
	{
		return ConvTime32To64(_t64, (ctktime32_t)_time);
	}
	else
	{
		 *_t64 = (ctktime64_t)_time;
		 return TRUE;
	}
}*/

static inline bool_t ConvDateTimeTo32(ctkdatetime32_t * const _dt32, const ctkdatetime_t _datetime)
{
	*_dt32 = (ctkdatetime32_t)_datetime;
	return TRUE;
}

/*static inline bool_t ConvDateTimeTo64(ctkdatetime64_t * const _dt64, const ctkdatetime_t _datetime)
{
	if (sizeof(ctkdatetime_t) == 4)
	{
		return ConvDateTime32To64(_dt64, (ctkdatetime32_t)_datetime);
	}
	else
	{
		*_dt64 = (ctkdatetime64_t)_datetime;
		return TRUE;
	}
}*/

static inline bool_t ConvTimeFrom32(ctktime_t * const _time, const ctktime32_t _t32)
{
	*_time = _t32;
	return TRUE;
}

/*static inline bool_t ConvTimeFrom64(ctktime_t * const _time, const ctktime64_t _t64)
{
	if (sizeof(ctktime_t) == 4)
	{
		return ConvTime64To32((ctktime32_t *)_time, _t64);
	}
	else
	{
		*(ctktime64_t *)_time = _t64;
		return TRUE;
	}
}*/

static inline bool_t ConvDateTimeFrom32(ctkdatetime_t * const _datetime, const ctkdatetime32_t _dt32)
{
	*_datetime = _dt32;
	return TRUE;
}

/*static inline bool_t ConvDateTimeFrom64(ctkdatetime_t * const _datetime, const ctkdatetime64_t _dt64)
{
	if (sizeof(ctkdatetime_t) == 4)
	{
		return ConvDateTime64To32((ctkdatetime32_t *)_datetime, _dt64);
	}
	else
	{
		*(ctkdatetime64_t *)_datetime = _dt64;
		return TRUE;
	}
}*/


//---------------------------------------------------
/**
	\brief	Prevod struktury ctktm na cele cislo ctktime_t (UTC)

	\param	timeptr		struktura ctktm
	\return Pokud je nektera z hodnot v tm mimo rozsah funkce vraci 0
*/
//---------------------------------------------------
#define mktime mkgmtime

ctktime_t mkgmtime( ctktm_t * timeptr );




//----------------------------------------------------
/**
	\brief	Funkce pro Inicializaci
*/
//----------------------------------------------------
static inline void CtktimeInitialize(void)
{}

//----------------------------------------------------
/**
	\brief	Funkce pro ukonceni
*/
//----------------------------------------------------
static inline void CtktimeFinalize(void)
{}

#endif // #ifndef _CTKTIME_H_
