时间函数对比

#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;
}

你可能感兴趣的:(时间函数对比)