现在的位置: 首页 > 操作系统 > 正文

linux 获取系统当前时间

2018年10月07日 操作系统 ⁄ 共 658字 ⁄ 字号 评论关闭
代码:
#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

抱歉!评论已关闭.