From e11e660e527577bf6e13cf7c2e59e8c9de54ef18 Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 17 Dec 2013 08:55:08 -0500 Subject: [PATCH] cmTime.h/c : Added cmTimeDiffMicros(),cmTimeIsLTE(),cmTimeIsGTE(),cmTimeIsEqual(),cmTimeIsZero(),cmTimeSetZero(). --- cmTime.c | 50 +++++++++++++++++++++++++++++++++++++++++++++----- cmTime.h | 17 +++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/cmTime.c b/cmTime.c index 5c7db8b..042e30f 100644 --- a/cmTime.c +++ b/cmTime.c @@ -62,12 +62,52 @@ unsigned cmTimeElapsedMicros( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 ) unsigned cmTimeAbsElapsedMicros( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 ) { - if( t1->tv_sec > t0->tv_sec ) + if( cmTimeIsLTE(t0,t1) ) return cmTimeElapsedMicros(t0,t1); - if( t1->tv_sec == t0->tv_sec ) - if( t1->tv_nsec > t0->tv_nsec ) - return cmTimeElapsedMicros(t0,t1); - return cmTimeElapsedMicros(t1,t0); } + +int cmTimeDiffMicros( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 ) +{ + if( cmTimeIsLTE(t0,t1) ) + return cmTimeElapsedMicros(t0,t1); + + return -((int)cmTimeElapsedMicros(t1,t0)); +} + +bool cmTimeIsLTE( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 ) +{ + if( t0->tv_sec < t1->tv_sec ) + return true; + + if( t0->tv_sec == t1->tv_sec ) + return t0->tv_nsec <= t1->tv_nsec; + + return false; +} + +bool cmTimeIsGTE( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 ) +{ + if( t0->tv_sec > t1->tv_sec ) + return true; + + if( t0->tv_sec == t1->tv_sec ) + return t0->tv_nsec >= t1->tv_nsec; + + return false; +} + +bool cmTimeIsEqual( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 ) +{ return t0->tv_sec==t1->tv_sec && t0->tv_nsec==t1->tv_nsec; } + +bool cmTimeIsZero( const cmTimeSpec_t* t0 ) +{ return t0->tv_sec==0 && t0->tv_nsec==0; } + +void cmTimeSetZero( cmTimeSpec_t* t0 ) +{ + t0->tv_sec = 0; + t0->tv_nsec = 0; +} + + diff --git a/cmTime.h b/cmTime.h index a5aa639..212ba81 100644 --- a/cmTime.h +++ b/cmTime.h @@ -33,6 +33,23 @@ extern "C" { // The function therefore begins by swapping t1 and t0 if t0 is after t1. unsigned cmTimeAbsElapsedMicros( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 ); + + // Same as cmTimeElapsedMicros() but returns a negative value if t0 is after t1. + int cmTimeDiffMicros( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 ); + + + // Returns true if t0 <= t1. + bool cmTimeIsLTE( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 ); + + // Return true if t0 >= t1. + bool cmTimeIsGTE( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 ); + + bool cmTimeIsEqual( const cmTimeSpec_t* t0, const cmTimeSpec_t* t1 ); + + bool cmTimeIsZero( const cmTimeSpec_t* t0 ); + + void cmTimeSetZero( cmTimeSpec_t* t0 ); + //) //}