红联Linux门户
Linux帮助

在 Linux 下用 C 编写显示时间的程序

发布时间:2006-01-20 10:02:57来源:红联作者:天使
这里用到时间函数localtime(),显示本地时间的函数:

具体示例 time.c:

#include
#include

main()
{
struct tm *tm_ptr;
time_t the_time;

(void) time(&the_time);
tm_ptr=localtime(&the_time);

printf("Raw time is %ldn",the_time);
printf("Mytime show:n");
printf("Date:%02d/%02d/%02dn",
tm_ptr->tm_year,tm_ptr->tm_mon+1,tm_ptr->tm_mday);
printf("Time:%02d/%02d/%02dn",
tm_ptr->tm_hour,tm_ptr->tm_min,tm_ptr->tm_sec);
exit(0);
}

运行程序:./time
显示结果:
Raw time is 986544171
Mytime show:
Date:101/04/06
Time:16/02/51

结果显示的第一行显示的数字是 linux 的原始时间表示.还有一个时间函数 gmtime() ,显示的是格林尼治时间,将例子程序的 localtime() 换成 gmtime() ,会有与北京时间相差 8 小时的时间显示,同时将程序做下改动,就能显示出让自己满意的格式,感兴趣的朋友不防试试...
文章评论

共有 0 条评论