cwTime.h/cpp : Added fracSecondsToSpec()

This commit is contained in:
kevin 2024-11-08 11:09:43 -05:00
parent c68f741ccf
commit 1ed6202885
2 changed files with 11 additions and 2 deletions

View File

@ -297,6 +297,13 @@ cw::rc_t cw::time::futureMs( spec_t& ts, unsigned ms )
return rc; return rc;
} }
void cw::time::fracSecondsToSpec( spec_t& ts, double sec )
{
const unsigned long long ns_per_sec = 1000000000;
ts.tv_sec = (unsigned long long)sec;
ts.tv_nsec = (sec - ts.tv_sec) * ns_per_sec;
}
void cw::time::secondsToSpec( spec_t& ts, unsigned sec ) void cw::time::secondsToSpec( spec_t& ts, unsigned sec )
{ {
ts.tv_sec = sec; ts.tv_sec = sec;
@ -305,12 +312,13 @@ void cw::time::secondsToSpec( spec_t& ts, unsigned sec )
double cw::time::specToSeconds( const spec_t& t ) double cw::time::specToSeconds( const spec_t& t )
{ {
const long long ns_per_sec = 1000000000;
spec_t ts = t; spec_t ts = t;
double sec = ts.tv_sec; double sec = ts.tv_sec;
while( ts.tv_nsec >= 1000000000 ) while( ts.tv_nsec >= ns_per_sec )
{ {
sec += 1.0; sec += 1.0;
ts.tv_nsec -= 1000000000; ts.tv_nsec -= ns_per_sec;
} }
return sec + ((double)ts.tv_nsec)/1e9; return sec + ((double)ts.tv_nsec)/1e9;

View File

@ -73,6 +73,7 @@ namespace cw
// 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 fracSecondsToSpec( spec_t& ts, double sec );
void secondsToSpec( spec_t& ts, unsigned sec ); void secondsToSpec( spec_t& ts, unsigned sec );
double specToSeconds( const spec_t& ts ); double specToSeconds( const spec_t& ts );