C语言头文件 time.h
如下内容主要参考:https://www.runoob.com/cprogramming/c-standard-library-time-h.html
time相关的函数在 time.h 中可以查看原型。如下命令可以找出time.h的路径:
whereis time.h
在time.h中声明了很多和时间相关的函数,
先说明几个结构体和类型:
CLOCKS_PER_SEC, 这个宏表示每秒的处理器时钟个数。
clock_t 是长整型,这是一个适合存储处理器时间的类型, 通常用clock_t 类型的差值除以CLOCKS_PER_SEC
time_t 是长整型,这是一个适合存储日历时间的类型
struct tm 这是一个用来保存时间和日期的结构。
struct tm {
int tm_sec; /* 秒,范围从 0 到 59 */
int tm_min; /* 分,范围从 0 到 59 */
int tm_hour; /* 小时,范围从 0 到 23 */
int tm_mday; /* 一月中的第几天,范围从 1 到 31 */
int tm_mon; /* 月,范围从 0 到 11 */
int tm_year; /* 自 1900 年起的年数 */
int tm_wday; /* 一周中的第几天,范围从 0 到 6 */
int tm_yday; /* 一年中的第几天,范围从 0 到 365 */
int tm_isdst; /* 夏令时 */
};
strace timespec{
time_t tv_sec; // 记录当前的second时间
long tv_nsec; // 记录当前的 ns 时间
}
使用的函数是 int clock_gettime(clockid_t clk_id, struct timespec *tp);
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
使用的函数 int gettimeofday ( struct timeval * tv , struct timezone * tz ), 注意这个结构体和函数都是在 sys/time.h 中
一、常用的显示时间的函数
time_t time(time_t *timer)
计算当前日历时间,并把它编码成 time_t 格式。
如下函数将time_t类型和 struct tm 类型 相互转化
struct tm *gmtime(const time_t *timer)
timer 的值被分解为 tm 结构,并用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。
struct tm *localtime(const time_t *timer)
timer 的值被分解为 tm 结构,并用本地时区表示。
time_t mktime(struct tm *timeptr)
把 struct tm类型的数据 转换为一个依据本地时区的 time_t 值。
char *ctime(const time_t *timer)
返回一个表示当地时间的字符串,当地时间是基于time_t类型的数据, 和time()配合使用
char * asctime(const struct tm *timeptr)
返回一个指向字符串的指针,它代表了结构 timeptr 的日期和时间。
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)
根据 format 中定义的格式化规则,格式化结构 timeptr 表示的时间,并把它存储在 str 中。
1.2 函数举例
#include <time.h> void main() { time_t cur_time; char buffer[80]; time(&cur_time); printf("cur_time=%ld, %s", cur_time, ctime(&cur_time)); printf("%s", asctime(localtime(&cur_time))); printf("%s", asctime(gmtime(&cur_time))); strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localtime(&cur_time)); printf("Formatted time: %s\n", buffer); sleep(2); time(&cur_time); printf("cur_time=%ld, %s", cur_time, ctime(&cur_time)); printf("%s", asctime(localtime(&cur_time))); printf("%s", asctime(gmtime(&cur_time))); strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localtime(&cur_time)); printf("Formatted time: %s\n", buffer); }
执行结果
$ ./a.out
cur_time=1719316637, Tue Jun 25 19:57:17 2024
Tue Jun 25 19:57:17 2024
Tue Jun 25 11:57:17 2024
Formatted time: 2024-06-25 19:57:17
cur_time=1719316639, Tue Jun 25 19:57:19 2024
Tue Jun 25 19:57:19 2024
Tue Jun 25 11:57:19 2024
Formatted time: 2024-06-25 19:57:19
二、可以记录代码执行时间的函数
1. clock_t clock(void)
返回处理器时钟所使用的时间。
和CLOCKS_PER_SEC相除,得到秒为单位的时间
2. int clock_gettime(clockid_t clk_id, struct timespec *tp);
2.1> clockid_t是确定哪个时钟类型.
CLOCK_REALTIME: 标准POSIX实时时钟
CLOCK_MONOTONIC: POSIX时钟,以恒定速率运行;不会复位和调整,它的取值和 CLOCK_REALTIME 是一样的.
CLOCK_PROCESS_CPUTIME_ID和CLOCK_THREAD_CPUTIME_ID是CPU中的硬件计时器中实现的.
2.2> struct timespec *tp
它也有一个时间结构体:timespec ,timespec计算时间次数的单位是十亿分之一秒.
3. int gettimeofday ( struct timeval * tv , struct timezone * tz )
gettimofday()函数的精确度是微秒(μs)
4. int timespec_get(struct timespec *ts, int base);
该函数没有测试过
总结:
clock()函数的精确度是10毫秒(ms)
times()函数的精确度是10毫秒(ms)
gettimofday()函数的精确度是微秒(μs)
clock_gettime()函数的计量单位为十亿分之一,也就是纳秒(ns)
void main() { struct timespec tm1, tm2; clock_gettime(CLOCK_MONOTONIC, &tm1); usleep(5000); clock_gettime(CLOCK_MONOTONIC, &tm2); printf("%ld sec, %ld ns\n", tm2.tv_sec - tm1.tv_sec, tm2.tv_nsec - tm1.tv_nsec); } $ ./a.out 0 sec, 5032722 ns
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)