time函数
time函数
- time
#include<time.h>
time_t time(time_t *t);
typdef long int time_t;
time() returns the time as the number of secs since 1970-01-01 00:00:00 +0000(UTC)
假如参数t不是NULL,返回值(秒数)(日历时间)也存储在t中。
失败返回-1.
- ctime
#include <time.h>
char *ctime(const time_t *time);
将秒数转化为具体日期(ASCII),长度固定26字节。
the time is 1449579811
the time ascii is Tue Dec 8 21:03:31 2015
等价:asctime(localtime(t));
- broken-down time
Broken-down time is stored in the structure tm whichi is deiined in time.h as follows:
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
tm_year指的是自1900年以来的年数。
- asctime
char *asctime(const struct tm *tm);
将分离时间装换为字符串时间。
- localtime
struct tm *localtime(const time_t *timep);
将秒数装换为分离时间。
- gmtime
struct tm *gmtime(const time_t *timep);
- mktime
time_t mktime(struct tm *tm);
将分离时间转换为秒数。
- difftime
double difftime(time_t time1, time_t time0);
The difftime() function returns the number of seconds elapsed between time time1 and time time0, represented as a double. Each of the times is specified in calendar time, which means its value is a measurement (in seconds) relative to the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
- clock
clock_t clock(void);
DESCRIPTION
The clock() function returns an approximation of processor time used by the program.
RETURN VALUE
The value returned is the CPU time used so far as a clock_t; to get the number of seconds used, divide by CLOCKS_PER_SEC.
If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t) -1.
- gettimeofday
#include <sys/time.h>
struct timeval{
long tv_sec;
long tv_usec;
};
int gettimeofday(struct timeval *tv, struct timezone *tz);
gettimeofday将时间保存在tv中,tz一般使用NULL代替,不建议设置tz。返回日历时间即绝对时间(非本地时间)。
通过不同时间段运行函数可测量时间。
- clock_gettime/clock_settime
#include <time.h> struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ }; int clock_gettime(clockid_t clk_id, struct timespec *tp); int clock_settime(clockid_t clk_id, const struct timespec *tp);
The functions clock_gettime() and clock_settime() retrieve and set the time of the specified clock clk_id.
The clk_id argument is the identifier of the particular clock on which to act. A clock may be system-wide and hence visible for all processes, or per-process if it measures time only within a single process.m
CLOCK_REALTIME: real time,受adjtime和NTP影响和系统管理员调时的影响。
CLOCK_REALTIME_COARSE:快但少精度的CLOCK_REALTIME。
CLOCK_MONOTONIC:单调时钟,不受管理员调时影响,但受NTP和adjtime等调时影响。
CLOCK_MONOTONIC_RAW:和CLOCK_MONOTONIC相似,但基于硬件时间,不受NTP和aditime等任何调时影响。准确的启动时间应倚赖于此。
CLOCK_PROCESS_CPUTIME_ID:Pre-process CPU-time clock,测量进程内所有线程花费时间。
CLOCK_THREAD_CPUTIME_ID:Thread-specific CPU-time clock。
- setitimer&getitimer
#include <sys/time.h>
int getitimer(int which, struct itimerval *value);
int setitimer(int which, struct itimerval *newval, struct itimerval *oldval);
struct itimerval {
struct timeval it_interval;
struct timeval it_value;
};
itimerval 结构中的 it_value 是减少的时间,当这个值为 0 的时候就发出相应的信号了. 然后设置为 it_interval 值。
当it_interval为0时,timer为单触发时钟,singal-shot timer。
Linux 操作系统为每一个进程提供了 3 个内部间隔计时器.
ITIMER_REAL:减少实际时间.到时的时候发出 SIGALRM 信号.
ITIMER_VIRTUAL:减少有效时间(进程执行的时间).产生 SIGVTALRM 信号.
ITIMER_PROF:减少进程的有效时间和系统时间(为进程调度用的时间).这个经常和上面一 个使用用来计算系统内核时间和用户时间.产生 SIGPROF 信号.
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <sys/time.h> #include <signal.h> struct itimerval it; void sighandler(int sig) { printf("catch sig:[%d]...\n", sig); } int main(int argc, char *argv[]) { struct sigaction sa; memset(&sa, 0, sizeof(struct sigaction)); sa.sa_handler=sighandler; sa.sa_flags=0; sigaction(SIGALRM, &sa, NULL); //signal(SIGALRM, sighandler); //signal(SIGVTALRM, sighandler); it.it_interval.tv_sec = 2; it.it_interval.tv_usec = 100; it.it_value.tv_sec = it.it_interval.tv_sec; it.it_value.tv_usec = it.it_interval.tv_usec; setitimer(ITIMER_REAL, &it, NULL); //setitimer(ITIMER_VIRTUAL, &it, NULL); while(1){sleep(1);} return 0; }
参考: