enhanced qse_timegm

This commit is contained in:
hyung-hwan 2009-06-07 07:42:57 +00:00
parent ebd6199812
commit 08c1d422f3
9 changed files with 125 additions and 50 deletions

View File

@ -47,7 +47,7 @@ public:
this->alloc = alloc_mem;
this->realloc = realloc_mem;
this->free = free_mem;
this->data = this;
this->udd = this;
}
/**
@ -88,25 +88,25 @@ protected:
/**
* a bridge function from the qse_mmgr_t type the allocMem() function.
*/
static void* alloc_mem (void* data, qse_size_t n) throw ()
static void* alloc_mem (void* udd, qse_size_t n) throw ()
{
return ((Mmgr*)data)->allocMem (n);
return ((Mmgr*)udd)->allocMem (n);
}
/**
* a bridge function from the qse_mmgr_t type the reallocMem() function.
*/
static void* realloc_mem (void* data, void* ptr, qse_size_t n) throw ()
static void* realloc_mem (void* udd, void* ptr, qse_size_t n) throw ()
{
return ((Mmgr*)data)->reallocMem (ptr, n);
return ((Mmgr*)udd)->reallocMem (ptr, n);
}
/**
* a bridge function from the qse_mmgr_t type the freeMem() function.
*/
static void free_mem (void* data, void* ptr) throw ()
static void free_mem (void* udd, void* ptr) throw ()
{
return ((Mmgr*)data)->freeMem (ptr);
return ((Mmgr*)udd)->freeMem (ptr);
}
};

View File

