/* 
** 
** file name: tim.c
**
** time functions
**
*/

#include "../include/includes.h"


char * GetASCTime(char *timep)
{
	struct tm  *tp, tb;
	long       ltime;

	tp = &tb;
	ltime = time((long *)0);
	tp = localtime(&ltime);
	timep = asctime(tp);
	timep[24] = 0;
	return timep;
}

char * CharTime(char *ptr, const time_t *timep)
{
		strncpy(ptr, ctime(timep), 24);
		ptr[24] = 0;
		return ptr;
}

/* Get current hour */
/*
int gethour(char * hour_buf)
{
    struct tm *t, tt;
    long curr_time;
    
    t=&tt;
    curr_time=time((long *)0);
    t=localtime(&curr_time);
    sprintf(hour_buf, "%d", t -> tm_hour);
    hour_buf[2]=0;
    return t -> tm_hour;
}    
*/
u_short
GetHour(time_t t)
{
	struct tm tmb;

	memcpy(&tmb, localtime(&t), sizeof(struct tm));
	return tmb.tm_hour;
}
	
u_short
GetTmHour()
{
	struct tm tmb;
	time_t   curr_time;

	curr_time = time(NULL);
	memcpy(&tmb, localtime(&curr_time), sizeof(struct tm));
	return tmb.tm_hour;
}

/* Get current hour */
/*
int getmin(char * min_buf)
{
    struct tm *t, tt;
    long curr_time;
    
    t=&tt;
    curr_time=time((long *)0);
    t=localtime(&curr_time);
    sprintf(min_buf, "%d", t -> tm_min);
    min_buf[2]=0;
    return t -> tm_min;
}
*/
u_short
GetMinute(time_t t)
{
	struct tm tmb;
	memcpy(&tmb, localtime(&t), sizeof(struct tm));
	return tmb.tm_min;
}

u_short
GetTmMinute()
{
	struct tm tmb;
	time_t   curr_time;

	curr_time = time(NULL);
	memcpy(&tmb, localtime(&curr_time), sizeof(struct tm));
	return tmb.tm_min;
}

/* Get current time */
/*
int getsec(char * sec_buf)
{
    struct tm *t, tt;
    long curr_time;
    
    t=&tt;
    curr_time=time((long *)0);
    t=localtime(&curr_time);
    sprintf(sec_buf, "%d", t -> tm_sec);
    sec_buf[2]=0;
    return t -> tm_sec;
}
*/
u_short
GetTmSecond()
{
	struct tm tmb;
	time_t   curr_time;

	curr_time = time(NULL);
	memcpy(&tmb, localtime(&curr_time), sizeof(struct tm));
	return tmb.tm_sec;
}

/* Get current time */
/*
void gettime(time_buf)
char    *time_buf;
{
    struct tm *t,tt;
    long curr_time;

    t=&tt;
    curr_time=time((long *)0);
    t=localtime(&curr_time);
    sprintf(time_buf,"%02d:%02d:%02d",t->tm_hour,t->tm_min,t->tm_sec);
    time_buf[8]=0;
}
*/

const char * ppsgetdaytime(char *daytime_buf)
{
    struct tm *t,tt;
    long curr_time;

    t=&tt;
    curr_time=time((long *)0);
    t=localtime(&curr_time);
    sprintf(daytime_buf,"%04d%02d%02d%02d%02d%02d",
            t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour,
            t->tm_min, t->tm_sec);
    daytime_buf[14] = 0;
	return daytime_buf;
}

const int getnowtime(int *time_buf)
{
    struct tm *t,tt;
    long curr_time;

    t=&tt;
    curr_time=time((long *)0);
    t=localtime(&curr_time);
    time_buf[0] = t->tm_year+1900;
    time_buf[1] = t->tm_mon+1;
    time_buf[2] = t->tm_mday;
    time_buf[3] = t->tm_hour;
    time_buf[4] = t->tm_min;
    time_buf[5] = t->tm_sec;
    time_buf[6] = t->tm_wday;
    return 1;
}

const char * AsciiTime(char *ptr, time_t time)
{
    struct tm *t,tt;
    
    t=&tt;
    t=localtime(&time);
    sprintf(ptr,"%04d%02d%02d%02d%02d%02d",
            t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour,
            t->tm_min, t->tm_sec);
    ptr[14] = 0;
	return ptr;
}
    

const char * AsciiTimeYMD(char *ptr, time_t time)
{
    struct tm *t,tt;
    
    t=&tt;
    t=localtime(&time);

    sprintf(ptr,"%04d-%02d-%02d",
            t->tm_year+1900, t->tm_mon+1, t->tm_mday);
    
    return ptr;
}

