linux 获取系统当前时间

代码:
#include<unistd.h>
#include<time.h>
#include<sys/time.h>

int main()
{
	int n = time(0);
	printf("time:%d\n",n);

	struct timeval tv;
	gettimeofday(&tv,NULL);
	printf("gettimeofday:%u,%u\n",tv.tv_sec,tv.tv_usec);

	time_t t;
	time(&t);
	struct tm *pTime;
	pTime = gmtime(&t);
	printf("gmtime:%04d-%02d-%02d,%02d:%02d:%02d\n",pTime->tm_year,pTime->tm_mon,pTime->tm_mday,pTime->tm_hour,pTime->tm_min,pTime->tm_sec);

	pTime = localtime(&t);
	printf("localtime:%04d-%02d-%02d,%02d:%02d:%02d\n",pTime->tm_year+1900,pTime->tm_mon+1,pTime->tm_mday,pTime->tm_hour,pTime->tm_min,pTime->tm_sec);

	return 0;
}
运行结果:
time:1350457789
gettimeofday:1350457789,751874
gmtime:0112-09-17,07:09:49
localtime:2012-10-17,15:09:49


你可能感兴趣的:(linux,struct,null)