@ -1,5 +1,5 @@
/*
* $Id: mem.h 75 2009-02-22 14:10:34Z hyunghwan.chung $
* $Id: mem.h 186 2009-06-06 13:42:57Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -30,15 +30,15 @@
/* allocate a memory block */
#define QSE_MMGR_ALLOC(mmgr,size) \
((mmgr)->alloc((mmgr)->data,size))
((mmgr)->alloc((mmgr)->udd,size))
/* reallocate a memory block */
#define QSE_MMGR_REALLOC(mmgr,ptr,size) \
((mmgr)->realloc((mmgr)->data,ptr,size))
((mmgr)->realloc((mmgr)->udd,ptr,size))
/* free a memory block */
#define QSE_MMGR_FREE(mmgr,ptr) \
((mmgr)->free((mmgr)->data,ptr))
((mmgr)->free((mmgr)->udd,ptr))
#ifdef __cplusplus
extern "C" {

View File

@ -1,5 +1,5 @@
/*
* $Id: time.h 75 2009-02-22 14:10:34Z hyunghwan.chung $
* $Id: time.h 186 2009-06-06 13:42:57Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -46,7 +46,7 @@
#define QSE_NSECS_PER_USEC (1000)
#define QSE_USECS_PER_SEC (QSE_USECS_PER_MSEC*QSE_MSECS_PER_SEC)
#define QSE_IS_LEAPYEAR(year) (!((year)%4) && (((year)%100) || !((year)%400)))
#define QSE_IS_LEAPYEAR(year) ((!((year)%4) && ((year)%100)) || !((year)%400))
#define QSE_DAYS_PER_YEAR(year) (QSE_IS_LEAPYEAR(year)? 366: 365)
/* number of milliseconds since the Epoch (00:00:00 UTC, Jan 1, 1970) */
@ -93,21 +93,16 @@ int qse_settime (
/******/
/****f* Common/qse_gmtime
* NAME
* qse_gmtime - convert numeric time to broken-down time
* SYNOPSIS
/**
* The qse_gmtime() function converts numeric time to broken-down time.
*/
int qse_gmtime (
qse_ntime_t nt,
qse_btime_t* bt
);
/******/
/****f* Common/qse_localtime
* NAME
* qse_localtime - convert numeric time to broken-down time
* SYNOPSIS
/**
* The qse_localtime() converts numeric time to broken-down time
*/
int qse_localtime (
qse_ntime_t nt,
@ -115,11 +110,10 @@ int qse_localtime (
);
/******/
/****f* Common/qse_timegm
* NAME
* qse_timegm - convert broken-down time to numeric time
*
* SYNOPSIS
/**
* The qse_timegm() converts broken-down time to numeric time. It is the
* inverse of qse_gmtime(). It is useful if the broken-down time is in UTC
* and the local environment is not.
*/
int qse_timegm (
const qse_btime_t* bt,
@ -127,11 +121,9 @@ int qse_timegm (
);
/******/
/****f* Common/qse_timelocal
* NAME
* qse_timelocal - convert broken-down time to numeric time
*
* SYNOPSIS
/**
* The qse_timelocal() converts broken-down time to numeric time. It is the
* inverse of qse_localtime();
*/
int qse_timelcoal (
const qse_btime_t* bt,

View File

@ -1,5 +1,5 @@
/*
* $Id: macros.h 176 2009-06-02 14:13:50Z hyunghwan.chung $
* $Id: macros.h 186 2009-06-06 13:42:57Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -149,7 +149,7 @@
*/
#define QSE_MT(txt) (txt)
#define QSE_WQ_I(val) (L ## #val)
#define QSE_WQ_I(val) (L ## QSE_MQ_I(val))
#define QSE_WQ(val) QSE_WQ_I(val)
#define QSE_WC(ch) ((qse_wchar_t)L ## ch)
#define QSE_WS(str) ((const qse_wchar_t*)L ## str)

View File

@ -1,5 +1,5 @@
/*
* $Id: types.h 176 2009-06-02 14:13:50Z hyunghwan.chung $
* $Id: types.h 186 2009-06-06 13:42:57Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -394,21 +394,21 @@ struct qse_mmgr_t
* allocate a memory chunk of the size \a n.
* @return a pointer to a memory chunk on success, QSE_NULL on failure.
*/
void* (*alloc) (void* data, qse_size_t n);
void* (*alloc) (void* udd, qse_size_t n);
/**
* resize a memory chunk pointed to by \a ptr to the size \a n.
* @return a pointer to a memory chunk on success, QSE_NULL on failure.
*/
void* (*realloc) (void* data, void* ptr, qse_size_t n);
void* (*realloc) (void* udd, void* ptr, qse_size_t n);
/**
* frees a memory chunk pointed to by \a ptr.
*/
void (*free) (void* data, void* ptr);
void (*free) (void* udd, void* ptr);
/**
* a pointer to user-defined data passed as the first parameter to
* alloc(), realloc(), and free().
*/
void* data;
void* udd;
};
typedef struct qse_mmgr_t qse_mmgr_t;
/******/

View File

@ -1,5 +1,5 @@
/*
* $Id: rex.c 135 2009-05-15 13:31:43Z hyunghwan.chung $
* $Id: rex.c 186 2009-06-06 13:42:57Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -334,7 +334,7 @@ static struct __char_class_t __char_class[] =
{ QSE_T("thai"), 4, cc_isthai },
*/
{ QSE_NULL, 0, QSE_NULL }
{ QSE_NULL, 0, QSE_NULL }
};
qse_rex_t* qse_rex_open (qse_mmgr_t* mmgr, qse_size_t xtn)

View File

@ -1,5 +1,5 @@
/*
* $Id: time.c 76 2009-02-22 14:18:06Z hyunghwan.chung $
* $Id: time.c 186 2009-06-06 13:42:57Z hyunghwan.chung $
*
Copyright 2006-2009 Chung, Hyung-Hwan.
@ -38,12 +38,28 @@
#define EPOCH_DIFF_MSECS (EPOCH_DIFF_SECS*QSE_MSECS_PER_SEC)
#endif
static int mdays[2][QSE_MONS_PER_YEAR] =
static const int mdays[2][QSE_MONS_PER_YEAR] =
{
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
/* number of days from the beginning of the year to the end of of
* a previous month. adjust for leap years in the code. */
static const int mdays_tot[2][QSE_MONS_PER_YEAR] =
{
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 },
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }
};
/* number of days from the beginning of the year to the end of of
* a previous month. adjust for leap years in the code. */
static const int mdays_rtot[2][QSE_MONS_PER_YEAR] =
{
{ 334, 306, 275, 245, 214, 184, 153, 122, 92, 61, 31, 0 },
{ 335, 306, 275, 245, 214, 184, 153, 122, 92, 61, 31, 0 }
};
int qse_gettime (qse_ntime_t* t)
{
#ifdef _WIN32
@ -227,6 +243,8 @@ int qse_localtime (qse_ntime_t nt, qse_btime_t* bt)
int qse_timegm (const qse_btime_t* bt, qse_ntime_t* nt)
{
#if 0
#ifdef _WIN32
/* TODO: verify qse_timegm for WIN32 */
SYSTEMTIME st;
@ -247,6 +265,7 @@ int qse_timegm (const qse_btime_t* bt, qse_ntime_t* nt)
return 0;
#else
/* TODO: qse_timegm - remove dependency on timegm */
struct tm tm;
@ -269,6 +288,51 @@ int qse_timegm (const qse_btime_t* bt, qse_ntime_t* nt)
#endif
#endif
#endif
qse_ntime_t n = 0;
int y = bt->year + QSE_BTIME_YEAR_BASE;
int midx = QSE_IS_LEAPYEAR(y)? 1: 0;
if (y < QSE_EPOCH_YEAR)
{
int x;
for (x = y; x < QSE_EPOCH_YEAR - 1; x++)
n += QSE_DAYS_PER_YEAR(x);
for (x = bt->mon + 1; x < QSE_MONS_PER_YEAR; x++)
n += mdays[midx][x];
n += mdays[midx][bt->mon] - bt->mday;
if (midx == 1) n -= 1;
n *= QSE_HOURS_PER_DAY;
n = (n + QSE_HOURS_PER_DAY - bt->hour - 1) * QSE_MINS_PER_HOUR;
n = (n + QSE_MINS_PER_HOUR - bt->min - 1) * QSE_SECS_PER_MIN;
n = (n + QSE_SECS_PER_MIN - bt->sec) * QSE_MSECS_PER_SEC;
if (bt->msec > 0) n += QSE_MSECS_PER_SEC - bt->msec;
*nt = -n;
}
else
{
int x;
for (x = QSE_EPOCH_YEAR; x < y; x++)
n += QSE_DAYS_PER_YEAR(x);
/*for (x = 0; x < bt->mon; x++) n += mdays[midx][x];*/
n += mdays_tot[midx][bt->mon];
n = (n + bt->mday - 1) * QSE_HOURS_PER_DAY;
n = (n + bt->hour) * QSE_MINS_PER_HOUR;
n = (n + bt->min) * QSE_SECS_PER_MIN;
n = (n + bt->sec) * QSE_MSECS_PER_SEC;
*nt = n + bt->msec;
}
return 0;
}
int qse_timelocal (const qse_btime_t* bt, qse_ntime_t* nt)
@ -290,7 +354,7 @@ int qse_timelocal (const qse_btime_t* bt, qse_ntime_t* nt)
*nt = ((qse_ntime_t)timelocal(&tm)*QSE_MSECS_PER_SEC) + bt->msec;
return 0;
#else
#warning #### timelocal() is not available on this platform ####
*nt = ((qse_ntime_t)mktime(&tm)*QSE_MSECS_PER_SEC) + bt->msec;
return -1;
#endif
}

View File

@ -1762,7 +1762,7 @@ static int write_str_to_file (
QSE_MEMSET (&arg, 0, QSE_SIZEOF(arg));
pair = qse_map_insert (&sed->e.out.files,
path, plen, &arg, QSE_SIZEOF(arg));
(void*)path, plen, &arg, QSE_SIZEOF(arg));
if (pair == QSE_NULL)
{
SETERR0 (sed, QSE_SED_ENOMEM, cmd->lnum);

View File

@ -4,6 +4,7 @@
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#define R(f) \
do { \
@ -63,12 +64,16 @@ static int test1 (void)
nt <= (qse_ntime_t)QSE_TYPE_MAX(int); nt += QSE_SECS_PER_DAY)
{
time_t t = (time_t)nt;
qse_ntime_t qnt = nt * 1000;
struct tm* tm;
if (qnt >= 0) qnt += rand() % 1000;
else qnt -= rand() % 1000;
tm = gmtime (&t);
qse_gmtime (nt * 1000, &bt);
qse_printf (QSE_T(">>> time %lld: "), (long long)nt*1000);
qse_gmtime (qnt, &bt);
qse_printf (QSE_T(">>> time %lld: "), (long long)qnt);
if (tm->tm_year != bt.year ||
tm->tm_mon != bt.mon ||
@ -78,14 +83,28 @@ static int test1 (void)
tm->tm_min != bt.min ||
tm->tm_sec != bt.sec)
{
qse_printf (QSE_T("[ERROR]\n"));
print_time (nt, &bt);
qse_printf (QSE_T("[GMTIME ERROR %lld]\n"), (long long)t);
print_time (qnt, &bt);
qse_printf (QSE_T("-------------------------------\n"));
}
else
{
qse_printf (QSE_T("[OK]\n"));
qse_ntime_t xx;
qse_printf (QSE_T("[GMTIME OK]"));
if (qse_timegm (&bt, &xx) == -1)
{
qse_printf (QSE_T("[TIMEGM FAIL]\n"));
}
else
{
if (xx == qnt)
qse_printf (QSE_T("[TIMEGM OK %d/%d/%d %d:%d:%d]\n"), bt.year + QSE_BTIME_YEAR_BASE, bt.mon + 1, bt.mday, bt.hour, bt.min, bt.sec);
else qse_printf (QSE_T("[TIMEGM ERROR %lld, %d/%d/%d %d:%d:%d]\n"), (long long)xx, bt.year + QSE_BTIME_YEAR_BASE, bt.mon + 1, bt.mday, bt.hour, bt.min, bt.sec);
}
}
}
return 0;