C Unix 時間戳程式碼片段
C 範例,用於取得當前 Unix 時間戳、用 strftime() 將紀元秒轉換為可讀日期,以及用 timegm() 從日曆日期建構時間戳。
#include <time.h>
time_t ts = time(NULL);#include <time.h>
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
long ms = (long)ts.tv_sec * 1000L + ts.tv_nsec / 1000000L;#include <time.h>
#include <stdio.h>
time_t ts = 1700000000;
struct tm *tm_info = gmtime(&ts); // UTC
char buf[64];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm_info);
printf("%s\n", buf); // "2023-11-15 06:13:20"#include <time.h>
time_t ts = 1700000000;
struct tm *tm_info = localtime(&ts); // host timezone
char buf[64];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm_info);#define _GNU_SOURCE
#include <time.h>
struct tm t = {0};
t.tm_year = 2023 - 1900; // years since 1900
t.tm_mon = 10; // 0-indexed; 10 = November
t.tm_mday = 15;
t.tm_hour = 6;
t.tm_min = 13;
t.tm_sec = 20;
time_t ts = timegm(&t); // GNU/POSIX; use mktime() for local#include <time.h>
time_t start = time(NULL);
/* ... work ... */
time_t end = time(NULL);
double elapsed = difftime(end, start);C 時間戳記基礎
C 用 time_t 表示 Unix 時間,它是定義在 <time.h> 中的有號整數型別。在 64 位系統上,time_t 是 64 位,免受 2038 年溢位影響。在 32 位系統上,許多平台仍是 32 位;用 sizeof(time_t) 確認。標準函式 time(NULL) 以秒返回目前 Unix 時間。
gmtime 與 localtime
gmtime() 把 time_t 轉換為 UTC 的 struct tm。localtime() 轉換為主機系統的本地時區。當你需要可重現、與時區無關的結果時,始終使用 gmtime()。兩個函式都返回指向共享靜態 struct 的指標;如果你需要跨函式呼叫使用,請把結果複製到一個本地 struct tm 變數中。
C 生產注意事項
對於次秒精度,優先使用 clock_gettime(CLOCK_REALTIME) 而非 gettimeofday();POSIX 把 gettimeofday() 標記為過時。把日曆日期轉換為 UTC 時間戳記時,使用 timegm()(GNU/BSD)或可移植的等價物;mktime() 把輸入解釋為本地時間,在非 UTC 環境中會悄然產生錯誤結果。
- 在嵌入式目標上檢查 sizeof(time_t);32 位 time_t 在 2038 年 1 月回繞
- 在多執行緒程式碼中使用 gmtime_r() 和 localtime_r() 以避免資料競爭
- strftime() 格式字串與地區設定無關;為互通性,優先用 %Y-%m-%d 而非 %x
- timegm() 不在 POSIX 中;在 Windows 上用 _mkgmtime(),或用 mktime() + UTC 偏移手動實作
轉換 Linux 時間:date(1)、time_t、gmtime、strftime
搜尋「convert linux time」或 Linux 時間戳記轉換通常指 shell 命令 date(1) 或下面的 C 函式庫 time_t 函式。Linux 把 Unix 時間戳記存為 time_t——舊系統上是有號 32 位(Y2038 限制),現代系統上是有號 64 位。
- Shell (GNU date):date -u -d @1700000000 — epoch → UTC 日期時間
- Shell (BSD/macOS):date -ur 1700000000 — 注意是 -r 而非 -d @
- Shell 目前 epoch:date +%s(秒),date +%s%3N(毫秒,僅 GNU)
- C 函式庫:time_t t = time(NULL); struct tm *u = gmtime(&t); strftime(buf, sizeof(buf), '%Y-%m-%dT%H:%M:%SZ', u);
- C 函式庫:timegm(struct tm *) 返回一個對應 UTC 日曆時間的 time_t
- POSIX time_t:有號 32 位在 2038-01-19 溢位;64 位 time_t 是現代的修復
Linux 紀元時間、Linux 時間戳記轉時間、線上 Linux 轉換器
變體:「linux epoch time」(Linux 上的 Unix 時間)、「linux timestamp to time」(epoch → 掛鐘)、「linux timestamp converter online」(本頁)。
- Linux epoch time / linux epoch time — 與 Unix 時間相同;目前值:date +%s
- Linux timestamp to time / linux timestamp to time — date -u -d @1700000000 返回 UTC 掛鐘時間
- Linux timestamp converter online / linux timestamp converter online — 本頁是 date(1) 的瀏覽器版等價物
FAQ
- 在 C 中 Unix 時間戳記應使用什麼型別?
- 秒精度用 time_t,需要儲存毫秒或奈秒時用 int64_t(來自 <stdint.h>)。避免使用 int 或 long,因為它們的大小依賴於平台。
- 在 C 中如何取得毫秒?
- 使用 <time.h> 的 clock_gettime(CLOCK_REALTIME, &ts)。結果在 ts.tv_sec(秒)和 ts.tv_nsec(奈秒)中。毫秒按 ts.tv_sec * 1000LL + ts.tv_nsec / 1000000 計算。