For faster navigation, this Iframe is preloading the Wikiwand page for time.h.

time.h

time.hC标准函数库中获取时间与日期、对时间与日期数据操作及格式化的头文件

表示时间的三种数据类型

[编辑]
  • 日历时间(Calendar Time),是从一个标准时间点(epoch)到现在的时间经过的秒数,不包括插入闰秒对时间的调整。开始计时的标准时间点,各种编译器一般使用UTC 1970-01-01 00:00:00。日历时间用数据类型time_t表示。[1]:20time_t类型实际上一般是32位整数类型,因此表示的时间不能晚于UTC 2038-01-18 19:14:07。为此,某些编译器引入了64位甚至更长的整型来保存日历时间,如Visual C++支持__time64_t数据类型,通过_time64()函数获取日历时间,可支持到UTC 3001-01-01 00:00:00的时间。
  • 处理器时间(Processor Time),也被称为CPU时间(CPU Time),用以度量进程使用的CPU资源。处理器时间以时钟滴答数(Clock Tick)计算,通常从进程启动开始计时,因此这是相对时间。时钟滴答数用系统基本数据类型clock_t来表示,每秒钟包含CLOCKS_PER_SEC(time.h中定义的常量,一般为1000)个时钟滴答,也可使用sysconf函数得到每秒的时钟滴答数。clock_t类型一般是32位整数类型。[1]:20
  • 分解时间(broken-down time),用结构数据类型tm表示,tm包含下列结构成员:
成员 描述
int tm_hour 小时 (0 – 23)
int tm_isdst 夏令时启用 (> 0)、禁用 (= 0)、未知 (< 0)
int tm_mday 一月中的哪一天 (1 – 31)
int tm_min 分 (0 – 59)
int tm_mon 月 (0 – 11, 0 = 一月)
int tm_sec 秒 (0 – 60, 60 = 闰秒)
int tm_wday 一周中的哪一天 (0 – 6, 0 = 周日)
int tm_yday 一年中的哪一天 (0 – 365)
int tm_year 1900 以来的年数

从电脑系统时钟获得时间的方法

[编辑]
  • time_t time(time_t* timer)
得到从标准计时点(一般是UTC 1970年1月1日午夜)到当前时间的秒数。
  • clock_t clock(void)
得到从进程启动到此次函数调用的累计的时钟滴答数。

Windows API提供了更为精确的GetLocalTime()获取毫秒级的日历时间;QueryPerformanceCounter和QueryPerformanceFrequency两个函数获取高于1毫秒的精度。

三种时间日期数据类型的转换函数

[编辑]
  • struct tm* gmtime(const time_t* timer)
从日历时间time_t到分解时间tm世界协调时UTC)的转换。函数返回的是一个静态分配的tm结构存储空间,该存储空间被gmtime, localtimectime函数所共享. 这些函数的每一次调用会覆盖这块tm结构存储空间的内容。
  • struct tm* gmtime_r(const time_t* timer, struct tm* result)
该函数是gmtime函数的线程安全版本.
  • struct tm* localtime(const time_t* timer)
从日历时间time_t到分解时间tm的转换,即结果数据已经调整到本地时区与夏令时。
  • time_t mktime(struct tm* ptm)
从基于本地时区(与夏令时)的分解时间tm到日历时间time_t的转换。忽略tm_wday与tm_yday的输入值。如果tm_isdst不确定则输入设为-1。其它各输入域的值可以任意设定,输出时被规范化到正确范围。例如,想要计算2012年的第200天的日期,设为1月200日,函数会输出正确的结果
  • time_t timegm(struct tm* brokentime)
从分解时间tm(被视作UTC时间,不考虑本地时区设置)到日历时间time_t的转换。该函数较少被使用。

时间日期数据的格式化函数

[编辑]
  • char *asctime(const struct tm* tmptr)
把分解时间tm输出到字符串,结果的格式为"Www Mmm dd hh:mm:ss yyyy",即“周几 月份数 日数 小时数:分钟数:秒钟数 年份数”。函数返回的字符串为静态分配,长度不大于26,与ctime函数共享。函数的每次调用将覆盖该字符串内容。
  • char* ctime(const time_t* timer)
把日历时间time_t timer输出到字符串,输出格式与asctime函数一样.
  • size_t strftime(char* s, size_t n, const char* format, const struct tm* tptr)
把分解时间tm转换为自定义格式的字符串,类似于常见的字符串格式输出函数sprintf。例如:strftime(buf, 64, "%Y-%m-%d %H:%M:%S", localtime);
  • char * strptime(const char* buf, const char* format, struct tm* tptr)
strftime的逆操作,把字符串按照自定义的格式转换为分解时间tm

对时间数据的操作

[编辑]
  • double difftime(time_t timer2, time_t timer1)
比较两个日历时间之差。

原始码示例

[编辑]

打印当前时间到标准输出流:

# include <stdio.h>
# include <time.h>

int main(void)
{
    time_t  timer = time(NULL);
    printf("ctime is %s\n", ctime(&timer));
    return 0;
}

参考文献

[编辑]
  1. ^ 1.0 1.1 Stevens, W. Richard; Rago, Stephen A., Advanced Programming in the UNIX Environment 3rd., United States of America: Pearson Education, Inc., 2013 [2021-02-09], ISBN 978-0-321-63773-4, (原始内容存档于2014-03-15) (英语) 

参考来源

[编辑]

外部链接

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
time.h
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?