time.h 详细介绍

< ctime> (time.h)包含获得和使用日期和时间信息的函数的定义。

一、Macro constants(宏常量)

  • CLOCKS_PER_SEC:滴答声/秒,时间的单位
  • NULL:空指针

二、types(类型)

  • clock_t:时钟类型,表示时钟滴答数的基本数据类型

  • size_t:无符号整型

  • time_t:时间类型,表示时间

  • struct tm:时间结构,包含日历、时间

Member Type Meaning Range
tm_sec int seconds after the minute0-60*
tm_min int minutes after the hour 0-59
tm_hour int hours since midnight 0-23
tm_mday int day of the month 1-31
tm_mon int months since January 0-11
tm_year int years since 1900
tm_wday int days since Sunday 0-6
tm_yday int days since January 1 0-365
tm_isdst int Daylight Saving Time flag

三、时间操作

1、clock_t clock (void);

描述:从特定时间开始消耗的时间,如果失败,返回-1。

/* clock example: frequency of primes */
#include <stdio.h>      /* printf */
#include <time.h>       /* clock_t, clock, CLOCKS_PER_SEC */
#include <math.h>       /* sqrt */int frequency_of_primes (int n) {int i,j;int freq=n-1;for (i=2; i<=n; ++i) for (j=sqrt(i);j>1;--j) if (i%j==0) {--freq; break;}return freq;
}int main ()
{clock_t t;int f;t = clock();printf ("Calculating...\n");f = frequency_of_primes (99999);printf ("The number of primes lower than 100,000 is: %d\n",f);t = clock() - t;printf ("It took me %d clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);return 0;
}

2、double difftime (time_t end, time_t beginning);

描述:计算从beginning到end的时间(end-beginning)(单位/秒)。

/* difftime example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, difftime, time, mktime */int main ()
{time_t now;struct tm newyear;double seconds;time(&now);  /* get current time; same as: now = time(NULL)  */newyear = *localtime(&now);newyear.tm_hour = 0; newyear.tm_min = 0; newyear.tm_sec = 0;newyear.tm_mon = 0;  newyear.tm_mday = 1;seconds = difftime(now,mktime(&newyear));printf ("%.f seconds since new year in the current timezone.\n", seconds);return 0;
}

3、time_t mktime (struct tm * timeptr);

描述:

  • 返回timeptr指针描述的时间,如果时间未描述,返回-1。

  • localtime的逆变换

  • 忽略结构成员tm_wday and tm_yday;其他成员及时超出有效范围,也将解释。

/* mktime example: weekday calculator */
#include <stdio.h>      /* printf, scanf */
#include <time.h>       /* time_t, struct tm, time, mktime */int main ()
{time_t rawtime;struct tm * timeinfo;int year, month ,day;const char * weekday[] = { "Sunday", "Monday","Tuesday", "Wednesday","Thursday", "Friday", "Saturday"};/* prompt user for date */printf ("Enter year: "); fflush(stdout); scanf ("%d",&year);printf ("Enter month: "); fflush(stdout); scanf ("%d",&month);printf ("Enter day: "); fflush(stdout); scanf ("%d",&day);/* get current timeinfo and modify it to the user's choice */time ( &rawtime );timeinfo = localtime ( &rawtime );timeinfo->tm_year = year - 1900;timeinfo->tm_mon = month - 1;timeinfo->tm_mday = day;/* call mktime: timeinfo->tm_wday will be set */mktime ( timeinfo );printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);return 0;
}

4、time_t time (time_t* timer);

描述:

  • 获取当前时间。

  • 如果时间指针不为NULL,将返回其指向的时间。

  • 不能取得时间,返回-1.

  • 返回的时间基于: 00:00 hours, Jan 1, 1970 UTC

/* time example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, difftime, time, mktime */int main ()
{time_t timer;struct tm y2k = {0};double seconds;y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;time(&timer);  /* get current time; same as: timer = time(NULL)  */seconds = difftime(timer,mktime(&y2k));printf ("%.f seconds since January 1, 2000 in the current timezone", seconds);return 0;
}

