#include <time.h>
time_t time(time_t *t)
errno がセットされる。#include <time.h>
clock_t clock(void);
CLOCKS_PER_SEC で割ると、秒数を求められる。この定数値は、Linux の場合 1,000,000、Windows の場合は 1000 になっている。ミリ秒を求めたい場合は、戻り値を (CLOCKS_PER_SEC / 1000) で割ればよい。clock() は短い時間を返す。clock_t が 32bit になる環境では、clock() の戻り値は約 72 秒で一周する。#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
timeval 構造体には、秒 (tv_sec)、マイクロ秒 (tv_usec)が格納される。errno をセットする。ちなみに、timeval 構造体同士の演算を行うには、以下のようなマクロを使用します (see: man timeradd)。
#include <sys/time.h>
void timeradd(struct timeval *a, struct timeval *b, struct timeval *res);
void timersub(struct timeval *a, struct timeval *b, struct timeval *res);
void timerclear(struct timeval *tvp);
int timerisset(struct timeval *tvp);
int timercmp(struct timeval *a, struct timeval *b)
timeval の比較は timercmp マクロを使って以下のようにします。
if (timercmp(&a, &b, ==)) { ...等しい場合 ... }
if (timercmp(&a, &b, <)) { ... a < b の場合 ... }
if (timercmp(&a, &b, >)) { ... a > b の場合 ... }