UNIX 與 Linux 的時間,是從「新紀元時間」Epoch 開始累積的秒數。Epoch 定義為 1970 年 1 月 1 日 00:00:00(格林威治時間)。
多數 UNIX 系統用 32 位元有號整數記錄,正值是 1970 之後,負值是之前,可表示的範圍是:
2^31 / 86400 = 24855.13 天 ≈ 68.10 年 1970 + 68.10 = 2038.0958 1970 - 68.10 = 1901.9042
也就是 [1901.9042, 2038.0958],確切地說,界限落在 2038 年 1 月 19 日 03:14:07 UTC。那一刻時間會翻成負數,跳到 1901 年 12 月 13 日星期五 20:45:52。這就是所謂的 UNIX 2038 BUG。多數 UNIX 系統沒有 Y2K 問題,但都有 2038 問題。
struct timeval {
int tv_sec; /* 從 Epoch 起算的秒數 */
int tv_usec; /* 微秒 */
};
struct timezone {
int tv_minuteswest; /* 距格林威治的西向分鐘差 */
int tv_dsttime; /* 日光節約時間修正方式 */
};
struct timespec {
long int tv_sec;
long int tv_nsec; /* 奈秒 */
};
struct tm {
int tm_sec; /* 秒,[0,61],多出的 2 秒供跳秒用 */
int tm_min; /* 分,[0,59] */
int tm_hour; /* 時,[0,23] */
int tm_mday; /* 日,[1,31] */
int tm_mon; /* 月,[0,11] */
int tm_year; /* 年,需加 1900 */
int tm_wday; /* 週幾,[0,6] */
int tm_yday; /* 一年第幾天,[0,365] */
int tm_isdst; /* 是否日光節約時間 */
};
struct itimerval {
struct timeval it_interval; /* 重複計時的間隔 */
struct timeval it_value; /* 本次剩餘時間 */
};
time_t time(time_t *t);
傳回 Epoch 起的秒數;若 t 非 NULL,同時把值填入 t。需要更高精度時用:
int gettimeofday(struct timeval *tv, struct timezone *tz); int settimeofday(const struct timeval *tv, const struct timezone *tz);
struct tm * gmtime(const time_t *t); /* 轉 UTC/GMT */ struct tm * localtime(const time_t *t); /* 轉本地時間 */ time_t mktime(struct tm *tp); /* tm → time_t,本地時間 */ time_t timegm(struct tm *tp); /* tm → time_t,UTC */ double difftime(time_t t2, time_t t1); /* 秒差 */
localtime 會尊重 TZ 環境變數,因此同一台機器上不同使用者可以顯示不同時區。
char * asctime(struct tm *tp); char * ctime(struct tm *tp);
兩者都輸出標準 UNIX 格式,例如 Mon May 3 08:23:35 1999。ctime 一律用當地時間,asctime 則依 tm 內的時區資訊。
要自訂格式就用 strftime,用法近似 sprintf:
size_t strftime(char *str, size_t max, char *fmt, struct tm *tp);
常用 specifier:
%a 週名簡寫 %A 週名全稱 %b 月名簡寫 %B 月名全稱 %c 等同 ctime 格式 %d 本月第幾日 %H 時(24 小時制) %I 時(12 小時制)%j 當年第幾天 %m 月 %M 分 %p AM 或 PM %S 秒 %U 週(以週日起算)%W 週(以週一起算) %w 週幾 %x 當地日期 %X 當地時間 %y 兩位數年 %Y 四位數年 %Z 時區縮寫 %% 百分比符號
反向解析字串成 tm 則用 strptime:
char * strptime(char *s, char *fmt, struct tm *tp);
time_t now;
struct tm *timenow;
time(&now);
timenow = localtime(&now);
printf("recent time is : %s \n", asctime(timenow));
unsigned int sleep(unsigned int seconds);
讓程式休眠數秒,除非收到不可忽略的信號。沒睡飽會傳回剩餘秒數,否則傳回 0。
void usleep(unsigned long usec); /* 單位為微秒 */ int select(0, NULL, NULL, NULL, struct timeval *tv); /* 可當 sleep 用 */ int nanosleep(struct timespec *req, struct timespec *rem);
nanosleep 若沒睡飽,會把剩餘時間寫進 rem。
unsigned int alarm(unsigned int seconds);
專為 SIGALRM 而設,指定秒數後向自身送訊號。再次呼叫會取代前一次設定;傳 0 則取消。若原本已有設定,傳回上一個剩餘秒數,否則傳回 0。
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void sigalrm_fn(int sig)
{
/* Do something */
printf("alarm!\n");
alarm(2);
return;
}
int main(void)
{
signal(SIGALRM, sigalrm_fn);
alarm(2);
while(1) pause();
}
int setitimer(int which, const struct itimerval *value,
struct itimerval *ovalue);
比 alarm 強得多,支援三種計時基礎:
ITIMER_REAL:真實時間,送 SIGALRMITIMER_VIRTUAL:行程實際執行的時間,送 SIGVTALRMITIMER_PROF:使用者態加核心態的執行時間,送 SIGPROF成功傳回 0,失敗傳回 -1。
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
int sec;
void sigroutine(int signo){
switch (signo){
case SIGALRM:
printf("Catch a signal -- SIGALRM \n");
signal(SIGALRM, sigroutine);
break;
case SIGVTALRM:
printf("Catch a signal -- SIGVTALRM \n");
signal(SIGVTALRM, sigroutine);
break;
}
return;
}
int main()
{
struct itimerval value, ovalue, value2;
sec = 5;
printf("process id is %d ", getpid());
signal(SIGALRM, sigroutine);
signal(SIGVTALRM, sigroutine);
value.it_value.tv_sec = 1;
value.it_value.tv_usec = 0;
value.it_interval.tv_sec = 1;
value.it_interval.tv_usec = 0;
setitimer(ITIMER_REAL, &value, &ovalue);
value2.it_value.tv_sec = 0;
value2.it_value.tv_usec = 500000;
value2.it_interval.tv_sec = 0;
value2.it_interval.tv_usec = 500000;
setitimer(ITIMER_VIRTUAL, &value2, &ovalue);
for(;;);
}
輸出會是 SIGALRM 每秒一次、SIGVTALRM 每 0.5 秒一次交錯出現。
Linux 的信號機制承襲自早期 UNIX。那個年代的信號處理有一個惱人特性:進程每處理完一次信號,該信號的處理方式就被重設為預設動作。若不希望如此,就必須在處理函式末尾再呼叫一次 signal() 重新安裝——上面兩個範例之所以每次都重設,正是這個緣故。
在 Red Hat 7.2 中,SIGRTMIN = 32、SIGRTMAX = 63,小於 SIGRTMIN 的信號都被歸為不可靠信號。要寫出健壯的程式,建議改用 sigaction(),它的行為明確且跨平台一致。