adding time functions

This commit is contained in:
hyung-hwan 2008-12-18 02:39:15 +00:00
parent d45b0c3127
commit 3d9fbd489f
7 changed files with 195 additions and 50 deletions

View File

@ -2,7 +2,12 @@ items:
NAME
SYNOPSIS
DESCRIPTION
INPUT
INPUTS
OUTPUT
OUTPUTS
RETURN
EXAMPLE
EXAMPLES
NOTES
BUGS
@ -13,7 +18,12 @@ item order:
NAME
SYNOPSIS
DESCRIPTION
INPUT
INTPUTS
OUTPUT
OUTPUTS
RETURN
EXAMPLE
EXAMPLES
NOTES
BUGS

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.h 499 2008-12-16 09:42:48Z baconevi $
* $Id: awk.h 501 2008-12-17 08:39:15Z baconevi $
*
* {License}
*/
@ -1160,15 +1160,13 @@ ase_char_t* ase_awk_valtostr (
*
* DESCRIPTION
* The ase_awk_valtonum() function converts a value to a number.
* The converted value is stored into the variable pointed to by
* either l or r depending on the type of the number. If the value
* is converted to a long number, the function returns 0 and l is
* set with the converted number. If the value is converted to a real number,
* the function returns 1 and r is set with a real number.
* If the value is converted to a long number, it is stored in the memory
* pointed to by l and 0 is returned. If the value is converted to a real
* number, it is stored in the memory pointed to by r and 1 is returned.
*
* RETURN
* The ase_awk_valtonum() function returns -1 on error, 0 if the converted
* value is a long number and 1 if it is a real number.
* number is a long number and 1 if it is a real number.
*
* EXAMPLES
* ase_long_t l;

View File

@ -8,37 +8,124 @@
#include <ase/types.h>
#include <ase/macros.h>
#define ASE_EPOCH_YEAR ((ase_time_t)1970)
#define ASE_EPOCH_MON ((ase_time_t)1)
#define ASE_EPOCH_DAY ((ase_time_t)1)
#define ASE_EPOCH_WDAY ((ase_time_t)4)
#define ASE_EPOCH_YEAR ((ase_ntime_t)1970)
#define ASE_EPOCH_MON ((ase_ntime_t)1)
#define ASE_EPOCH_DAY ((ase_ntime_t)1)
#define ASE_EPOCH_WDAY ((ase_ntime_t)4)
#define ASE_DAY_IN_WEEK ((ase_time_t)7)
#define ASE_MON_IN_YEAR ((ase_time_t)12)
#define ASE_HOUR_IN_DAY ((ase_time_t)24)
#define ASE_MIN_IN_HOUR ((ase_time_t)60)
#define ASE_DAY_IN_WEEK ((ase_ntime_t)7)
#define ASE_MON_IN_YEAR ((ase_ntime_t)12)
#define ASE_HOUR_IN_DAY ((ase_ntime_t)24)
#define ASE_MIN_IN_HOUR ((ase_ntime_t)60)
#define ASE_MIN_IN_DAY (ASE_MIN_IN_HOUR * ASE_HOUR_IN_DAY)
#define ASE_SEC_IN_MIN ((ase_time_t)60)
#define ASE_SEC_IN_MIN ((ase_ntime_t)60)
#define ASE_SEC_IN_HOUR (ASE_SEC_IN_MIN * ASE_MIN_IN_HOUR)
#define ASE_SEC_IN_DAY (ASE_SEC_IN_MIN * ASE_MIN_IN_DAY)
#define ASE_MSEC_IN_SEC ((ase_time_t)1000)
#define ASE_MSEC_IN_SEC ((ase_ntime_t)1000)
#define ASE_MSEC_IN_MIN (ASE_MSEC_IN_SEC * ASE_SEC_IN_MIN)
#define ASE_MSEC_IN_HOUR (ASE_MSEC_IN_SEC * ASE_SEC_IN_HOUR)
#define ASE_MSEC_IN_DAY (ASE_MSEC_IN_SEC * ASE_SEC_IN_DAY)
#define ASE_USEC_IN_MSEC ((ase_time_t)1000)
#define ASE_NSEC_IN_USEC ((ase_time_t)1000)
#define ASE_USEC_IN_SEC ((ase_time_t)ASE_USEC_IN_MSEC * ASE_MSEC_IN_SEC)
#define ASE_USEC_IN_MSEC ((ase_ntime_t)1000)
#define ASE_NSEC_IN_USEC ((ase_ntime_t)1000)
#define ASE_USEC_IN_SEC ((ase_ntime_t)ASE_USEC_IN_MSEC * ASE_MSEC_IN_SEC)
#define ASE_IS_LEAPYEAR(year) (!((year)%4) && (((year)%100) || !((year)%400)))
#define ASE_DAY_IN_YEAR(year) (ASE_IS_LEAPYEAR(year)? 366: 365)
/* number of milliseconds since the Epoch (00:00:00 UTC, Jan 1, 1970) */
typedef ase_long_t ase_time_t;
typedef ase_long_t ase_ntime_t;
typedef struct ase_btime_t ase_btime_t;
struct ase_btime_t
{
int sec;
int min;
int hour;
int mday;
int mon;
int year;
int wday;
int yday;
int isdst;
};
#ifdef __cplusplus
extern "C" {
#endif
int ase_gettime (ase_time_t* t);
int ase_settime (ase_time_t t);
/****f* ase.cmn/ase_gettime
* NAME
* ase_gettime - get the current time
*
* SYNPOSIS
*/
int ase_gettime (
ase_ntime_t* nt
);
/******/
/****f* ase.cmn/ase_settime
* NAME
* ase_settime - set the current time
*
* SYNOPSIS
*/
int ase_settime (
ase_ntime_t nt
);
/******/
/****f* ase.cmn/ase_gmtime
* NAME
* ase_gmtime - convert numeric time to broken-down time
*
* SYNOPSIS
*/
void ase_gmtime (
ase_ntime_t nt,
ase_btime_t* bt
);
/******/
/****f* ase.cmn/ase_localtime
* NAME
* ase_localtime - convert numeric time to broken-down time
*
* SYNOPSIS
*/
int ase_localtime (
ase_ntime_t nt,
ase_btime_t* bt
);
/******/
/****f* ase.cmn/ase_mktime
* NAME
* ase_mktime - convert broken-down time to numeric time
*
* SYNOPSIS
*/
int ase_mktime (
const ase_btime_t* bt,
ase_ntime_t* nt
);
/******/
/****f* ase.cmn/ase_strftime
* NAME
* ase_strftime - format time
*
* SYNOPSIS
*/
ase_size_t ase_strftime (
ase_char_t* buf,
ase_size_t size,
const ase_char_t* fmt,
ase_btime_t* bt
);
/******/
#ifdef __cplusplus
}

View File

@ -1,5 +1,5 @@
/*
* $Id: StdAwk.cpp 499 2008-12-16 09:42:48Z baconevi $
* $Id: StdAwk.cpp 501 2008-12-17 08:39:15Z baconevi $
*
* {License}
*/
@ -67,7 +67,7 @@ int StdAwk::open ()
int StdAwk::run (const char_t* main, const char_t** args, size_t nargs)
{
ase_time_t now;
ase_ntime_t now;
if (ase_gettime(&now) == -1) this->seed = 0;
else this->seed = (unsigned int)now;
@ -224,7 +224,7 @@ int StdAwk::srand (Run& run, Return& ret, const Argument* args, size_t nargs,
if (nargs == 0)
{
ase_time_t now;
ase_ntime_t now;
if (ase_gettime (&now) == -1)
this->seed = (unsigned int)now;
@ -249,9 +249,12 @@ int StdAwk::srand (Run& run, Return& ret, const Argument* args, size_t nargs,
int StdAwk::systime (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len)
{
ase_time_t now;
if (ase_gettime (&now) == -1) now = 0;
return ret.set ((long_t)now / ASE_MSEC_IN_SEC);
ase_ntime_t now;
if (ase_gettime(&now) == -1)
return ret.set (ASE_TYPE_MIN(long_t));
else
return ret.set ((long_t)now / ASE_MSEC_IN_SEC);
}
int StdAwk::strftime (Run& run, Return& ret, const Argument* args, size_t nargs,

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.c 496 2008-12-15 09:56:48Z baconevi $
* $Id: awk.c 501 2008-12-17 08:39:15Z baconevi $
*
* {License}
*/
@ -454,6 +454,7 @@ int ase_awk_setword (ase_awk_t* awk,
return 0;
}
/* TODO: XXXX */
int ase_awk_setrexfns (ase_awk_t* awk, ase_awk_rexfns_t* rexfns)
{
if (rexfns->build == ASE_NULL ||

View File

@ -708,7 +708,7 @@ int ase_awk_runsimple (ase_awk_t* awk, ase_char_t** icf, ase_awk_runcbs_t* cbs)
ase_awk_runios_t ios;
runio_data_t rd;
rxtn_t rxtn;
ase_time_t now;
ase_ntime_t now;
rd.ic.files = icf;
rd.ic.index = 0;
@ -1033,7 +1033,7 @@ static int bfn_srand (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
}
else
{
ase_time_t now;
ase_ntime_t now;
if (ase_gettime(&now) == -1) rxtn->seed >>= 1;
else rxtn->seed = (unsigned int)now;
@ -1055,12 +1055,14 @@ static int bfn_srand (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
static int bfn_systime (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
{
ase_awk_val_t* r;
ase_time_t now;
ase_ntime_t now;
int n;
if (ase_gettime(&now) == -1) now = 0;
if (ase_gettime(&now) == -1)
r = ase_awk_makeintval (run, ASE_TYPE_MIN(ase_long_t));
else
r = ase_awk_makeintval (run, now / ASE_MSEC_IN_SEC);
r = ase_awk_makeintval (run, now / ASE_MSEC_IN_SEC);
if (r == ASE_NULL)
{
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);

View File

@ -11,23 +11,28 @@
#include <time.h>
#endif
#ifdef _WIN32
#define WIN_EPOCH_YEAR ((ase_time_t)1601)
#define WIN_EPOCH_MON ((ase_time_t)1)
#define WIN_EPOCH_DAY ((ase_time_t)1)
#define EPOCH_DIFF_YEARS (ASE_EPOCH_YEAR - WIN_EPOCH_YEAR)
#define EPOCH_DIFF_DAYS (EPOCH_DIFF_YEARS * 365 + EPOCH_DIFF_YEARS / 4 - 3)
#define EPOCH_DIFF_SECS (EPOCH_DIFF_DAYS * 24 * 60 * 60)
#define EPOCH_DIFF_MSECS (EPOCH_DIFF_SECS * ASE_MSEC_IN_SEC)
#endif
#if defined(ASE_USE_SYSCALL) && defined(HAVE_SYS_SYSCALL_H)
#include <sys/syscall.h>
#endif
int ase_gettime (ase_time_t* t)
#ifdef _WIN32
#define WIN_EPOCH_YEAR ((ase_ntime_t)1601)
#define WIN_EPOCH_MON ((ase_ntime_t)1)
#define WIN_EPOCH_DAY ((ase_ntime_t)1)
#define EPOCH_DIFF_YEARS (ASE_EPOCH_YEAR-WIN_EPOCH_YEAR)
#define EPOCH_DIFF_DAYS (EPOCH_DIFF_YEARS*365+EPOCH_DIFF_YEARS/4-3)
#define EPOCH_DIFF_SECS (EPOCH_DIFF_DAYS*24*60*60)
#define EPOCH_DIFF_MSECS (EPOCH_DIFF_SECS*ASE_MSEC_IN_SEC)
#endif
static int ytab[2][12] =
{
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
int ase_gettime (ase_ntime_t* t)
{
#ifdef _WIN32
SYSTEMTIME st;
@ -40,7 +45,7 @@ int ase_gettime (ase_time_t* t)
GetSystemTime (&st);
if (SystemTimeToFileTime (&st, &ft) == FALSE) return -1;
*t = ((ase_time_t)(*((ase_int64_t*)&ft)) / (10 * 1000));
*t = ((ase_ntime_t)(*((ase_int64_t*)&ft)) / (10 * 1000));
*t -= EPOCH_DIFF_MSECS;
return 0;
#else
@ -59,7 +64,7 @@ int ase_gettime (ase_time_t* t)
#endif
}
int ase_settime (ase_time_t t)
int ase_settime (ase_ntime_t t)
{
#ifdef _WIN32
FILETIME ft;
@ -86,3 +91,42 @@ int ase_settime (ase_time_t t)
#endif
}
void ase_gmtime (ase_ntime_t nt, ase_btime_t* bt)
{
/* code based on minix 2.0 src/lib/ansi/gmtime.c */
ase_ntime_t days; /* total days */
ase_ntime_t secs; /* number of seconds in the fractional days */
ase_ntime_t time; /* total seconds */
int year = ASE_EPOCH_YEAR;
time = nt / ASE_MSEC_IN_SEC;
days = (unsigned long)time / ASE_SEC_IN_DAY;
secs = (unsigned long)time % ASE_SEC_IN_DAY;
bt->sec = secs % ASE_SEC_IN_MIN;
bt->min = (secs % ASE_SEC_IN_HOUR) / ASE_SEC_IN_MIN;
bt->hour = secs / ASE_SEC_IN_HOUR;
bt->wday = (days + 4) % ASE_DAY_IN_WEEK;
while (days >= ASE_DAY_IN_YEAR(year))
{
days -= ASE_DAY_IN_YEAR(year);
year++;
}
bt->year = year - 1900;
bt->yday = days;
bt->mon = 0;
while (days >= ytab[ASE_IS_LEAPYEAR(year)][bt->mon])
{
days -= ytab[ASE_IS_LEAPYEAR(year)][bt->mon];
bt->mon++;
}
bt->mday = days + 1;
bt->isdst = 0;
}