time_t 時刻値を tm 構造体に変換します。 これらの関数のセキュリティを強化したバージョンを使用できます。「gmtime_s、_gmtime32_s、_gmtime64_s」を参照してください。
構文
struct tm *gmtime( const time_t *sourceTime ); // See note in remarks section about linkage
struct tm *_gmtime32( const __time32_t *sourceTime );
struct tm *_gmtime64( const __time64_t *sourceTime );
パラメーター
sourceTime
格納されている時刻へのポインター。 時刻は、世界協定時刻 (UTC: Coordinated Universal Time) の 1970 年 1 月 1 日の深夜 00:00:00 から経過した時間 (秒単位) を表します。
戻り値
tm 型の構造体へのポインター。 返された構造体の各フィールドには、sourceTime 引数を現地時刻ではなく UTC で評価した値が格納されています。 構造体の各フィールドは 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 ビット バージョンの gmtime、mktime、mkgmtime、localtime の各関数はすべて、1 スレッドあたり共通の tm 構造体を 1 つ使用して変換を行います。 これらの関数を呼び出すたびに、前の呼び出しの結果は破棄されます。
sourceTime が 1970 年 1 月 1 日の深夜 0 時よりも前の日付を示している場合、gmtime は NULL を返します。 エラーの戻り値はありません。
_gmtime64 構造体を使用する __time64_t では、UTC の 3000 年 12 月 31 日の 23 時 59 分 59 秒までの日付を表すことができます。
_gmtime32 では、UTC の 2038 年 1 月 18 日の 23 時 59 分 59 秒までの日付のみを表します。 これらの関数の日付範囲の下限は、どちらも 1970 年 1 月 1 日の午前 0 時です。
gmtime は _gmtime64 に評価されるインライン関数であり、time_t は __time64_t が定義されている場合を除き _USE_32BIT_TIME_T と等価です。 コンパイラに time_t を従来の 32 ビットの time_t として解釈させる必要がある場合は、_USE_32BIT_TIME_T を定義できますが、そのためには gmtime を _gmtime32 にインライン展開し、time_t を __time32_t として定義します。 64 ビット プラットフォームでは許可されていないため、_USE_32BIT_TIME_T を使用することはお勧めしません。 いずれにしても、2038 年 1 月 18 日より後にアプリケーションでエラーが発生する可能性があります。
これらの関数では、パラメーターの検証が行われます。
sourceTime が NULL ポインターの場合、または sourceTime 値が負の場合、「パラメーターの検証」に説明されているように、これらの関数では無効なパラメーター ハンドラーが呼び出されます。 実行の継続が許可された場合、関数は NULL を返し、errno を EINVAL に設定します。
解説
_gmtime32 関数では、sourceTime 値を展開して、tm に定義され、静的に割り当てられた TIME.H 型の構造体に保存します。
sourceTime 値は、通常は time 関数の呼び出しにより取得されます。
既定では、この関数のグローバル状態の適用対象は、アプリケーションになります。 この動作を変更するには、「CRT でのグローバル状態」を参照してください。
注
Windows SDK バージョン 10.0.26100.6901 と Visual Studio 2026 以降を一緒に使用する場合、 gmtime は static inline されなくなります (内部リンケージ)。 代わりに、 inline (外部リンケージ) です。
前の動作に戻すには、CRT ヘッダーを含める前に #define _STATIC_INLINE_UCRT_FUNCTIONS=1 します。 既定では、_STATIC_INLINE_UCRT_FUNCTIONS は 0 に設定されています。
この変更により、UCRT が C++ 標準に準拠し、C++ モジュールとの互換性が向上します。
要件
| ルーチンによって返される値 | 必須の C ヘッダー | 必須の C++ ヘッダー |
|---|---|---|
gmtime, _gmtime32, _gmtime64 |
<time.h> |
<ctime> または <time.h> |
互換性の詳細については、「 Compatibility」を参照してください。
例
// 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( <ime );
// Obtain coordinated universal time:
newtime = _gmtime64( <ime ); // 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
関連項目
時間管理
$
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