gmtime_gmtime32_gmtime64

time_t 時間值轉換成 tm 結構。 這些函式已有更安全的版本可用,請參閱 gmtime_s_gmtime32_s_gmtime64_s

語法

struct tm *gmtime( const time_t *sourceTime );
struct tm *_gmtime32( const __time32_t *sourceTime );
struct tm *_gmtime64( const __time64_t *sourceTime );

參數

sourceTime
預存時間的指標。 時間以國際標準時間 1970 年 1 月 1 日 (UTC) 午夜 (00: 00:00) 以來經過的秒代表。

傳回值

類型 tm 之結構的指標。 傳回結構的欄位以 UTC 來保存 sourceTime 引數的評估值,而非當地時間。 每個結構欄位的類型是 int,如下所示︰

欄位 描述
tm_sec 分鐘之後的秒數 (0 - 59)。
tm_min 小時之後的分鐘 (0 - 59)。
tm_hour 午夜後的小時 (0 - 23)。
tm_mday 月份的日期 (1 - 31)。
tm_mon 月份 (0 - 11;1 月 = 0)。
tm_year 年份 (目前年份減去 1900)。
tm_wday 週中的日 (0 - 6;星期日 = 0)。
tm_yday 年中的日 (0 - 365;1 月 1 日 = 0)。
tm_isdst gmtime 一律為 0。

32 位元和 64 位元版本的 gmtimemktimemkgmtimelocaltime 進行轉換時每個執行緒都使用單一的 tm 結構。 每次呼叫這些函式的其中之一時,會終結任何先前呼叫的結果。 如果 sourceTime 代表 1970 年 1 月 1 日午夜以前的日期,gmtime 會傳回 NULL。 不會傳回錯誤。

_gmtime64,使用 __time64_t 結構,可讓日期以 23:59:59、3000 年 12 月 31 日 UTC 表示。 _gmtime32 僅代表日期到 2038 年 1 月 18 日 23:59:59 UTC。 1970 年 1 月 1 日午夜是這兩個函式的日期範圍下限。

gmtime 是內嵌函式,其評估為 _gmtime64,且除非 _USE_32BIT_TIME_T 已定義,否則 time_t 相當於 __time64_t。 如果您必須強制編譯器將 time_t 解譯為舊的 32 位元 time_t,您可以定義 _USE_32BIT_TIME_T,但是這樣做會導致gmtime 內嵌至 _gmtime32,且 time_t 被定義為 __time32_t。 不建議使用 _USE_32BIT_TIME_T,因為它不允許在 64 位平台上使用。 在任何情況下,您的應用程式可能會在 2038 年 1 月 18 日之後失敗。

這些函式會驗證它們的參數。 如果 sourceTimeNULL 指標,或如果 sourceTime 值為負值,則這些函式會叫用無效的參數處理常式,如參數驗證中所述。 如果允許繼續執行,這些函式會傳回 NULL,並將 errno 設定為 EINVAL

備註

_gmtime32 函式會細分 sourceTime 值,並將它儲存在類型 tm 的結構中,如 TIME.H 中所定義。 sourceTime 的值通常取自對 time 函式的呼叫。

根據預設,此函式的全域狀態會限定於應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態

需求

常式 必要的 C 標頭 必要的 C++ 標頭
gmtime_gmtime32_gmtime64 <time.h> <ctime><time.h>

如需相容性詳細資訊,請參閱相容性

範例

// crt_gmtime.c
// compile with: /W3
// This program uses _gmtime64 to convert a long-
// integer representation of coordinated universal time
// to a structure named newtime, then uses asctime to
// convert this structure to an output string.

#include <time.h>
#include <stdio.h>

int main(void)
{
   struct tm *newtime;
   __int64 ltime;
   char buff[80];

   _time64( &ltime );

   // Obtain coordinated universal time:
   newtime = _gmtime64( &ltime ); // C4996
   // Note: _gmtime64 is deprecated; consider using _gmtime64_s
   asctime_s( buff, sizeof(buff), newtime );
   printf( "Coordinated universal time is %s\n", buff );
}
Coordinated universal time is Tue Feb 12 23:11:31 2002

另請參閱

時間管理
asctime, _wasctime
ctime, _ctime32, _ctime64, _wctime, _wctime32, _wctime64
_ftime_ftime32_ftime64
gmtime_s_gmtime32_s_gmtime64_s
localtime_localtime32_localtime64
_mkgmtime_mkgmtime32_mkgmtime64
mktime_mktime32_mktime64
time_time32_time64