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

原创|其它|编辑:郝浩|2009-09-17 15:01:11.000|阅读 409 次

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

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

  获取系统时间和日期

    #include "stdio.h"
  #include "time.h"
  main()
  {
  time_t tm;
  time(&tm);
  printf("\nThe current time and date is :%s",ctime(&tm));
  /*ctime()将获取的时间转化成字符串*/
  getch();
  }
  让程序延时指定的时间段
  #include "stdio.h"
  #include "dos.h"
  main()
  {
  printf("\nDelay 4 seconds by delay function");
  delay(4000);
  printf("\n4 seconds delay is over");
  getch()
  }

  在单片机和嵌入式中使用较多,延时的方法很多,通常用for循环,而使用汇编写的延时函数要精确很多,所以实际应用中通常采用汇编编写延时函数。

  比较两个时间

 #include "stdio.h"
  #include "time.h"
  main()
  {
  time_t start,end;
  time(&start);
  do
  {
  time(&end);
  }while(difftime(start,end)<5.00);
  printf("\nThe programme has delay 5 seconds!");
  getch();
  }
  /*比较大于1秒的时间*/
  #include "stdio.h"
  #include "dos.h"
  #include "time.h"
  main()
  {
  clock_t start,end;
  int i;
  start=clock();
  for(i=0;i<1000;i++){;}
  end=clock();
  printf("\n%f seconds passed",(end-start)/CLK_TCK);
  getch();
  } /*用于实现1秒以内的时间比较*/


标签:

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

文章转载自:IT专家网

为你推荐

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


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP