方法一:


25、记录时间的几种方法_#include25、记录时间的几种方法_it技术_02View Code


#include <time.h>
#include <stdio.h>
void main( void )
{
time_t ltime;
time( &ltime );
printf( "The time is %s\n", ctime(&ltime ) );
}


方法二:



struct timeval start;
struct timeval end;
float time_use = 0;
gettimeofday(&start,NULL);
//do some operation
gettimeofday(&end,NULL);
time_use = end.tv_sec - start.tv_sec; //in sec


方法三:



struct tm *local;
time_t t;
time(&t);
local=localtime(&t);
char ctime[30];
memset(ctime, 0, 30);
sprintf(time, "[%d:%d:%d]",local->tm_hour, local->tm_min, local->tm_sec);
cout << time;