cwTime.h,cpp : Added seconds,milli,microToSpec() and subtractMicros()

This commit is contained in:
kevin 2021-10-11 12:22:27 -04:00
parent e128a1dd38
commit 2d54982009
2 changed files with 70 additions and 0 deletions

View File

@ -142,6 +142,35 @@ cw::rc_t cw::time::now( spec_t& ts )
return rc; return rc;
} }
void cw::time::subtractMicros( spec_t& ts, unsigned micros )
{
unsigned rem_us = micros % 1000000; // fractional seconds in microseconds
unsigned rem_ns = rem_us * 1000; // fractional seconds in nanoseconds
// if the fractional micros is greater than the fractional nano's
if( rem_ns > ts.tv_nsec )
{
// subtract the fractional nano's from the fractional micros
// (this sets the fractional nano's to 0)
rem_ns -= ts.tv_nsec;
// convert the remaining fractional micros to the fractional nano's
ts.tv_nsec = 1000000000 - rem_ns;
// subtract the carry
ts.tv_sec -= 1;
}
else
{
ts.tv_nsec -= rem_ns;
}
assert( ts.tv_sec > micros / 1000000 );
ts.tv_sec -= micros / 1000000;
}
void cw::time::advanceMs( spec_t& ts, unsigned ms ) void cw::time::advanceMs( spec_t& ts, unsigned ms )
{ {
@ -171,6 +200,29 @@ cw::rc_t cw::time::futureMs( spec_t& ts, unsigned ms )
return rc; return rc;
} }
void cw::time::secondsToSpec( spec_t& ts, unsigned sec )
{
ts.tv_sec = sec;
ts.tv_nsec = 0;
}
void cw::time::millisecondsToSpec( spec_t& ts, unsigned ms )
{
unsigned sec = ms/1000;
unsigned ns = (ms - (sec*1000)) * 1000000;
ts.tv_sec = sec;
ts.tv_nsec = ns;
}
void cw::time::microsecondsToSpec( spec_t& ts, unsigned us )
{
unsigned sec = us/1000000;
unsigned ns = (us - (sec*1000000)) * 1000;
ts.tv_sec = sec;
ts.tv_nsec = ns;
}
cw::rc_t cw::time::test() cw::rc_t cw::time::test()
{ {
@ -185,6 +237,18 @@ cw::rc_t cw::time::test()
printf("dMs:%i : GTE:%i LTE:%i\n",dMs, isGTE(t0,t1), isLTE(t0,t1) ); printf("dMs:%i : GTE:%i LTE:%i\n",dMs, isGTE(t0,t1), isLTE(t0,t1) );
microsecondsToSpec( t0, 2500000 ); // 2.5 seconds
printf("%li %li\n",t0.tv_sec,t0.tv_nsec);
subtractMicros( t0, 750000 ); // subtract .75 seconds
printf("%li %li\n",t0.tv_sec,t0.tv_nsec);
subtractMicros( t0, 500000 ); // subtract .5 seconds
printf("%li %li\n",t0.tv_sec,t0.tv_nsec);
return kOkRC; return kOkRC;
} }

View File

@ -53,12 +53,18 @@ namespace cw
rc_t now( spec_t& ts ); rc_t now( spec_t& ts );
void subtractMicros( spec_t& ts, unsigned us );
// Advance 'ts' by 'ms' milliseconds. // Advance 'ts' by 'ms' milliseconds.
void advanceMs( spec_t& ts, unsigned ms ); void advanceMs( spec_t& ts, unsigned ms );
// Advance the current time by 'ms' milliseconds; // Advance the current time by 'ms' milliseconds;
rc_t futureMs( spec_t& ts, unsigned ms ); rc_t futureMs( spec_t& ts, unsigned ms );
void secondsToSpec( spec_t& ts, unsigned sec );
void millisecondsToSpec( spec_t& ts, unsigned ms );
void microsecondsToSpec( spec_t& ts, unsigned us );
rc_t test(); rc_t test();
//) //)