//////////////////////////////////////////////////
//Title    : pub_time.c
//Auhtor   : Liu Wei
//Desc     : Linux time function 
//Created  : 2007-06-02
//Revision :
//
//Revision :
//
//////////////////////////////////////////////////

#include "./include/pub_time.h"

/*@ignore@*/
extern void FLogMsg( u8 LogMask , const char *pFmt, ...);
///////////////////////////////////////////////////////////////////////////
//  Name    : GetTickCount
//  Function: return the ms value of the time
//  Note    :                                                          
///////////////////////////////////////////////////////////////////////////
extern long GetTickCount() 
{
	struct timeval tv;

	gettimeofday(&tv, NULL);

	return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}

///////////////////////////////////////////////////////////////////////////
//  Name    : GetCurrentTime
//  Function: get current system time(BcdFormate)
//  Note    : now_time's length is 6: year,month,day,hour,minute,second   
//  Return  : 1 successful 0 fail                                                   
///////////////////////////////////////////////////////////////////////////
extern int GetCurrentTime( u8 *pNowTime )
{
	struct tm *pTMNowTime;
	time_t     tTemptime;
	
	tTemptime  = time(NULL);
	pTMNowTime = localtime(&tTemptime);
	if( pTMNowTime == NULL )
		return 0;
	pNowTime[0] = pTMNowTime->tm_year-100;
	pNowTime[1] = pTMNowTime->tm_mon+1;
	pNowTime[2] = pTMNowTime->tm_mday;
	pNowTime[3] = pTMNowTime->tm_hour;
	pNowTime[4] = pTMNowTime->tm_min;
	pNowTime[5] = pTMNowTime->tm_sec; 
	
	return 1;
}

extern long  timezone;

extern int GetFDCurrentTime( u8 *strTime )
{
	time_t now;
	struct tm *local;	
    	unsigned int iTime;

	time(&now);		
	local = gmtime(&now);	
    	iTime = mktime(local) + 0; //timezone; //DIFF_EPOCH_TO_NTP; 
		
	strTime[0] = (iTime >> 24)&0xff;
	strTime[1] = (iTime >> 16) & 0xff;
	strTime[2] = (iTime >> 8) & 0xff;
	strTime[3] = (iTime >> 0) & 0xff;
	strTime[4] = '\0';
		
	return 1;
}



///////////////////////////////////////////////////////////////////////////
//  Name    : GetCurrentTime
//  Function: get current system time in ASCII format
//  Note    :                                                      
///////////////////////////////////////////////////////////////////////////
extern char *GetAsciiTime()
{
	char   *pAscTime;
	time_t tCurTime;
	
	tCurTime  = time(NULL);
	pAscTime  = ctime(&tCurTime);
	
	return (char *)pAscTime;
}

char   *TimeToStr ( time_t TimeVal, char *Format )
{
    struct tm *tm;
    //static char sBuff[128];
    char   *String;
    char   *pCh;
    char   *Fmt;

    if ( Format != NULL)
    {
        Fmt = Format;
    }
    else
    {
        //Use %H instead of %k as %H is more portable
        Fmt = "%a %b %e %H:%M:%S %Y %Z";
    }
    tm = localtime ( &TimeVal );
    if (  NULL != tm )
    {
    	String = asctime ( tm );
        if (  NULL != String  )
        {
        	pCh = strchr ( String, '\n' );
            if ( NULL != pCh )
            {
                *pCh = CNULL;
            }
            return String;
        }
    }

    FLogMsg ( PIF_DBG, "TimeToStr() failed - No conversion func defined?" );

    return ( char * ) NULL;
}



///////////////////////////////////////////////////////////////////////////
//  Name    : GetTimeHMS
//  Function: get current system time string of hour mininute and second
//  Note    :                                                      
///////////////////////////////////////////////////////////////////////////
extern char *GetTimeHMS(char *pTimeBuf)
{
	struct tm *pTM,tTM;
	long lCurTime;

	pTM = &tTM;
	lCurTime = time((long *)0);
	pTM = localtime(&lCurTime);
	sprintf(pTimeBuf,"%02d:%02d:%02d",pTM->tm_hour,pTM->tm_min,pTM->tm_sec);
	pTimeBuf[8] = CNULL;	
	
	return pTimeBuf;
}

/*@end@*/
