加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码门户网 (https://www.92codes.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Unix > 正文

unix linux下时间结构,linux/unix系统时间相关的结构体及其说明

发布时间:2022-10-11 16:30:31 所属栏目:Unix 来源:网络
导读: 该类型定义在头文件 /usr/include/sys/time.h 中:
#define _TIME_T
typedeflongtime_t;
#endif
函数定义:time_ttime(time_t*lpt);
如:time_ttime= time(NULL);
2、struct timeb结构:

该类型定义在头文件 /usr/include/sys/time.h 中:

#define _TIME_T

typedeflongtime_t;

#endif

函数定义:time_ttime(time_t*lpt);

如:time_ttime= time(NULL);

2、struct timeb结构:它有两个主要成员,一个是秒,另一个是毫秒;精确度:毫秒(10E-3秒);

由函数ftime()获取struct timeb结构的时间;位于/usr/include/sys/timeb.h中unix系统时间,其定义如下:

struct timeb

{

time_ttime;

unsigned short millitm;

shorttimezone;

shortdstflag;

};

#include

intftime(structtimeb*tp);

调用成功返回0;调用失败返回-1;

3、structtimeval结构,它有两个成员;一个是秒,另一个表示微秒,精确度:微秒(10E-6);

由函数gettime0fday()获取;

structtimeval结构定义为:

structtimeval

{

longtv_sec;

longtv_usec;

}

读取struct timeval结构数据的函数说明:

#include

intgettimeofday(structtimeval*tv,structtimezone*tz);

该函数会提取系统当前时间,并把时间分为秒和微秒两部分填充到结构structtimeval中;同时把当地的时区信息填充到结构structtimezone中;

返回值:成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。

structtimezone结构的定义为:

structtimezone

{

inttz_minuteswest;

inttz_dsttime;

}

上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下

DST_NONE

DST_USA

DST_AUST

DST_WET

DST_MET

DST_EET

DST_CAN

DST_GB

DST_RUM

DST_TUR

DST_AUSTALT

4、structtimespec结构:它是POSIX.4标准定义的一个时间结构,精确度:纳秒(10E-9秒);

由函数gethrestime()或gethrestime_lasttick()获取当前系统structtimespec结构的时间;其定义如下:

structtimespec

{

time_ttv_sec;

longtv_nsec;

};

typedefstructtimespectimespec_t;

该结构定义在头头文件/usr/include/sys/time_impl.h 中;

externvoidgethrestime(timespec_t*);

externvoidgethrestime_lasttick(timespec_t*);

5、clock_t类型:由函数clock()获取;

#include

clock_tclock(void);

该函数以

微秒

的方式返回CPU的时间;

类型 clock_t 定义在头文件/usr/include/sys/types.h中:

#ifndef_CLOCK_T

#define _CLOCK_T

typedeflongclock_t;

#endif

6、structtm结构:由函数gmtime()解析time_t得到

structtm*gmtime(const time_t*timep);

函数说明:gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后

将结果由结构tm返回。

结构tm的定义为

struct tm

{

inttm_sec;

inttm_min;

inttm_hour;

inttm_mday;

inttm_mon;

inttm_year;

inttm_wday;

inttm_yday;

int tm_isdst;

};

inttm_sec 代表目前秒数,正常范围为0-59,但允许至61秒

inttm_min 代表目前分数,范围0-59

inttm_hour 从午夜算起的时数,范围为0-23

inttm_mday 目前月份的日数,范围01-31

inttm_mon 代表目前月份,从一月算起,范围从0-11

inttm_year 从1900 年算起至今的年数

inttm_wday 一星期的日数,从星期一算起,范围为0-6

inttm_yday 从今年1月1日算起至今的天数,范围为0-365

inttm_isdst 日光节约时间的旗标

此函数返回的时间日期未经时区转换,而是UTC时间。

返回值:返回结构tm代表目前UTC 时间

7、Unix对时间单位的定义:

#define SEC1//秒

#define MILLISEC1000//毫秒

#define MICROSEC1000000// 微秒

#define NANOSEC1000000000// 纳秒

8、时间格式化函数:

size_tstrftime(char*str,size_tmax,char*fmt,structtm*tp);

strftime有点像sprintf,其格式由fmt来指定。

%a:本第几天名称,缩写

%A:本第几天名称,全称

%b:月份名称,缩写

%B:月份名称,全称

%c:与ctime/asctime格式相同

%d:本月第几日名称,由零算起

%H:当天第几个小时,24小时制,由零算起

%I:当天第几个小时,12小时制,由零算起

%j:当年第几天,由零算起

%m:当年第几月,由零算起

%M:该小时的第几分,由零算起

%p:AM或PM

%S:该分钟的第几秒,由零算起

%U:当年第几,由第一个日开始计算

%W:当年第几,由第一个一开始计算

%w:当第几日,由零算起

%x:当地日期

%X:当地时间

%y:两位数的年份

%Y:四位数的年份

%Z:时区名称的缩写

%%:%符号

char*strptime(char*s,char*fmt,structtm*tp);如同scanf一样,解译字串成为tm格式

%h:与%b及%B同

%c:读取%x及%X格式

%C:读取%C格式

%e:与%d同

%D:读取%m/%d/%y格式

%k:与%H同

%l:与%I同

%r:读取"%I:%M:%S%p"格式

%R:读取"%H:%M"格式

%T:读取"%H:%M:%S"格式

%y:读取两位数年份

%Y:读取四位数年份

(编辑:源码门户网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章