APUE学习笔记(3)-时间概念

写在前面

1.          本文内容对应《 UNIX 环境高级编程》 ( 2 ) 》第 1 6 8 章。

2.          总结了 UNIX 系统下的两种时间概念。

3.          希望本文对您有所帮助,也欢迎您给我提意见和建议。

 


UNIX

 

系统一直使用两种不同的时间概念:日历时间和进程时间。

日历时间

保存日历时间的数据类型有三种:

l          time_t :记录自国际标准时间公元 1970 1 1 00:00:00 以来经历的秒数。通过 time 函数返回。文件属性中的最后访问时间,最后修改时间和最后文件属性更改时间,都使用该类型记录。

 

#include <time.h>

time_t time(time_t *calptr);

l          timeval :通过 gettimeofday 函数返回,除了记录 time_t 类型的秒数外,提供 long 类型的微秒计数。

 

#include <sys/time.h>

struct timeval {

     time_t tv_sec;    /* seconds */

     long   tv_usec;   /* microseconds */

};

int gettimeofday(struct timeval *restrict tp, void *restrict tzp);

l          tm :该结构将日历时间以年,月,日,时,分,秒,周日的形式表示。

 

struct tm {         /* a broken-down time */

     int  tm_sec;     /* seconds after the minute: [0 - 60] */

         int  tm_min;     /* minutes after the hour: [0 - 59] */

         int  tm_hour;    /* hours after midnight: [0 - 23] */

         int  tm_mday;    /* day of the month: [1 - 31] */

         int  tm_mon;     /* months since January: [0 - 11] */

         int  tm_year;    /* years since 1900 */

         int  tm_wday;    /* days since Sunday: [0 - 6] */

         int  tm_yday;    /* days since January 1: [0 - 365] */

         int  tm_isdst;   /* daylight saving time flag: <0, 0, >0 */

};

time_t,tm 和字符串之间相互转换的办法有:

l          time_t->tm :使用 localtime gmtime 函数,前者将日历时间转换为本地时间(考虑本地时区和夏时制标志),后者则将日历时间转换成国际标准时间的年,月,日,时,分,秒,周日。

 

#include <time.h>

struct tm *gmtime(const time_t *calptr);

struct tm *localtime(const time_t *calptr);

l          tm->time_t ,使用 mktime 函数,以本地时间的年月日等作为参数。

 

#include <time.h>

time_t mktime(struct tm *tmptr);struct tm *localtime(const time_t *calptr);

l          tm time_t-> 字符串 :使用 asctime ctime 函数,产生 26 字节的字符串,与 date 命令的输出形式类似。或者使用复杂如 printf strftime 函数。

 

#include <time.h>

char *asctime(const struct tm *tmptr);

char *ctime(const time_t *calptr);

size_t strftime(char *restrict buf, size_t maxsize,

                const char *restrict format,

                const struct tm *restrict tmptr);

 


进程时间

clock_t 类型存储,也称为 CPU 时间,用以度量进程使用的中央处理器资源,以时钟滴答计算(每秒钟滴答数可通过 sysconf(_SC_CLK_TCK) 查询)。当度量一个进程的执行时间时, UNIX 系统使用三个进程时间值(可执行 time 命令获得):

l          时钟时间 ,又称为墙上时钟时间。它是进程运行的时间总量,其值与系统中同时运行的进程数有关。

l          用户 CPU 时间 ,是执行用户指令所用的时间。

l          系统 CPU 时间 ,是为该进程执行内核程序所经历的时间。

任一进程及其已终止子进程(父进程 wait 到的子进程)的用户和系统 CPU 时间可调用 times 函数获得,保存在 tms 结构中。函数返回值为墙上时钟时间。它们都是相对于过去的某一时刻测量的,因此不能使用绝对值,而必须使用相对值。

两种时间概念的使用范例如下:

 

#include <time.h>

#include <sys/time.h>

#include <sys/times.h>

#include <stdio.h>

#include <unistd.h>

 

int main()

{

     time_t tt;

     struct timeval tval;

     struct tm *ltm, *gtm;

      long clktck;

     clock_t start, end;

     struct tms tmsstart, tmsend;

 

     start = times(&tmsstart);

     tt = time(NULL);

     printf("time_t(%d bytes): %d(sec)/n", sizeof(tt), (int)tt);

     gettimeofday(&tval, NULL);

     printf("timeval: %d(sec), %d(micsec)/n", (int)tval.tv_sec, (int)tval.tv_usec);

     ltm = localtime(&tt);

     printf("local time: %s", asctime(ltm));

     gtm = gmtime(&tt);

     printf("std time: %s", asctime(gtm));

     clktck = sysconf(_SC_CLK_TCK);

     printf("_SC_CLK_TCK: %ld/n", clktck);

     sleep(1);

     end = times(&tmsend);

     printf("total: %f(sec)/n", (end-start)/(double)clktck);

     printf("user: %f(sec)/n", (tmsend.tms_utime-tmsstart.tms_utime)/(double)clktck);

     printf("sys: %f(sec)/n", (tmsend.tms_stime-tmsstart.tms_stime)/(double)clktck);

 

     exit(0);

}

运行结果为:

 

pydeng@pydeng-laptop:~/apue.2e/mytest$ time ./a.out

time_t(4 bytes): 1249594897(sec)

timeval: 1249594897(sec), 828079(micsec)

local time: Fri Aug  7 05:41:37 2009

std time: Thu Aug  6 21:41:37 2009

_SC_CLK_TCK: 100

total: 1.000000(sec)

user: 0.000000(sec)

sys: 0.000000(sec)

 

real 0m1.002s

user 0m0.000s

sys  0m0.000s

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值