`
feiliboos
  • 浏览: 666007 次
文章分类
社区版块
存档分类
最新评论

time.h时间函数

 
阅读更多

time.h

目录

代码示例
从系统时钟获取时间方式
time函数介绍
  1. 1、函数名称: localtime
  2. 2、函数名称: asctime
  3. 3、函数名称: ctime
  4. 4、函数名称: difftime
  5. 5、函数名称: gmtime
  6. 6、函数名称: time
  7. 7、函数名称: tzset
代码示例
从系统时钟获取时间方式
time函数介绍
  1. 1、函数名称: localtime
  2. 2、函数名称: asctime
  3. 3、函数名称: ctime
  4. 4、函数名称: difftime
  5. 5、函数名称: gmtime
  6. 6、函数名称: time
  7. 7、函数名称: tzset
展开
  time.h是C/C++中的日期和时间头文件。

编辑本段代码示例

  # include <stdio.h>
  #include <time.h>
  int main(void)
  {
  time_t timer =time(NULL);
  printf("ctime is %s\n",ctime(&timer)); //得到日历时间
  return 0;
  }

编辑本段从系统时钟获取时间方式

  time_t time(time_t* timer)
  得到从标准计时点(一般是1970年1月1日午夜)到当前时间的秒数。
  clock_t clock(void)
  得到从程序启动到此次函数调用时累计的毫秒数。

编辑本段time函数介绍

1、函数名称: localtime

  函数原型: struct tm *localtime(const time_t *timer)
  函数功能: 返回一个以tm结构表达的机器时间信息
  函数返回: 以tm结构表达的时间,结构tm定义如下:
  struct tm{
  int tm_sec;
  int tm_min;
  int tm_hour;
  int tm_mday;
  int tm_mon;
  int tm_year;
  int tm_wday;
  int tm_yday;
  int tm_isdst;
  };
  参数说明: timer-使用time()函数获得的机器时间
  所属文件: <time.h>
  #include <time.h>
  #include <stdio.h>
  #include <dos.h>
  int main()
  {
  time_t timer;
  struct tm *tblock;
  timer=time(NULL);
  tblock=localtime(&timer);
  printf("Local time is: %s",asctime(tblock));
  return 0;
  }

2、函数名称: asctime

  函数原型: char* asctime(struct tm * ptr)
  函数功能: 得到机器时间(日期时间转换为ASCII码)
  函数返回: 返回的时间字符串格式为:星期,月,日,小时:分:秒,年
  参数说明: 结构指针ptr应通过函数localtime()和gmtime()得到

3、函数名称: ctime

  函数原型: char *ctime(long time)
  函数功能: 得到日历时间
  函数返回: 返回字符串格式:星期,月,日,小时:分:秒,年
  参数说明: time-该参数应由函数time获得
  所属文件: <time.h>
  #include <stdio.h>
  #include <time.h>
  int main()
  {
  time_t t;
  time(&t);
  printf("Today's date and time: %s",ctime(&t));
  return 0;
  }

4、函数名称: difftime

  函数原型: double difftime(time_t time2, time_t time1)
  函数功能: 得到两次机器时间差,单位为秒
  函数返回: 时间差,单位为秒
  参数说明: time1-机器时间一,time2-机器时间二.该参数应使用time函数获得
  所属文件: <time.h>
  #include <time.h>
  #include <stdio.h>
  #include <dos.h>
  #include <conio.h>
  int main()
  {
  time_t first, second;
  clrscr();
  first=time(NULL);
  delay(2000);
  second=time(NULL);
  printf("The difference is: %fseconds",difftime(second,first));
  getch();
  return 0;
  }

5、函数名称: gmtime

  函数原型: struct tm *gmtime(time_t *time)
  函数功能: 得到以结构tm表示的时间信息
  函数返回: 以结构tm表示的时间信息指针
  参数说明: time-用函数time()得到的时间信息
  所属文件: <time.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <time.h>
  #include <dos.h>
  char *tzstr="TZ=PST8PDT";
  int main()
  {
  time_t t;
  struct tm *gmt, *area;
  putenv(tzstr);
  tzset();
  t=time(NULL);
  area=localtime(&t);
  printf("Local time is:%s", asctime(area));
  gmt=gmtime(&t);
  printf("GMT is:%s", asctime(gmt));
  return 0;
  }

6、函数名称: time

  函数原型: time_t time(time_t *timer)
  函数功能: 得到机器的日历时间或者设置日历时间
  函数返回: 机器日历时间
  参数说明: timer=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型
  所属文件: <time.h>
  #include <time.h>
  #include <stdio.h>
  #include <dos.h>
  int main()
  {
  time_t t;
  t=time();
  printf("The number of seconds since January 1,1970 is %ld",t);
  return 0;
  }

7、函数名称: tzset

  函数原型: void tzset(void)
  函数功能: UNIX兼容函数,用于得到时区,在DOS环境下无用途
  函数返回:
  参数说明:
  所属文件: <time.h>
  #include <time.h>
  #include <stdlib.h>
  #include <stdio.h>
  int main()
  {
  time_t td;
  putenv("TZ=PST8PDT");
  tzset();
  time(&td);
  printf("Current time=%s",asctime(localtime(&td)));
  return 0;
  }


分享到:
评论

相关推荐

    STM32使用time.h库函数操作时间日期

    iar,用STM32实现time.h中时间日期库函数,完全的标准时间库,有time.h实现统一强大的日期计算

    C 标准库 – time.h

    time.h 头文件定义了四个变量类型、两个宏和各种操作日期和时间的函数。 库变量 下面是头文件 time.h 中定义的变量类型: 序号 变量 & 描述 1 size_t 是无符号整数类型,它是 sizeof 关键字的结果。 2 clock...

    time.h的使用 时间函数的使用

    time.h 的使用 time.h 的使用 time.h 的使用 time.h 的使用 time.h 的使用

    C语言运行库 _mingw.h和time.h

    C语言运行库 _mingw.h和time.h。time.h包含了C语言的时间函数,需要前者支持

    C语言time.h从头学

    本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的数据结构和函数,并对计时、时间的获取、时间的计算和...本文还通过大量的实例向你展示了time.h头文件中声明的各种函数和数据结构的详细使用方法。

    get internet time.zip

    C++获得网络时间函数库,包含get_internet_time.cpp,get_internet_time.h两个文件,以及调用示例

    C语言函数库详解.doc

    C语言函数库详解.doc C语言函数库,包含 C 标准库、IO 函数、字符处理函数、字符串函数、数学函数、时间和日期函数、其它函数等数百个常用的C语言函数,文档中对常用函数库的函数原型...13. &lt;time.h&gt;:日期与时间函数

    C语言函数大全

    目 录 1.字符函数,所在函数库为ctype.h……………………………………………………………………2 ...9. 时间日期函数,所在函数库为time.h、dos.h………………………………………………………19

    10.第十章 函数.txt

    #include&lt;time.h&gt; /*showTime函数返回1979年1月1日0时0分0秒到当前的秒数*/ long showTime() { long lct; lct=time(0); /*time(0)返回1979年1月1日0时0分0秒到当前的秒数*/ return lct; } int main() { ...

    获取时间的接口函数,包含.c及.h

    这是已经封装好的获取时间接口函数,可以以ms,us获取时间

    C标准库函数集.rar

    本文包括大部分C标准库函数,但没有列出一些用途有限的函数以及某些可以简单的从其他函数合成的函数,也没有包含多...10 日期与时间函数 &lt;time.h&gt; . . . . 48 11 由实现定义的限制 &lt;limits.h&gt; 和 &lt;float.h&gt; . . . 52

    数据结构Time.txt

    T(n) Time 时间复杂度:算法最坏情况下执行的次数,与n(循环次数)相关 S(n) Space 空间复杂度:算法所需变量所占字节数 所有数据结构: *线性表(顺序,链式) *栈(顺序,链式) 队列(单向和环状的顺序) 串,...

    c++中求程序运行时间

    time函数返回以格林尼治时间(GMT)为标准,从1970年1月1日00:00:00到现在的此时此刻所经过的秒数。 若time_t参数没有被忽略,则经过的秒数也会被填入该指针所指向的区域内。 1、数据类型time_t其实就是一个long...

    linux 本地时间 时间 函数

    时间和日期函数需要头部&lt;time.h&gt;。这个头部定义了三种与时间有关的类型:clock_t,time_t蛅m。类型clock_t和time_t可以用长整数表示系统时间和日期,称为日历时。结构类型tm保存分解为相应元素的日期和时间。tm结构...

    C语言基础教程TXT

    7.8.7 随机数发生器函数 第8章 UNIX系统界面 8.1 文件描述符 8.2 低级I/O—read和write系统调用 ...B.10 日期与时间函数:&lt;time.h&gt; B.11 由实现定义的限制:&lt;limits.h&gt;和 &lt;float.h&gt; 附录C 变更小结

    时间函数的调用

    时间函数的调用 #include #include &lt;time.h&gt; //#include &lt;iomanip&gt; using namespace std; int main( ) { int i,j; srand(time(0)); for (i=1;i;i++) {j=rand()0; cout;} return 0; } /*#include #include...

    Snake-Game:建立经典的蛇游戏

    与rand函数一起使用的time.h时间函数 Conio.h(控制台输入输出),用于使用kbhit srand()函数之类的函数:用于生成随机值。 它包含在stdlib libraray中。 给该参数的参数是time()Kbhit()函数:它给出一个非零...

    c语言time()

    C语言中各种描述时间的函数,c语言 time() 函数

    数据结构课程设计 扑克牌

    #include "time.h" /*系统时间函数*/ #include "graphics.h" /*图形函数*/ #include "alloc.h"/*动态地址分配函数*/ #include "stdlib.h" /*库函数*/ #include "string.h" /*字符串函数*/ #include "ctype.h" /*字符...

Global site tag (gtag.js) - Google Analytics