Linux C编程下的时间
Linux C编程下的时间处理
1. time.h
(C标准库时间处理)
1.1 时间显示相关函数
// 获取Epoch时间
time_t time(time_t *timer);
- 功能:获取当前系统时间(自1970-01-01 00:00:00 UTC的秒数)
- 参数:
timer
为输出参数(若为NULL
则不保存) - 返回值:成功返回时间值,失败返回
(time_t)-1
// 时间结构体转换
struct tm *localtime(const time_t *timer);
- 功能:将
time_t
转换为本地时间的tm
结构体 - 结构体定义:
struct tm {
int tm_sec; // 秒 [0-60](含闰秒)
int tm_min; // 分 [0-59]
int tm_hour; // 时 [0-23]
int tm_mday; // 日 [1-31]
int tm_mon; // 月 [0-11](0为1月)
int tm_year; // 年(实际年份减1900)
int tm_wday; // 星期 [0-6](0为周日)
int tm_yday; // 年中的第几天 [0-365]
int tm_isdst; // 夏令时标志(负数表示未知)
};
// 时间格式化
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr);
- 功能:将
tm
结构体按格式转换为字符串 - 常用格式符:
%Y
(年)、%m
(月)、%d
(日)、%H
(时)、%M
(分)、%S
(秒) - 返回值:成功返回写入字符数,失败返回0
1.2 POSIX时钟与定时器
// 高精度时间获取
int clock_gettime(clockid_t clk_id, struct timespec *tp);
- 功能:获取指定时钟的纳秒级时间
时钟类型:
CLOCK_REALTIME
:系统实时时间(可修改)CLOCK_MONOTONIC
:系统启动后的单调时间(不可修改)CLOCK_BOOTTIME
:包含系统挂起时间的单调时钟
- 结构体定义:
struct timespec {
time_t tv_sec; // 秒
long tv_nsec; // 纳秒 [0-999,999,999]
};
// POSIX间隔式定时器函数
int timer_create(clockid_t clockid, struct sigevent *sevp, timer_t *timerid);
int timer_settime(timer_t timerid, int flags, const struct itimerspec *new_value, struct itimerspec *old_value);
int timer_delete(timer_t timerid);
核心功能:
timer_create
: 创建定时器,支持多种通知方式(信号/线程)timer_settime
: 设置定时器初始和间隔时间(精度达纳秒级)timer_delete
: 销毁定时器资源
- 参数示例:
struct itimerspec {
struct timespec it_interval; // 间隔时间
struct timespec it_value; // 初始到期时间
};
struct sigevent {
int sigev_notify; // 通知方式:SIGEV_SIGNAL/SIGEV_THREAD
int sigev_signo; // 信号类型(如SIGUSR1)
union sigval sigev_value; // 传递的数据
void (*sigev_notify_function)(union sigval); // 线程处理函数
};
2. sys/time.h
(系统级时间与定时器)
2.1 高精度时间获取
int gettimeofday(struct timeval *tv, struct timezone *tz);
- 功能:获取微秒级时间(兼容旧系统,推荐优先使用
clock_gettime
) - 结构体定义:
struct timeval {
time_t tv_sec; // 秒
suseconds_t tv_usec; // 微秒 [0-999,999]
};
2.2 传统间隔定时器
int setitimer(int which, const struct itimerval *new_val, struct itimerval *old_val);
定时器类型:
ITIMER_REAL
:真实时间,到期发送SIGALRM
ITIMER_VIRTUAL
:进程用户态CPU时间,发送SIGVTALRM
ITIMER_PROF
:总CPU时间(用户态+内核态),发送SIGPROF
- 结构体定义:
struct itimerval {
struct timeval it_interval; // 周期间隔
struct timeval it_value; // 初始到期时间
};
3. sys/times.h
(进程时间统计)
clock_t times(struct tms *buf);
- 功能:获取进程及子进程的CPU时间统计
- 结构体定义:
struct tms {
clock_t tms_utime; // 用户态CPU时间
clock_t tms_stime; // 内核态CPU时间
clock_t tms_cutime; // 终止子进程的用户态CPU时间
clock_t tms_cstime; // 终止子进程的内核态CPU时间
};
4. 使用场景对比
功能 | time.h | sys/time.h | sys/times.h |
---|---|---|---|
时间显示 | 基础时间转换与格式化 | 高精度时间获取(微秒级) | 不适用 |
间隔定时器 | POSIX定时器(纳秒级) | 传统定时器(微秒级) | 不适用 |
进程时间统计 | 不适用 | 不适用 | 支持(times ) |
实时性支持 | 支持(CLOCK_REALTIME ) | 仅支持系统实时时钟 | 不适用 |
5. 技术选型建议
- 基础时间操作
• 优先使用time.h
的strftime
进行时间格式化
• 跨平台场景可用localtime_r
替代localtime
(线程安全) - 高精度计时
• 首选clock_gettime(CLOCK_MONOTONIC)
(精度达纳秒级)
• 旧系统兼容场景使用gettimeofday
(微秒级) - 定时器实现
• 实时系统:POSIX定时器(timer_create
+timer_settime
)支持多定时器独立管理
• 传统场景:setitimer
适合简单定时需求(需注意信号冲突问题) - 性能监控
• 进程级统计:sys/times.h
的times
函数
• 代码段级分析:clock_gettime(CLOCK_THREAD_CPUTIME_ID)
获取线程级CPU时间
引用说明
本文函数原型和实现原理参考了Linux手册页和POSIX标准,定时器示例代码可参考GitHub开源项目linux-timer-examples
。