您的位置:首页 > 其它

Unix中的时间整理

2016-06-11 09:23 239 查看
历史上,Unix系统中使用过两种不同类型的时间值:
1、日历时间。该值是自UTC(或格林尼治标准时间)以来经历过的描述累计值。对应的系统基本数据类型是time_t.
2、进程时间。又称CPU时间,用以度量进程使用的中央处理器资源。用时钟滴答数来表示,对应的系统基本数据类型是clock_t.

另外,unix系统中还定义了以下几种表示时间的数据类型:
struct tm : 格式化的分解的时间信息,具有多个字段,包括秒、分、时、日期、月份、年份、星期、一年中的第几天等。
struct timeval : 由秒和微秒两个字段,共同表示一个时间,最高精度时微秒。
struct timespec : 有秒和纳秒两个字段,分别以两种精度表示时间。

表示时间的基本数据类型time_t 、clock_t 和其它几种表示时间的结构体之间可以通过如下的系统调用函数联系起来:
time_t time(time_t *t);
int clock_gettime(clockid_t clk_id, struct timespec *tp);

int gettimeofday(struct timeval *tv, struct timezone *tz);
初次之外,还有如下一些库函数可以使用:
transform date and time to broken-down time or ASCII

#include <time.h>

char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);

char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);

struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);

struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);

time_t mktime(struct tm *tm);
使用场合:
1.文件的st_atime、st_mtime、st_ctime都是使用数据类型struct timespec.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  time t clock t unix时间