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_t 相当于一个long类型,time用于取Epoch记年以来到现在经过的秒数(系统当前时间),Epoch记年从1970年1月1日开始。把取到的时间存在指针指向的变量中。
localtime函数原型(在time.h中):
struct tm *localtime(const time_t *calptr);
参数:time_t 类型变量的指针;
返回值:指向 tm 结构体的指针类型;作用是将time_t的值转换为tm结构体。然后可以打印输出。
- #ifdef _TIME_H
- __BEGIN_NAMESPACE_STD
- /* 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_MISC
- 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
- };
- __END_NAMESPACE_STD
- #if defined __USE_XOPEN || defined __USE_POSIX
- __USING_NAMESPACE_STD(tm)
- #endif
ftime.h函数原型(timeb.h)
int ftime(struct timeb *tp);
参数:指向timeb结构体变量的指针;
返回值:将当前系统时间存入timeb结构体中,包括了秒和毫秒。作用是能获取当前时间精确到毫秒;
timeb结构体(sys/timeb.h)
- __BEGIN_DECLS
-
- /* 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. */
- };
-
- /* Fill in TIMEBUF with information about the current time. */
-
- extern int ftime (struct timeb *__timebuf);
-
- __END_DECLS
times函数原型(sys/times.h)
clock_t times(struct tms *buf);
参数:指向tms结构体变量的指针。
返回值:clock_t类型等同于long类型,用于获得进程运行时的CPU时间。
tms结构体(sys/times.h中)
- __BEGIN_DECLS
-
- /* 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. */
- };
-
-
- /* Store the CPU time used by this process and all its
- dead children (and their dead children) in BUFFER.
- Return the elapsed real time, or (clock_t) -1 for errors.
- All times are in CLK_TCKths of a second. */
- extern clock_t times (struct tms *__buffer) __THROW;
-
- __END_DECLS
示例:
- #include <stdio.h>
- #include <time.h>
- #include <sys/time.h>
- #include <sys/timeb.h>
- #include <sys/times.h>
- #include <unistd.h>
-
- int main(int argc, char* argv[])
- {
- int i = 0; ///< 循环参数
- long tck = 0; ///< 获取系统时钟
-
- long l_begin_time = 0; ///< 起始时间点
- long l_end_time = 0; ///< 结束时间点
-
- time_t curr;
-
- struct tm * t_tm;
-
- struct tms t_tms;
-
- struct timeb t_tmb;
-
- tzset(); ///< 对 UNIX 操作系统的兼容性
-
- // time函数获得秒数
- time(&curr);
- printf("current time is %ld seconds\n", curr);
-
- // localtime函数转换time_t
- t_tm = localtime(&curr);
- printf("%4d-%02d-%02d %02d:%02d:%02d\n", t_tm->tm_year + 1900, t_tm->tm_mon + 1, t_tm->tm_mday, t_tm->tm_hour, t_tm->tm_min, t_tm->tm_sec);
-
- // ftime函数获得时间包括毫秒
- ftime(&t_tmb);
- t_tm = localtime(&t_tmb.time);
- printf("%4d-%02d-%02d %02d:%02d:%02d :%3d\n", t_tm->tm_year + 1900, t_tm->tm_mon + 1, t_tm->tm_mday, t_tm->tm_hour, t_tm->tm_min, t_tm->tm_sec, t_tmb.millitm);
-
- // 用times函数计算以下循环运行花费的时间
- l_begin_time = times(&t_tms);
- printf("l_begin_time = %ld\n", l_begin_time);
- while (1)
- {
- i = i + 1;
- if (i == 0)
- {
- break;
- }
- }
- l_end_time = times(&t_tms);
- printf("l_end_time = %ld\n", l_end_time);
- printf("used CPU time: %ld\n", l_end_time - l_begin_time);
- tck = sysconf(_SC_CLK_TCK); ///< 获取系统时钟(1秒里有多少个)
- printf("transform minutes: %f\n", ((l_end_time - l_begin_time) / (double)tck));
-
- return 0;
- }
执行结果:
