函式
<ctime>
gmtime
struct tm * gmtime (const time_t * timer);
將 time_t 轉換為 UTC 時間的 tm 結構
使用 timer 所指向的值來填充一個 tm 結構,其值表示相應的時間,以協調世界時 (UTC)(即格林威治標準時間時區的時間)表示。
關於本地時間的替代方案,請參見 localtime。
返回值
一個指向 tm 結構的指標,其成員已填充為與 timer 的 UTC 時間表示相對應的值。
返回的值指向一個內部物件,其有效性或值可能會被後續任何對 gmtime 或 localtime 的呼叫所改變。
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
/* gmtime example */
#include <stdio.h> /* puts, printf */
#include <time.h> /* time_t, struct tm, time, gmtime */
#define MST (-7)
#define UTC (0)
#define CCT (+8)
int main ()
{
time_t rawtime;
struct tm * ptm;
time ( &rawtime );
ptm = gmtime ( &rawtime );
puts ("Current time around the World:");
printf ("Phoenix, AZ (U.S.) : %2d:%02d\n", (ptm->tm_hour+MST)%24, ptm->tm_min);
printf ("Reykjavik (Iceland) : %2d:%02d\n", (ptm->tm_hour+UTC)%24, ptm->tm_min);
printf ("Beijing (China) : %2d:%02d\n", (ptm->tm_hour+CCT)%24, ptm->tm_min);
return 0;
}
|
輸出
Current time around the World:
Phoenix, AZ (U.S.) : 8:22
Reykjavik (Iceland) : 15:22
Beijing (China) : 23:22
|
資料競爭
該函式訪問由 timer 指向的物件。
該函式還會訪問並修改一個共享的內部物件,這可能在併發呼叫 gmtime 和 localtime 時引入資料競爭。一些庫提供了一個替代函式來避免這種資料競爭:gmtime_r(非可移植)。
異常 (C++)
無異常保證:此函式從不丟擲異常。