Display time since start

T

timhoagland

Hi,

I have a C++ data acquisition program, running under Linux. The basic
stucture is:

Setup devices
Acquire data for x seconds.
Cleanup.

The problem is that I don't have a way to determine how long the data
acquisition has been running. What I'd like is to print out (using
printf or cout) a small clock that gives the elapsed time since data
acquisition began.

I'd appreciate any help anyone may have.
 
R

Ron AF Greve

Hi,

From the top of my head

#include <time.h>

Lookup
time
localtime
strftime

time_t Time = time( 0 );

const int Size = 400;
char Buffer[ Size ];
strftime( Buffer, Size - 1, "%T", localtime( &Time ) );
cout << Buffer << string( 400, ' ' ) << '\r'; // Keep on one line


Regards, Ron
 
J

James Kanze

I have a C++ data acquisition program, running under Linux. The basic
stucture is:
Setup devices
Acquire data for x seconds.
Cleanup.
The problem is that I don't have a way to determine how long the data
acquisition has been running. What I'd like is to print out (using
printf or cout) a small clock that gives the elapsed time since data
acquisition began.

You can use time() to get the current time, and difftime to get
the difference in seconds between two times. (If you limit
yourself to Posix, Linux, and possibly Windows, you can just
subtract between two time_t to get the difference in seconds.
This isn't portable, however.)
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
Hi,

I have a C++ data acquisition program, running under Linux. The basic
stucture is:

Setup devices
Acquire data for x seconds.
Cleanup.

The problem is that I don't have a way to determine how long the data
acquisition has been running. What I'd like is to print out (using
printf or cout) a small clock that gives the elapsed time since data
acquisition began.

Well, as others said, you can use time.h.
Another solution is to use boost::date_time:

#include "boost/date_time/posix_time/posix_time.hpp"

using namespace boost::posix_time;

// stores date of data acquisition start
ptime start(second_clock::universal_time());
//use microsec_clock::universal_time() for sub second precision

// to printout elapsed time
time_duration elapsed=second_clock::universal_time()-start;
std::cout<<"Elapsed: "<<to_simple_string(elapsed)<<std::endl;

There is also boost/timer.hpp that would be of interest to you but I
have never used it.
http://boost.org/libs/timer/timer.htm

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,298
Messages
2,571,540
Members
48,275
Latest member
tetedenuit01

Latest Threads

Top