龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

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

时间:2009-12-22 15:42来源:未知 作者:admin 点击:
分享到:
这里用到时间函数localtime(),显示本地时间的函数: 具体示例 time.c: #include #include main() { strUCt tm *tm_ptr; time_t the_time; (void) time( tm_ptr=localtime( printf("Raw time is %ldn",the_time); printf("Mytime show:n")

  这里用到时间函数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 小时的时间显示,同时将程序做下改动,就能显示出让自己满足的格式,感爱好的朋友不防试试...

  

精彩图集

赞助商链接