1. time.h概述
定义了两个宏,声明了四种类型和几个操作时间的函数。很多函数处理都表示当前日期(公历)和时间。有些函数处理本地时间(某个特定时区的时间)和夏令时(是确定本地时间算法的临时替代)。
2. 定义
2.1 types
序号 | 标签 | 说明 |
---|
1 | clock_t | 表示时间的算数类型,CPU时钟计时单元,由clock()返回 |
2 | size_t | 无符号整形,可表示任何对象的bytes数 |
3 | time_t | 时间类型,由time()返回,表示的时间(日历时间)是从一个时间点(1970年1月1日0时0分0秒)到此时的秒数 |
4 | struct tm | Structure containing a calendar date and time broken down into its components.(tm结构的这种时间表示为分解时间) |
2.2 Macro constants
序号 | 标签 | 说明 |
---|
1 | NULL | 空指针 |
2 | CLOCKS_PER_SEC | 是clock返回的每秒钟有多少个时钟计时单元,用于将clock()函数的结果转化为以秒为单位的量,但是这个量的具体值是与操作系统相关的 |
2.3 Time manipulation(操作)
序号 | 标签 | 原型 | 说明 |
---|
1 | clock | clock_t clock (void); | 函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,若挂钟时间不可取,则返回-1。如果要以秒为单位计算,只需把函数clock()的返回值除以宏CLOCKS_PER_SEC即可 |
2 | difftime | double difftime (time_t end, time_t beginning); | return the result of (end-beginning) in seconds as a floating-point value of type double |
3 | mktime | time_t mktime (struct tm * timeptr); | 用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的本地时间所经过的秒数 |
4 | time | time_t time (time_t* timer); | 获得日历时间(Calendar Time |
2.4 Conversion
序号 | 标签 | 原型 | 说明 |
---|
1 | asctime | char* asctime (const struct tm * timeptr); | Convert tm structure to string,format:Www Mmm dd hh:mm:ss yyyy |
2 | ctime | char* ctime (const time_t * timer); | Convert time_t value to string,format:Www Mmm dd hh:mm:ss yyyy, equivalent to: asctime(localtime(timer)) |
3 | gmtime | struct tm * gmtime (const time_t * timer); | Convert time_t to tm as UTC time |
4 | localtime | struct tm * localtime (const time_t * timer); | Convert time_t to tm as local time |
5 | strftime | size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr ); | strftime() 函数根据区域设置格式化本地时间/日期 |
3. 参考文献
- http://blog.youkuaiyun.com/xbaer/article/details/6422326
- http://www.cplusplus.com/reference/ctime/
- 《C标准库》,P.J. Plauger 著