四、转换

1、char* asctime (const struct tm * timeptr);

描述:

返回timeptr指针的时间,用C-字符串描述返回类型为:Www Mmm dd hh:mm:ss yyyy(星期 月 日 时分秒 年)输出在新一行,以空字符串结束
/* asctime example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, time, localtime, asctime */int main ()
{time_t rawtime;struct tm * timeinfo;time ( &rawtime );timeinfo = localtime ( &rawtime );printf ( "The current date/time is: %s", asctime (timeinfo) );return 0;
}

2、char* ctime (const time_t * timer);

描述:

返回timer指针的时间,用C-字符串描述返回类型为:Www Mmm dd hh:mm:ss yyyy(星期 月 日 时分秒 年)输出在新一行,以空字符串结束和asctime(localtime(timer))一样
/* ctime example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, time, ctime */int main ()
{time_t rawtime;time (&rawtime);printf ("The current local time is: %s", ctime (&rawtime));return 0;
}

3、struct tm * gmtime (const time_t * timer);

描述:

利用timer填充tm结构体把time_t时间转化为UTC time
/* gmtime example */
#include <stdio.h>      /* puts, printf */
#include <time.h>       /* time_t, struct tm, time, gmtime */#define MST (-7)
#define UTC (0)
#define CCT (+8)int main ()
{time_t rawtime;struct tm * ptm;time ( &rawtime );ptm = gmtime ( &rawtime );puts ("Current time around the World:");printf ("Phoenix, AZ (U.S.) :  %2d:%02d\n", (ptm->tm_hour+MST)%24, ptm->tm_min);printf ("Reykjavik (Iceland) : %2d:%02d\n", (ptm->tm_hour+UTC)%24, ptm->tm_min);printf ("Beijing (China) :     %2d:%02d\n", (ptm->tm_hour+CCT)%24, ptm->tm_min);return 0;
}

4、struct tm * localtime (const time_t * timer);

描述:

利用timer填充tm结构体把time_t时间转化为 local time
/* localtime example */
#include <stdio.h>      /* puts, printf */
#include <time.h>       /* time_t, struct tm, time, localtime */int main ()
{time_t rawtime;struct tm * timeinfo;time (&rawtime);timeinfo = localtime (&rawtime);printf ("Current local time and date: %s", asctime(timeinfo));return 0;
}

5、·size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );

描述:

把timeptr中的时间,以特定的格式,复制到ptr中以format格式,复制时间timeptr到ptr,最多maxsize字符。ptr:目的数组,存储C-字符串maxsize:包括末尾空字符,复制到ptr的最大字符数format:格式返回值:返回复制到ptr的字符数(不包括末尾空字符),若超出范围maxsize,返回0
/* strftime example */
#include <stdio.h>      /* puts */
#include <time.h>       /* time_t, struct tm, time, localtime, strftime */int main ()
{time_t rawtime;struct tm * timeinfo;char buffer [80];time (&rawtime);timeinfo = localtime (&rawtime);strftime (buffer,80,"Now it's %I:%M%p.",timeinfo);puts (buffer);return 0;
}
specifier Replaced by Example
%a Abbreviated weekday name *
%A Full weekday name * Thursday
%b Abbreviated month name * Aug
%B Full month name * August
%c Date and time representation * Thu Aug 23 14:55:02 2001
%C Year divided by 100 and truncated to integer (00-99) 20
%d Day of the month, zero-padded (01-31) 23
%D Short MM/DD/YY date, equivalent to %m/%d/%y 08/23/01
%e Day of the month, space-padded ( 1-31) 23
%F Short YYYY-MM-DD date, equivalent to %Y-%m-%d 2001-08-23
%g Week-based year, last two digits (00-99) 01
%G Week-based year 2001
%h Abbreviated month name * (same as %b) Aug

time.h 详细介绍相关推荐

  1. Arduino WString.h库功能函数详细介绍

    Arduino WString.h库功能函数详细介绍 在Arduino开发框架下,String是一个很重要的数据类型.

  2. linux路由介绍,Linux的路由表详细介绍

    Linux的路由表详细介绍 一 在Linux下执行route命令[root@localhost backup]# route -nKernel IP routing tableDestination ...

  3. Linux文件系统中的inode节点详细介绍

    一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB). 操作系统 ...

  4. 红黑树(一)之 原理和算法详细介绍---转帖

    目录 1 红黑树的介绍 2 红黑树的应用 3 红黑树的时间复杂度和相关证明 4 红黑树的基本操作(一) 左旋和右旋 5 红黑树的基本操作(二) 添加 6 红黑树的基本操作(三) 删除 作者:Sky W ...

  5. 红黑树(一)之 原理和算法详细介绍

    出处:http://www.cnblogs.com/skywang12345/p/3245399.html 概要 目录 1 红黑树的介绍 2 红黑树的应用 3 红黑树的时间复杂度和相关证明 4 红黑树 ...

  6. Linux shell脚本基础学习详细介绍(完整版)2

    详细介绍Linux shell脚本基础学习(五) Linux shell脚本基础前面我们在介绍Linux shell脚本的控制流程时,还有一部分内容没讲就是有关here document的内容这里继续 ...

  7. u-boot 详细介绍 .

    Bootloader 对于计算机系统来说,从开机上电到操作系统启动需要一个引导过程.嵌入式Linux系统同样离不开引导程序,这个引导程序就叫作Bootloader. 6.1.1  Bootloader ...

  8. Ubuntu根目录下各文件夹的功能详细介绍

    Ubuntu的根目录下存在着很多的文件夹,但你知道他们都存放着哪些文件呢?这些是深入了解Ubuntu系统必不缺少的知识,本文就关于此做一下介绍吧. /bin/    用以存储二进制可执行命令文件. / ...

  9. wince中BIB文件的详细介绍

    wince中BIB文件的详细介绍 在WinCE中使用的一个重要的文件就是BIB文件,全称Binary Image Builder File.在WinCE的编译过程中会用到BIB文件,应该是在最后的Ma ...

最新文章

  1. Leangoo阶段式(瀑布式)游戏产品研发
  2. Swift 必备开发库 (高级篇)
  3. C# WinForm 技巧四:COMBOBOX搜索提示
  4. 微软开源P语言,实现安全的异步事件驱动编程
  5. 在中国,有这样一些村落
  6. 使用管控策略,设定多账号组织全局访问边界
  7. mysql视频第一课_MYSQL 第一课
  8. Oracle18C RPM安装介绍
  9. 如何在linux系统中设置静态ip地址
  10. Unity3D圣典学习【2】之CharacterController
  11. 菜鸟的Hadoop快速入门
  12. NVIDIA CUDA各版本下载链接(包括最新11版本和以往10.2版本)
  13. python监控服务器cpu温度实例_用python访问CPU温度
  14. 经济学金融学书籍推荐
  15. JavaWeb 注解
  16. Oracle 11g 扩展UNDO表空间
  17. hihocoder,#1498 : Diligent Robots。python
  18. springboot 整合 security(四) 方法级别权限控制 @resource,@secured,@preAuthorize
  19. 三种常见的建筑企业并购方案
  20. 1162. 【NOI2002】贪吃的九头龙 (Standard IO)

热门文章

  1. 【bzoj 4059】Non-boring sequences
  2. Full_of_Boys训练3总结
  3. JSON中的安全问题
  4. Gitlab环境快速部署(RPM包方式安装)
  5. java中的多线程(转自http://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html)
  6. 【转】linux之pmap命令!
  7. 开发完整J2EE解决方案的八个步骤
  8. R3抹掉加载的DLL
  9. bfs+状态压缩dp
  10. codeforces 229C