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

王朝c/c++·作者佚名  2008-06-01
窄屏简体版  字體: |||超大  

这里用到时间函数localtime(),显示本地时间的函数:

具体示例 time.c:

#include<time.h>

#include<stdio.h>

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航