Linux time函数

Linux下time函数都在time.h头文件中。

1、头文件
和时间有关的头文件有以下几个:

time.h
sys/time.h
sys/times.h
sys/timeb.h
sys/timex.h

time.h是C标准库的头文件,其余sys开头的都是Linux系统自己的头文件。
/usr/include/time.h定义了常用的time函数。
到/usr/include/sys目录下查看这几个文件:
sys/time.h定义了timezone结构体和Linux系统的时间函数。
sys/times.h定义了进程使用CPU时间的结构体tms。
sys/timeb.h定义了ftime函数的返回值的结构体timeb。
sys/timex.h定义了关于时钟调整算法的结构体timex。

2、常用函数和结构体
time函数原型(time.h中):

time_t time(time_t *calptr);

参数:
time_t类型变量的指针。
返回值:
time_t类型相当于一个long,time用于取Epoch记年以来到现在经过的秒数(系统当前时间),Epoch记年从1970年1月1日开始。把取到的时间存在指针指向的变量中。

localtime函数原型(time.h中):

struct tm *localtime(const time_t *calptr);

参数:
time_t类型变量的指针。
返回值:
指向tm结构体的指针类型。
作用是将time_t的值转换为tm结构体。然后可以打印输出。

tm结构体(time.h中):

/* Used by other time functions.  */
struct tm
{
  int tm_sec;			/* Seconds.	[0-60] (1 leap second) */
  int tm_min;			/* Minutes.	[0-59] */
  int tm_hour;			/* Hours.	[0-23] */
  int tm_mday;			/* Day.		[1-31] */
  int tm_mon;			/* Month.	[0-11] */
  int tm_year;			/* Year	- 1900.  */
  int tm_wday;			/* Day of week.	[0-6] */
  int tm_yday;			/* Days in year.[0-365]	*/
  int tm_isdst;			/* DST.		[-1/0/1]*/

#ifdef	__USE_BSD
  long int tm_gmtoff;		/* Seconds east of UTC.  */
  __const char *tm_zone;	/* Timezone abbreviation.  */
#else
  long int __tm_gmtoff;		/* Seconds east of UTC.  */
  __const char *__tm_zone;	/* Timezone abbreviation.  */
#endif
};


ftime函数原型(timeb.h):

int ftime(struct timeb *tp);

参数:
指向timeb结构体变量的指针。
返回值:
将当前系统时间存入timeb结构体中,包括了秒和毫秒。作用就是能获取当前时间精确到毫秒。

timeb结构体(sys/timeb.h):

/* Structure returned by the `ftime' function.  */
struct timeb
  {
    time_t time;		/* Seconds since epoch, as from `time'.  */
    unsigned short int millitm;	/* Additional milliseconds.  */
    short int timezone;		/* Minutes west of GMT.  */
    short int dstflag;		/* Nonzero if Daylight Savings Time used.  */
  };


times函数原型:

clock_t times(struct tms *buf);

参数:
指向tms结构体变量的指针。
返回值:
clock_t等同于long类型。用于获得进程运行时的CPU时间。

tms结构体(sys/times.h中):

/* Structure describing CPU time used by a process and its children.  */
struct tms
  {
    clock_t tms_utime;		/* User CPU time.  */
    clock_t tms_stime;		/* System CPU time.  */

    clock_t tms_cutime;		/* User CPU time of dead children.  */
    clock_t tms_cstime;		/* System CPU time of dead children.  */
  };


3、例子程序:test10.c

#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/timeb.h>
#include <unistd.h>

int main(void)
{
	int i = 0;
	int sum = 0;
	long tck = 0;
	long lBeginTime = 0;
	long lEndTime = 0;
	
	time_t curr;
	struct tm * tTM;
	struct tms tTMS;
	struct timeb tTimeB;
	
	tzset();
	
	//time函数获得秒数
	time(&curr);
	printf("current time is %ld seconds\n", curr);
	
	//localtime函数转换time_t
	tTM = localtime(&curr);
	printf("%4d-%02d-%02d %02d:%02d:%02d\n", tTM->tm_year + 1900, tTM->tm_mon + 1, tTM->tm_mday, 
	tTM->tm_hour, tTM->tm_min, tTM->tm_sec);
	
	//ftime函数获得时间包括毫秒
	ftime(&tTimeB);
	tTM = localtime(&tTimeB.time);
	printf("%4d-%02d-%02d %02d:%02d:%02d :%3d\n", tTM->tm_year + 1900, tTM->tm_mon + 1, tTM->tm_mday, 
	tTM->tm_hour, tTM->tm_min, tTM->tm_sec, tTimeB.millitm);
	
	//用times函数计算以下循环运行花费的时间
	lBeginTime = times(&tTMS);
	printf("lBeginTime = %ld\n", lBeginTime);
	while (1)
	{
		i = i + 1;
		if (i == 0)
			break;
	}
	lEndTime = times(&tTMS);
	printf("lEndTime = %ld\n", lEndTime);
	printf("循环使用的CPU时间为: %ld\n", lEndTime - lBeginTime);
	tck = sysconf(_SC_CLK_TCK);//获取系统时钟(1秒里有多少个)
	printf("转换为秒: %f\n", ((lEndTime - lBeginTime) / (double)tck));
	
	return 0;
}


执行结果:

[root@server ~]# ./test10
current time is 1421644980 seconds
2015-01-19 00:23:00
2015-01-19 00:23:00 :781
lBeginTime = 708268851
lEndTime = 708270107
循环使用的CPU时间为: 1256
转换为秒: 12.560000

参考资料:
http://www.cnblogs.com/yaozhongxiao/archive/2013/04/14/3020353.html
http://blog.csdn.net/jinan1861/article/details/5927677

《Linux time函数》上的3个想法

评论已关闭。