C语言开发基础:时间和日期操作(三)

原创|其它|编辑:郝浩|2009-09-17 15:04:28.000|阅读 514 次

概述:本文介绍系统调用之时间和日期操作的方法。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

   获取当地时间

   #include "stdio.h"
  #include "bios.h"
  #include "time.h"
  main()
  {
  struct tm *tmpointer;
  time_t secs;
  time(&secs);
  tmpointer=localtime(&secs); /*可以用asctime(tmpointer)来转换成字符串形式*/
  printf("\ndate:%d-%d-%d;time:%d:%d:%d",tmpointer->tm_mon+1,tmpointer->tm_mday,
  tmpointer->tm_year+1900,tmpointer->tm_hour,tmpointer->tm_min,tmpointer
  ->tm_sec);
  getch();
  }

  获取格林威治平时函数

 struct tm *gmtime(const time_t *timer);用法同上

  获取DOS系统时间

    #include "stdio.h"
  #include "dos.h"
  main()
  {
  struct time curtm;
  gettime(&curtm);
  printf("\nCurrent time%02d:%02d:%02d.%d",curtm.ti_hour,curtm.ti_min,curtm.ti_sec,curtm.ti_hund);
  getch(); /*ti_hund用来获取时间秒钟的小树点*/
  }

  另外与其它函数的搭配使用

    #include "stdio.h"
  #include "dos.h"
  main()
  {
  struct time *timer; /*这里定义指针类型,需要分配空间*/
  timer=(struct time *)malloc(sizeof(struct time));
  gettime(timer);
  printf("\nCurrent seconds :%02d",timer->ti_sec);
  sleep(4); /*程序停下4秒*/
  gettime(timer);
  printf("\nCurrent seconds :%02",timer->ti_sec);
  getch();
  }

  获取DOS系统日期

 #include "stdio.h"
  #include "dos.h"
  main()
  {
  struct date time;
  getdate(&time);
  printf("\nCurrent date %02d-%02d-%04d",time.da_mon,time.da_day,time.da_year);
  }


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:IT专家网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP