您的位置:首页 > 编程语言 > C语言/C++

C /C++标准库 - <ctime> (time.h)

2015-12-18 18:09 330 查看

1. time.h概述

定义了两个宏,声明了四种类型和几个操作时间的函数。很多函数处理都表示当前日期(公历)和时间。有些函数处理本地时间(某个特定时区的时间)和夏令时(是确定本地时间算法的临时替代)。

2. 定义

2.1 types

序号标签说明
1clock_t表示时间的算数类型,CPU时钟计时单元,由clock()返回
2size_t无符号整形,可表示任何对象的bytes数
3time_t时间类型,由time()返回,表示的时间(日历时间)是从一个时间点(1970年1月1日0时0分0秒)到此时的秒数
4struct tmStructure containing a calendar date and time broken down into its components.(tm结构的这种时间表示为分解时间)

2.2 Macro constants

序号标签说明
1NULL空指针
2CLOCKS_PER_SEC是clock返回的每秒钟有多少个时钟计时单元,用于将clock()函数的结果转化为以秒为单位的量,但是这个量的具体值是与操作系统相关的

2.3 Time manipulation(操作)

序号标签原型说明
1clockclock_t clock (void);函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,若挂钟时间不可取,则返回-1。如果要以秒为单位计算,只需把函数clock()的返回值除以宏CLOCKS_PER_SEC即可
2difftimedouble difftime (time_t end, time_t beginning);return the result of (end-beginning) in seconds as a floating-point value of type double
3mktimetime_t mktime (struct tm * timeptr);用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的本地时间所经过的秒数
4timetime_t time (time_t* timer);获得日历时间(Calendar Time

2.4 Conversion

序号标签原型说明
1asctimechar* asctime (const struct tm * timeptr);Convert tm structure to string,format:Www Mmm dd hh:mm:ss yyyy
2ctimechar* ctime (const time_t * timer);Convert time_t value to string,format:Www Mmm dd hh:mm:ss yyyy, equivalent to: asctime(localtime(timer))
3gmtimestruct tm * gmtime (const time_t * timer);Convert time_t to tm as UTC time
4localtimestruct tm * localtime (const time_t * timer);Convert time_t to tm as local time
5strftimesize_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );strftime() 函数根据区域设置格式化本地时间/日期

3. 参考文献

/article/11693369.html

http://www.cplusplus.com/reference/ctime/

《C标准库》,P.J. Plauger 著
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: