時間函式對比

Joe_vv發表於2014-08-11
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h> 

// 時間函式 time() 可用精度更高的函式 gettimeofday() 來代替!

int main()
{
	time_t timer = time(NULL);
	struct timeval tv;
	struct timeval tv2;
	struct timezone tz;
	gettimeofday (&tv , NULL);
	
	printf("===> timer1: %ld\n", timer);
	printf("===> tv_sec1: %ld\n", tv.tv_sec);
	printf("===> tv_usec1: %ld\n", tv.tv_usec);
	
	
	sleep(3);
	
	gettimeofday (&tv2 , &tz);
	timer = time(NULL);
	printf("===> timer2: %ld\n", timer);
	printf("===> tv_sec2: %ld\n", tv2.tv_sec);
	printf("===> tv_usec2: %ld\n", tv2.tv_usec);
	printf("======> usec go: %ld\n", tv2.tv_usec - tv.tv_usec);
	printf("===> tz_minuteswest: %d\n", tz.tz_minuteswest);
	printf("===> tz_dsttime: %d\n", tz.tz_dsttime);
	return 0;
}



博主所有文章已轉自私人部落格 Joe 的個人部落格,謝謝關注!

相關文章