网站首页  |  幼儿教育  |  中小学教育  |  电脑教育  |  英语教育  |  教育论文  |  家长教育 设为首页加入收藏联系投稿 
 位置: 中国教育学习网 > 电脑教育 > 程序设计 > C语言 > 正文

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

字号:   

这里用到时间函数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 小时的时间显示,同时将程序做下改动,就能显示出让自己满意的格式,感兴趣的朋友不防试试...
收藏此文  |  打印  

 
  • 上一篇教程:

  • 下一篇教程:

  •   GoogLe
     
      最新推荐
  • 此栏目下没有推荐教程

  •   最近更新

      GoogLe

     
    Powered by Cn-Education.Com (c) 2005-2008 中国教育学习网 教育网站长QQ交流群60041790
    设为首页  |  加入收藏  |  版权申明  |  广告服务  |  联系我们  |  友情链接  |  网站地图  |  返回顶部 ↑