linux lib time
<time.h>头文件
数据类型
time_t (long)存储从1970年1月1日 00:00:00 UTC以来的秒数
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 从1970年开始的年份
- int tm_wday 星期0~6
- int tm_yday 一年中的天数 0~365
- int tm_isdst 夏令时标志
}
struct timespec{
- time_t tv_sec 秒数
- long tv_nsec 纳秒
}
clockid_t 用于标识时钟的类型,CLOCK_REALTIME & CLOCK_MONOTONIC
常量
- CLOCK_REALTIME 表示系统当前的实时时间
- CLOCK_MONOTONIC 表示系统启动以来的时间,不受系统时间的改变
库函数
- time()
- time_t time(time_t *tloc);
- tloc非NULL,当前时间被存储到tloc指向的变量中
- return 从1970年1月1日00:00:00 UTC到当前的秒数
- localtime()
- struct tm* localtime(const time_t *timer);
- timer 指向time_t的指针
- return struct tm结构体,表示本地时间
- gmtime()
- struct tm* gmtime(const time_t *timer);
- timer 指向time_t的指针
- return struct tm结构体,表示utc时间
- mktime()
- time_t mktime(struct tm* tm);
- tm 指向struct tm的指针 ,表示要转换的时间
- return 转换后的time_t 时间
- difftime()
- 计算两个time_t时间值之间的差值
- double difftime(time_t end, time_t start);
- end 结束时间 start 开始时间
- return end-start的差值,秒为单位
- strftime()
- size_t strftime(char *s, size_t max, const char *fomat, const struct tm *tm);
- s指向字符串的指针,用于存储格式化的结果
- max 格式化字符串
- format格式
- 日期格式
- %Y 年份 2024
- %y 年份 24
- %m 月份 数字 01~12
- %B 完整的月份名称 January
- %b 缩写的月份名称 Jan
- %d 月份中的第几天 01~31
- %A 完整的星期几 Monday
- %a 缩写的星期几 Mon
- %j 一年中的第几天 001~365
- 时间格式
- %H 24小时制度
- %I 12小时制
- %M 分钟
- %S 秒
- %p AM或PM
- %Z 时区名称或缩写
- %z 时区的数字形式 如 +0200
- 日期时间混合
- %c 标准日期和时间字符串 Wed Oct 18 16:54:00 2024
- %x 标准日期字符串 10/18/2024
- %X 标准时间字符串 16:54:00
- 其他
- %% 表示 %
- 日期格式
- tm 指向struct tm的指针
- return 成功写入字符串的字符数不包括终止符,失败return 0
- asctime()
- char * asctime(const struct tm* tm);
- tm 指向struct tm的结构体
- return 格式化之后的字符串
- ctime()
- char * ctime(const time_t *timer);
- timer 指向time_t 的指针
- return 格式化之后的时间字符串
- clock()
- clock_t clock(void);
- return 程序从启动到当前所消耗的时间,单位时钟周期。
- time()
sys/time.h 头文件
主要数据类型
struct timeval{
- time_t tv_sec; 秒数1970-01-01 00:00:00 后的秒数
- suseconds_t tv_usec;微妙数
}
struct timezone{
- int tz_minuteswest; 西方时区与utc的时差 分钟
- int tz_sdttime; 夏令时标志 默认0
}
fd_set
- 与select()函数一起 使用时,与时间相关的等待操作有密切关系。是用于文件描述符集合的数据类型。 select()函数通过它来监测多个文件描述符的I/O状态。
- 常用宏
- FD_ZERO(fd_set *set); 将文件描述符集合set清空
- FD_SET(int fd, fd_set *set); 将fd添加到set中
- FD_ISSET(int fd, fd_set *set); 判断fd是否已经添加到set中
- FD_CLR(int fd, fd_set *set); 将fd 从set中移除。
函数
gettimeofday() 获取当前时间,精确到微秒
- int gettimeofday(struct timeval *tv, struct timezone *tz);
- tv 指向struct timeval结构体
- tz 指向struct timezone结构体
- return 0表示成功, return -1表示失败
settimeofday() 设置系统时间
- int settimeofday(const struct timeval *tv, const struct timezone *tz);
- tv指向struct timeval结构体
- tz指向struct timezone结构体
- return 0 表示成功, -1 表示失败
timersub() 宏 用于将两个struct timeval结构体相减的宏
- void timersub(struct timeval* a, struct timeval *b, struct timeval *result);
timeradd() 宏 用于将两个struct timeval 结构体相加的宏
- void timeradd(struct timeval *a, struct timeval *b, struct timeval *result);
- a 指向 struct timeval
- b 指向 struct timeval
- result 指向 struct timeval
timercmp() 宏
- int timercmp(struct timeval a, struct timeval b, CMP);
- a b 指向struct timeval
- CMP 比较操作符 < > == 等
select() 多路io操作 ,也可以用于实现高精度的超时等待
- int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
- nfds 文件描述符的数量 通常为fd_set中最大文件描述符的值加1
- readfds 用于监控可读事件的文件描述符集合
- writefds 用于监控可写事件的文件描述符集合
- exceptfds 用于监控异常事件的文件描述符集合
- timeout 指定超时时间NULL表示无限等待,timeout指向一个struct timeval结构。
- return 就绪的fd个数 超时return 0 出错return -1 并设置errno
setitimer() 用于设置计时器,并在计时器到期时发送信号,可以用于定时任务的实现
int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value);
which 选择定时器的类型
- ITIMER_REAL 实时时钟,计时器到期时发送SIGALRM信号
- ITIMER_VIRTUAL 进程消耗的虚拟事件,计时器到期发送SIGALRM信号
- ITIMER_PROF 进程执行事件与系统调用时间之和,计时器到期后发送SIGPROF信号
new_value 指向 struct itimerval用于设置定时器的初始值和间隔值
struct itimerval{
struct timeval it_value{
- time_t tv_sec
- suseconds_t tv_usec
}
struct timeval it_interval{
- tv_sec
- tv_usec
}
}
odl_value 如果部位null, 存储先前的定时器值
return 0 成功, return -1 失败 设置errno