linux下时间有关的函数和结构体
来源:互联网 发布:晓风软件 编辑:程序博客网 时间:2024/05/22 14:49
1、时间类型。Linux下常用的时间类型有4个:time_t,struct timeb, struct timeval,struct timespec,clock_t, struct tm.
(1) time_t是一个长整型,一般用来表示用1970年以来的秒数.
该类型定义在<sys/time.h>中.
一般通过 time_t time = time(NULL); 获取.
(2) struct timeb结构: 主要有两个成员, 一个是秒, 另一个是毫秒, 精确度为毫秒.
- struct timeb
- {
- time_t time;
- unsigned short millitm;
- short timezone;
- short dstflag;
- };
由函数int ftime(struct timeb *tp); 来获取timeb.
成功返回0, 失败返回-1.
(3) struct timeval有两个成员,一个是秒,一个是微妙.
- struct timeval
- {
- long tv_sec; /* seconds */
- long tv_usec; /* microseconds */
- };
- struct timezone
- {
- int tz_minuteswest; /* 和Greewich时间差了多少分钟*/
- int tz_dsttime; /* 日光节约时间的状态 */
- };
- struct timespec
- {
- time_t tv_sec; /* seconds */
- long tv_nsec; /* nanoseconds */
- };
CLOCK_REALTIME 统当前时间,从1970年1.1日算起
CLOCK_MONOTONIC 系统的启动时间,不能被设置
CLOCK_PROCESS_CPUTIME_ID 进程运行时间
CLOCK_THREAD_CPUTIME_ID 线程运行时间
CLOCK_REALTIME_HR CLOCK_REALTIME的高精度版本
CLOCK_MONOTONIC_HR CLOCK_MONOTONIC的高精度版本
获取特定时钟的时间精度:
long clock_getres(clockid_t );
设置特定时钟的时间:
long clock_settime(clockid_t ,struct timespec*);
休眠time中指定的时间,如果遇到信号中断而提前返回,则由left_time返回剩余的时间:
long clock_nanosleep(clockid_t ,int flag,timespec* time,timespec* left_time);
(5) clock_t类型, 由clock_t clock(); 返回获取.
表示进程占用的cpu时间. 精确到微秒.
(6)struct tm是直观意义上的时间表示方法:
- struct tm
- {
- int tm_sec; /* seconds */
- int tm_min; /* minutes */
- int tm_hour; /* hours */
- int tm_mday; /* day of the month */
- int tm_mon; /* month */
- int tm_year; /* year */
- int tm_wday; /* day of the week */
- int tm_yday; /* day in the year */
- int tm_isdst; /* daylight saving time */
- };
在所有的UNIX下,都有个time()的函数
time_t time(time_t *t);
这个函数会传回从epoch开始计算起的秒数,如果t是non-null,它将会把时间值填入t中。
对某些需要较高精准度的需求,Linux提供了gettimeofday()。
int gettimeofday(struct timeval * tv,struct timezone *tz);
int settimeofday(const struct timeval * tv,const struct timezone *tz);
- linux下时间有关的函数和结构体
- linux下时间有关的函数和结构体
- linux下时间有关的函数和结构体
- linux下时间有关的函数和结构体
- pthread_cond_timedwait及Linux下与时间有关的结构体
- linux和win系统下有关时间的计算函数
- linux下有关时间的几个函数
- Linux下的时间相关结构和函数
- Linux下的时间结构及函数
- Linux中表示“时间”的结构体和相关函数
- Linux中表示“时间”的结构体和相关函数
- Linux中表示“时间”的结构体和相关函数
- Linux中表示“时间”的结构体和相关函数
- Linux中表示“时间”的结构体和相关函数
- Linux中表示“时间”的结构体和相关函数
- linux-时间相关结构体和函数
- linux-时间相关结构体和函数
- linux-时间相关结构体和函数
- 游戏引擎概念理解
- IP地址
- 对话框程序禁用系统的关闭按钮X
- Android学习路线指南
- HTC Vive开发笔记1
- linux下时间有关的函数和结构体
- redis 分片(即分库分表)
- 圆的构造方法
- 消费类电源解决方案:锂电池快充芯片
- PHP Warning: proc_open(): fork failed
- 火狐浏览器中重新开始下载Chrome Download失败的文件
- 友盟分享遇到的坑
- Android Service完全解析,关于服务你所需知道的一切(上)
- 我的WCF之旅(12):使用MSMQ进行Reliable Messaging