diff --git a/cwTime.cpp b/cwTime.cpp index fe7537a..73bbcca 100644 --- a/cwTime.cpp +++ b/cwTime.cpp @@ -212,6 +212,30 @@ void cw::time::subtractMicros( spec_t& ts, unsigned micros ) } +void cw::time::advanceMicros( spec_t& ts, unsigned us ) +{ + if( us > 1000000 ) + { + // strip off whole seconds from usec + unsigned sec = us / 1000000; + + // find the remaining fractional second in microseconds + us = (us - sec*1000000); + + ts.tv_sec += sec; + } + + ts.tv_nsec += us * 1000000000; // convert microseconds to nanoseconds + + // stip off whole seconds from tv_nsec + while( ts.tv_nsec > 1e9 ) + { + ts.tv_nsec -= 1e9; + ts.tv_sec +=1; + } + +} + void cw::time::advanceMs( spec_t& ts, unsigned ms ) { if( ms > 1000 ) diff --git a/cwTime.h b/cwTime.h index 4e2f63f..b6512ab 100644 --- a/cwTime.h +++ b/cwTime.h @@ -65,7 +65,8 @@ namespace cw void subtractMicros( spec_t& ts, unsigned us ); - // Advance 'ts' by 'ms' milliseconds. + // Advance 'ts' by 'us/'ms' microseconds/milliseconds. + void advanceMicros( spec_t& ts, unsigned us ); void advanceMs( spec_t& ts, unsigned ms ); // Advance the current time by 'ms' milliseconds;