Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get time with more precision

Status
Not open for further replies.

Ezequiel

Programmer
Apr 4, 2001
30
CA
Hi!
I need to get the time with more precision than seconds from the machine that is running my c++ program. I would need at least miliseconds.

I'm using GNU C++ under Linux.

Any ideas?

Thanks!
Eze. ---
Ezequiel Glinsky
eze@bumeran.com
Buenos Aires, Argentina
 
I remember something called nanosleep which would sleep for nano-seconds. I belive it came with its own time structure as well. Check the help files on nanosleep or grep for it in your include directory.

Matt
 
I couldn't find any related time function to get the current time :-(
Eze. ---
Ezequiel Glinsky
eze@bumeran.com
Buenos Aires, Argentina
 
Try using
Code:
clock()
. Man clock should give you some clues.
 

Try clock() / ftime()

Anand
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
// Here is a snippet from a class static method of mine.
// This will give you current date/time to the millisecond.

#include <unistd.h>
#include <strstream>
#include <iomanip>
#include <cstring>

using namespace std;

// this const was part of a class...
const int getTimeOfDayBufferLength = 27;

// this function was a static method in that class...
char* myGetTimeOfDay(char* buffer, int bufferLength)
{
if (bufferLength<getTimeOfDayBufferLength)
{
buffer[0] = '\0';
}
else
{
timeval tv;
gettimeofday( &tv, NULL );
strftime( buffer, 20,
&quot;%Y/%m/%d %H:%M:%S&quot;, localtime(&tv.tv_sec) );
ostrstream ostr;
ostr << ':' << setfill('0') << setw(6) << tv.tv_usec
<< ends;
char* sp = ostr.str();
strcat(buffer, sp);
delete [] sp;
}
return buffer;
}
 
RTlinux supports the POSIX call clock_gettime(). This will give you the time in very fine resloution. May not be worth the hassle of installing RTlinux though.

Brudnakm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top