how to print time in micro seconds using ctime() function?
HI how to print time in macro second also with the use of following function in c++, I am using,
time_t rawtime;
time(&rawtime);
std::cout<<"Cuuent TIme is :: "<< ctime(&rawtime)<< std::endl;
Above code will give me only hours, minutes and second. I also need 开发者_运维百科the micro seconds. Any suggestion how to get time with micro seconds also?
I used following code to get time in H,M,S,mS:
char timeBuf [256];
struct timeval tv;
struct timezone tz;
struct tm *tm;
gettimeofday(&tv, &tz);
tm=localtime(&tv.tv_sec);
sprintf (timeBuf, "%02d:%02d:%02d:%03ld",tm->tm_hour, tm->tm_min, tm->tm_sec, (tv.tv_usec/1000) );
BSalunke, look at this piece of code. Includes the uSeconds and the time_t structure you were using.
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main( int argc, char **argv )
{
struct timeval timeValue;
long uSeconds;
if ( gettimeofday( &timeValue, NULL ) == 0 )
{
// This is what you have:
time_t time = timeValue.tv_sec;
// This is what you are looking for:
printf( "Current milliseconds: %ld\n", timeValue.tv_usec );
return EXIT_SUCCESS;
}
else
return EXIT_FAILURE;
}
Two options:
gettimeofday()
clock(...)
精彩评论