From fdc0ce7623b8456c96147abd8980888ef4e89be8 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 1 Aug 2014 17:17:00 +0000 Subject: [PATCH] added some time calc functions --- qse/include/qse/cmn/time.h | 29 ++++++++++++++++++++++++++--- qse/lib/cmn/time.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/qse/include/qse/cmn/time.h b/qse/include/qse/cmn/time.h index e9e428b7..64174fd4 100644 --- a/qse/include/qse/cmn/time.h +++ b/qse/include/qse/cmn/time.h @@ -110,6 +110,11 @@ struct qse_btime_t /*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 extern "C" { #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 ( 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 - * inverse of qse_gmtime(). It is useful if the broken-down time is in UTC + * The qse_timegm() function 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. */ QSE_EXPORT int qse_timegm ( @@ -164,6 +169,24 @@ QSE_EXPORT int qse_timelocal ( 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 } #endif diff --git a/qse/lib/cmn/time.c b/qse/lib/cmn/time.c index a7c27876..a26ece5e 100644 --- a/qse/lib/cmn/time.c +++ b/qse/lib/cmn/time.c @@ -501,3 +501,33 @@ int qse_timelocal (const qse_btime_t* bt, qse_ntime_t* nt) nt->nsec = 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; + } +}