Linux C 時間操作實例

格式化時間字符串:

void get_time_str() {

time_t nowtime;

char time_string[16];


nowtime = time(NULL);

strftime(time_string, 16, "%b %d %H:%M:%S", localtime(&nowtime));


printf("Now time is %s\n", time_string);

}

取得時間差:

#define TV_SUBSTRACT(t2, t1) \

(double) (t2).tv_sec + (t2).tv_usec / 1000000.0 - \

(double) (t1).tv.sec + (t1).tv_usec / 1000000.0


void get_time_elapsed() {

struct timeval tv1, tv2;

int i;

gettimeofday(&tv1, NULL);

for (i=1; i< 100000; i++) 

;

gettimeofday(&tv2, NULL);

printf("Elaspsed time is %.5f\n", TV_SUBSTRACT(tv2, tv1);

return;

}

elapsed


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章