const char * AsciiTimeYMDHMS(char *ptr, time_t time)
{
    struct tm *t,tt;
    
    t=&tt;
    t=localtime(&time);
    sprintf(ptr,"%02d-%02d-%04d %02d:%02d:%02d",
            t->tm_mday,  t->tm_mon+1,t->tm_year+1900, t->tm_hour,
            t->tm_min, t->tm_sec);
    ptr[19] = 0;
	return ptr;
}

u_short
GetTmDay()
{
	struct tm tmb;
	time_t   curr_time;

	curr_time = time(NULL);
	memcpy(&tmb, localtime(&curr_time), sizeof(struct tm));
	return tmb.tm_mday;
}

u_short
GetTmMonth()
{
	struct tm tmb;
	time_t   curr_time;

	curr_time = time(NULL);
	memcpy(&tmb, localtime(&curr_time), sizeof(struct tm));
	return tmb.tm_mon+1;
}
/*
int getday(day_buf)
char    *day_buf;
{
    struct tm *t,tt;
    long curr_date;

    t=&tt;
    curr_date=time((long *)0);
    t=localtime(&curr_date);
    sprintf(day_buf,"%02d", t->tm_mday);
    day_buf[2]=0;
    return t->tm_mday;
}
*/
u_short
GetTmYear()
{
	struct tm tmb;
	time_t   curr_time;

	curr_time = time(NULL);
	memcpy(&tmb, localtime(&curr_time), sizeof(struct tm));
	return tmb.tm_year+1900;
}

int getweek(week_buf)
char    *week_buf;
{
    struct tm *t,tt;
    long curr_date;

    t=&tt;
    curr_date=time((long *)0);
    t=localtime(&curr_date);
    sprintf(week_buf,"%02d", t->tm_wday);
    week_buf[2]=0;
    return t->tm_wday;
}
/*
int getmon(mon_buf)
char    *mon_buf;
{
    struct tm *t,tt;
    long curr_date;

    t=&tt;
    curr_date=time((long *)0);
    t=localtime(&curr_date);
    sprintf(mon_buf,"%02d", t->tm_mon+1);
    mon_buf[2]=0;
    return t->tm_mon;
}

int getyear(year_buf)
char    *year_buf;
{
    struct tm *t,tt;
    long curr_date;

    t=&tt;
    curr_date=time((long *)0);
    t=localtime(&curr_date);
    sprintf(year_buf,"%04d", t->tm_year+1900);
    year_buf[4]=0;
    return t->tm_year;
}
*/
/* Get current date */
/*
void getdate(date_buf)
char    *date_buf;
{
    struct tm *t,tt;
    long curr_date;

    t=&tt;
    curr_date=time((long *)0);
    t=localtime(&curr_date);
    sprintf(date_buf,"%04d-%02d-%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday);
    date_buf[10]=0;
}
*/
time_t 
MakeTime(struct tm *timep, char *ptr)
{
	char         tempbuf[5];
	struct tm    tm, *timeptr;
	time_t       retval;
	memset(&tm, 0, sizeof(struct tm));
	timeptr = &tm;
	strncpy(tempbuf, ptr, 4);
	tempbuf[4] = 0;
	timeptr->tm_year = atoi(tempbuf) - 1900;
	strncpy(tempbuf, ptr+4, 2);
	tempbuf[2] = 0;
	timeptr->tm_mon = atoi(tempbuf) - 1;
	strncpy(tempbuf, ptr+6, 2);
	tempbuf[2] = 0;
	timeptr->tm_mday = atoi(tempbuf);
	strncpy(tempbuf, ptr+8, 2);
	tempbuf[2] = 0;
	timeptr->tm_hour = atoi(tempbuf);
	strncpy(tempbuf, ptr+10, 2);
	tempbuf[2] = 0;
	timeptr->tm_min = atoi(tempbuf);
	strncpy(tempbuf, ptr+12, 2);
	tempbuf[2] = 0;
	timeptr->tm_sec = atoi(tempbuf);
	retval = mktime(timeptr);
	if (NULL != timep)
		timep = timeptr;
	return (retval);
}

char *
GetTmDate(char *date_buf)
{
	struct tm tmb;
	time_t   curr_time;

	curr_time = time(NULL);
	memcpy(&tmb, localtime(&curr_time), sizeof(struct tm));
	sprintf(date_buf, "%04d%02d%02d", 
					tmb.tm_year+1900, tmb.tm_mon+1, tmb.tm_mday);
	date_buf[9] = 0;
	return (char *)date_buf;
}

void
CharToTimeStamp(u_char *btime, char *atime)
{
    char timestamp[128]={""};
    char buf[128];
    BcdToAscii(buf, (BYTE *)atime, 14);
    memcpy(timestamp,&buf[2],12);

    sprintf(&timestamp[12],"%02d",0);
	sprintf(&timestamp[14],"%02d",8);
    AsciiToBcd(btime,timestamp,16);
}
