added some time calc functions

This commit is contained in:
hyung-hwan 2014-08-01 17:17:00 +00:00
parent 019475149c
commit fdc0ce7623
2 changed files with 56 additions and 3 deletions

View File

@ -110,6 +110,11 @@ struct qse_btime_t
/*int offset;*/ /*int offset;*/
}; };
#define qse_cleartime(x) ((x)->tv = (x)->nsec = 0);
#define qse_cmptime(x,y) \
(((x)->sec == (y)->sec)? ((x)->nsec - (y)->nsec): \
((x)->sec - (y)->sec))
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -138,7 +143,7 @@ QSE_EXPORT int qse_gmtime (
); );
/** /**
* The qse_localtime() converts numeric time to broken-down time * The qse_localtime() function converts numeric time to broken-down time
*/ */
QSE_EXPORT int qse_localtime ( QSE_EXPORT int qse_localtime (
const qse_ntime_t* nt, const qse_ntime_t* nt,
@ -146,8 +151,8 @@ QSE_EXPORT int qse_localtime (
); );
/** /**
* The qse_timegm() converts broken-down time to numeric time. It is the * The qse_timegm() function converts broken-down time to numeric time. It is
* inverse of qse_gmtime(). It is useful if the broken-down time is in UTC * the inverse of qse_gmtime(). It is useful if the broken-down time is in UTC
* and the local environment is not. * and the local environment is not.
*/ */
QSE_EXPORT int qse_timegm ( QSE_EXPORT int qse_timegm (
@ -164,6 +169,24 @@ QSE_EXPORT int qse_timelocal (
qse_ntime_t* nt qse_ntime_t* nt
); );
/**
* The qse_addtime() function adds x and y and stores the result in z
*/
QSE_EXPORT void qse_addtime (
const qse_ntime_t* x,
const qse_ntime_t* y,
qse_ntime_t* z
);
/**
* The qse_subtime() function subtract y from x and stores the result in z.
*/
QSE_EXPORT void qse_subtime (
const qse_ntime_t* x,
const qse_ntime_t* y,
qse_ntime_t* z
);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -501,3 +501,33 @@ int qse_timelocal (const qse_btime_t* bt, qse_ntime_t* nt)
nt->nsec = 0; nt->nsec = 0;
return 0; return 0;
} }
void qse_addtime (const qse_ntime_t* x, const qse_ntime_t* y, qse_ntime_t* z)
{
QSE_ASSERT (x->nsec < QSE_NSECS_PER_SEC);
QSE_ASSERT (y->nsec < QSE_NSECS_PER_SEC);
z->sec = x->sec + y->sec;
z->nsec = x->nsec + y->nsec;
if (z->nsec >= QSE_NSECS_PER_SEC)
{
z->sec = z->sec + 1;
z->nsec = z->nsec - QSE_NSECS_PER_SEC;
}
}
void qse_subtime (const qse_ntime_t* x, const qse_ntime_t* y, qse_ntime_t* z)
{
QSE_ASSERT (x->nsec < QSE_NSECS_PER_SEC);
QSE_ASSERT (y->nsec < QSE_NSECS_PER_SEC);
z->sec = x->sec - y->sec;
z->nsec = x->nsec - y->nsec;
if (z->nsec < 0)
{
z->sec = z->sec - 1;
z->nsec = z->nsec + QSE_NSECS_PER_SEC;
}
}