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 ); + //) //}