Unix中的时间整理

发布时间:2020-06-27 11:33:04 作者:brigh_tsun
来源:网络 阅读:339

历史上,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.

推荐阅读:
  1. postgresql获取Unix时间戳
  2. PostgreSQL 时间与Unix时间戳

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

time t clock t unix时间

上一篇:Java编程基础阶段笔记 day01 Java语言概述

下一篇:VisualStudio不让控制台程序一闪而过

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》