一些概念:Epoch,新纪元,时间点,在linux中是1970-1-1.00:00:00。
1. 获得当前时间
#include <time.h>
time_t time (time_t *t);
#include <sys/time.h>
int gettimeofday (struct timeval *tv, struct timezone *tz);
两个函数代表两种不同的精度,time_t精度为秒,struct timeval精度为微秒(百万分之一秒)。tz已经被弃用,使用时应用设置为NULL。
更高级的接口
#include <time.h>
int clock_gettime (clockid_t clock_id, struct timespec *ts);
struct timespec提供纳秒精度
其中clockid_t为以下值之一:
CLOCK_REALTIME
The system-wide real time (wall time) clock. Setting this clock requires special privileges.
CLOCK_MONOTONIC
A monotonically increasing clock that is not settable by any process. It represents the elapsed time since some unspecified starting point, such as system boot.
CLOCK_MONOTONIC_RAW
Similar to CLOCK_MONOTONIC, except the clock is not eligible for slewing (correction for clock skew). That is, if the hardware clock runs faster or slower than wall time, it won’t be adjusted when read via this clock. This clock is Linux-specific.
CLOCK_PROCESS_CPUTIME_ID
A high-resolution, per-process clock available from the processor. For example, on the x86 architecture, this clock uses the timestamp counter (TSC) register.
CLOCK_THREAD_CPUTIME_ID
Similar to the per-process clock, but unique to each thread in a process. POSIX requires only CLOCK_REALTIME. Therefore, while Linux reliably provides all five clocks, portable code should rely only on CLOCK_REALTIME.
2. 进程时间
#include <sys/times.h>
struct tms {
clock_t tms_utime; /* user time consumed */
clock_t tms_stime; /* system time consumed */
clock_t tms_cutime; /* user time consumed by children */
clock_t tms_cstime; /* system time consumed by children */
};
clock_t times (struct tms *buf);
获取进程运行的时间。
3. 设置时间
#define _SVID_SOURCE
#include <time.h>
int stime (time_t *t);
#include <sys/time.h>
int settimeofday (const struct timeval *tv, const struct timezone *tz);
#include <time.h>
int clock_settime (clockid_t clock_id, const struct timespec *ts);
设置时间需要有CAP_SYS_TIME capability,通常只有root用户才具有。
4. 操作时间
Broken-down time( tm structure )
#include <time.h>
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* the day of the month */
int tm_mon; /* the month */
int tm_year; /* the year */
int tm_wday; /* the day of the week */
int tm_yday; /* the day in the year */
int tm_isdst; /* daylight savings time? */
#ifdef _BSD_SOURCE
long tm_gmtoff; /* time zone's offset from GMT */
const char *tm_zone; /* time zone abbreviation */
#endif /* _BSD_SOURCE */
};
4.1 将time_t转换为tm
#include <time.h>
struct tm * gmtime (const time_t *timep);
struct tm * gmtime_r (const time_t *timep, struct tm *result);
Expressed in terms of the UTC time zone, on failure, it returns NULL.
#include <time.h>
char * asctime (const struct tm *tm);
char * asctime_r (const struct tm *tm, char *buf);
4.3 Converts a tm structrue to time_t
#include <time.h>
time_t mktime (struct tm *tm);
4.3 Converts time_t to ASCII resprentation
#include <time.h>
char * ctime (const time_t *timep);
char * ctime_r (const time_t *timep, char *buf);
4.4 将time_t转换为当地时间的tm structrue
#include <time.h>
struct tm * localtime (const time_t *timep);
struct tm * localtime_r (const time_t *timep, struct tm *result);
4.5 计算两个time_t之差
#include <time.h>
double difftime (time_t time1, time_t time0);
time_t的精度为秒,可以通过简单的加减来进来时间之差的计算,使用这个函数只是为了兼容而已。