added qse_localtime() but it needs to be of the dependency on the system's localtime/localtime_r() later

This commit is contained in:
hyung-hwan 2008-12-23 04:12:59 +00:00
parent 311e3bf4e7
commit c9a39fa64f
3 changed files with 76 additions and 10 deletions

View File

@ -51,7 +51,7 @@ struct qse_btime_t
int wday; /* 0(sun)-6(sat) */
int yday; /* 0(jan 1) to 365 */
int isdst;
int offset;
/*int offset;*/
};
#ifdef __cplusplus
@ -87,7 +87,7 @@ int qse_settime (
*
* SYNOPSIS
*/
void qse_gmtime (
int qse_gmtime (
qse_ntime_t nt,
qse_btime_t* bt
);
@ -105,13 +105,25 @@ int qse_localtime (
);
/******/
/****f* qse.cmn/qse_mktime
/****f* qse.cmn/qse_timegm
* NAME
* qse_mktime - convert broken-down time to numeric time
* qse_timegm - convert broken-down time to numeric time
*
* SYNOPSIS
*/
int qse_mktime (
int qse_timegm (
const qse_btime_t* bt,
qse_ntime_t* nt
);
/******/
/****f* qse.cmn/qse_timelocal
* NAME
* qse_timelocal - convert broken-down time to numeric time
*
* SYNOPSIS
*/
int qse_timelcoal (
const qse_btime_t* bt,
qse_ntime_t* nt
);

View File

@ -1073,8 +1073,9 @@ static int bfn_systime (qse_awk_run_t* run, const qse_char_t* fnm, qse_size_t fn
return 0;
}
static int bfn_gmtime (qse_awk_run_t* run, const qse_char_t* fnm, qse-size_t fnl)
static int bfn_gmtime (qse_awk_run_t* run, const qse_char_t* fnm, qse_size_t fnl)
{
/* TODO: *********************** */
qse_ntime_t nt;
qse_btime_t bt;
@ -1083,6 +1084,12 @@ static int bfn_gmtime (qse_awk_run_t* run, const qse_char_t* fnm, qse-size_t fnl
/* TODO: create an array containing
* .....
*/
return -1;
}
static int bfn_localtime (qse_awk_run_t* run, const qse_char_t* fnm, qse_size_t fnl)
{
return -1;
}
#define ADD_FUNC(awk,name,min,max,bfn) \
@ -1105,6 +1112,7 @@ static int add_functions (qse_awk_t* awk)
ADD_FUNC (awk, QSE_T("srand"), 0, 1, bfn_srand);
ADD_FUNC (awk, QSE_T("systime"), 0, 0, bfn_systime);
ADD_FUNC (awk, QSE_T("gmtime"), 0, 0, bfn_gmtime);
ADD_FUNC (awk, QSE_T("localtime"), 0, 0, bfn_localtime);
/*
ADD_FUNC (awk, QSE_T("strftime"), 0, 2, bfn_strftime);
ADD_FUNC (awk, QSE_T("strfgmtime"), 0, 2, bfn_strfgmtime);

View File

@ -3,6 +3,7 @@
*/
#include <qse/cmn/time.h>
#include "mem.h"
#ifdef _WIN32
#include <windows.h>
@ -115,8 +116,9 @@ static void brkdntime (qse_ntime_t nt, qse_btime_t* bt, qse_ntime_t offset)
qse_ntime_t year = QSE_EPOCH_YEAR;
nt += offset;
/* TODO: support bt->msecs */
/*bt->msecs = nt % QSEC_MSECS_PER_SEC;*/
bt->msec = nt % QSE_MSECS_PER_SEC;
if (bt->msec < 0) bt->msec = QSE_MSECS_PER_SEC + bt->msec;
secs = nt / QSE_MSECS_PER_SEC;
days = secs / QSE_SECS_PER_DAY;
@ -171,10 +173,54 @@ static void brkdntime (qse_ntime_t nt, qse_btime_t* bt, qse_ntime_t offset)
bt->mday = days + 1;
bt->isdst = 0;
bt->offset = offset;
/*bt->offset = offset;*/
}
void qse_gmtime (qse_ntime_t nt, qse_btime_t* bt)
int qse_gmtime (qse_ntime_t nt, qse_btime_t* bt)
{
brkdntime (nt, bt, 0);
return 0;
}
int qse_localtime (qse_ntime_t nt, qse_btime_t* bt)
{
struct tm* tm;
time_t t = (time_t)(nt / QSE_MSECS_PER_SEC);
qse_ntime_t rem = nt % QSE_MSECS_PER_SEC;
/* TODO: remove dependency on localtime/localtime_r */
#ifdef _WIN32
tm = localtime (&t);
#else
struct tm btm;
tm = localtime_r (&t, &btm);
#endif
if (tm == QSE_NULL) return -1;
QSE_MEMSET (bt, 0, QSE_SIZEOF(*bt));
bt->msec = (rem >= 0)? rem: (QSE_MSECS_PER_SEC + rem);
bt->sec = tm->tm_sec;
bt->min = tm->tm_min;
bt->hour = tm->tm_hour;
bt->mday = tm->tm_mday;
bt->mon = tm->tm_mon;
bt->year = tm->tm_year;
bt->wday = tm->tm_wday;
bt->yday = tm->tm_yday;
bt->isdst = tm->tm_isdst;
/*bt->offset = tm->tm_offset;*/
return 0;
}
int qse_timegm (const qse_btime_t* bt, qse_ntime_t* nt)
{
return -1;
}
int qse_timelocal (const qse_btime_t* bt, qse_ntime_t* nt)
{
return -1;
}