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.  */
  };
#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;
}

执行结果:

[[email protected] ~]# ./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

时间: 2024-08-02 05:50:39

Linux time函数的相关文章

linux内核函数库文件的寻找

linux内核函数的so库文件怎么找呢? 首先还是要产生一个进程的coredump文件的 linux有一个lib-gdb.so库,这个进程的coredump文件中所有load段的最后一个load段中,通过读取二进制文件将最后一个load段读取出来保存lib-gdb.so库文件,这个库文件就是内核函数的库文件. coredump文件头->多个程序头(每一个程序头都会对应一个load段)->通过程序头读取load段

Linux mmap函数简介

一.简介 Linux提供了内存映射函数mmap, 它把文件内容映射到一段内存上(准确说是虚拟内存上), 通过对这段内存的读取和修改, 实现对文件的读取和修改, 先来看一下mmap的函数声明: 头文件: <unistd.h> <sys/mman.h> 原型: void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offsize); 返回值: 成功则返回映射区起始地址, 失败则返回MAP_FAI

动态替换Linux核心函数的原理和实现

转载:https://www.ibm.com/developerworks/cn/linux/l-knldebug/ 动态替换Linux核心函数的原理和实现 在调试Linux核心模块时,有时需要能够实时获取内部某个路径上的某些函数的执行状态,例如查看传入的变量是否是期望的值,以便判断整个执行流程是否依然正常.由于系统运行时的动态性,使得在执行之前无法预先知道在执行路径的什么地方可能出现问题,因此只能在整个路径上增加许多不必要的信息查询点,造成有用的状态信息被淹没,而且这种增加信息输出的方式(一般

linux进程调度函数浅析(基于3.16-rc4)

众所周知,进程调度使用schedule()函数来完成,下面我们从分析该函数开始,代码如下(kernel/sched/core.c): 1 asmlinkage __visible void __sched schedule(void) 2 { 3 struct task_struct *tsk = current; 4 5 sched_submit_work(tsk); 6 __schedule(); 7 } 8 EXPORT_SYMBOL(schedule); 第3行获取当前进程描述符指针,存

linux fcntl函数

linux fcntl函数 #include <unistd.h>#include <fcntl.h>int fcntl(int fd, int cmd);int fcntl(int fd, int cmd, long arg);int fcntl(int fd, int cmd, struct flock *lock); [描述]fcntl()针对(文件)描述符提供控制.参数fd是被参数cmd操作(如下面的描述)的描述符.针对cmd的值,fcntl能够接受第三个参数int arg

linux crypt函数

linux crypt函数 1. crypt定义 #define _XOPEN_SOURCE /* See feature_test_macros(7) */ #include <unistd.h > char *crypt(const char *key, const char *salt); 上面是man 3 crypt看到的crypt函数定义. 从定义中看到要想使用crypt函数那么就得定义_XOPEN_SOURCE宏,有一些人只是把unistd.h包含进来,然后发现编译的时候出现cry

linux 时间函数

linux 时间函数: time/time_t  秒 ftime/ struct timeb 毫秒 gettimeofday/struct timeval us clock_gettime/ struct timespec ns gmtime/localtime/timegm/mktime/strftime/struct tm 其中 time 精度低, ftime 已废弃, clock_gettime 系统调用,精度高 gettimeofday  vsyscall ,系统态, 开销低 定时函数,

linux 系统函数之 (dirname, basename)【转】

转自:http://blog.csdn.net/peter_cloud/article/details/9308333 版权声明:本文为博主原创文章,未经博主允许不得转载. 除非你的原件考虑跨平台. 在Linux编程多使用一些系统函数真的很方便,哎没办法越来越懒~~~~~~ 今天记录一下dirname 和basename这两个简单的处理文件路径的linux系统函数. 头文件: #include <libgen.h> 函数定义: char *dirname(char *path); char *

linux文件函数-open

linux文件函数-open 一 打开文件 函数名:open 函数原形: int open(const char *pathname, int flags) int open(const char *pathname, int flags, mode_t mode) 函数功能:打开或者创建一个文件或者设备 所属头文件: #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> 返回值:成功返回文件描述符,失

linux文件函数-creat

linux文件函数-creat 一 创建文件 函数名:creat 函数原形: int creat(const char *pathname, mode_t mode) 函数功能:创建文件或者设备,并以只写的方式打开 所属头文件: #include <sys/types.h> #inlcude <sys/stat.h> #include <fcntl.h> 返回值: 成功:返回文件描述符数值 失败:返回-1 参数说明 pathname:创建文件的路径和名称 mode:创建