time_t 時刻値を tm 構造体に変換し、ローカル タイム ゾーンに合わせて修正します。 これらの関数は、「CRT のlocaltimeで説明されているように、セキュリティが強化された_localtime32、_localtime64、のバージョンです。
構文
errno_t localtime_s( // See note in remarks section about linkage
struct tm* const tmDest,
time_t const* const sourceTime
);
errno_t _localtime32_s(
struct tm* tmDest,
__time32_t const* sourceTime
);
errno_t _localtime64_s(
struct tm* tmDest,
__time64_t const* sourceTime
);
パラメーター
tmDest
目的の情報を格納する時間構造体へのポインター。
sourceTime
格納されている時刻へのポインター。
戻り値
正常終了した場合は 0。 エラーが発生した場合、戻り値はエラー コードです。 エラー コードは、Errno.h で定義されています。 これらのエラーの一覧については、「errno」を参照してください。
エラー条件
tmDest |
sourceTime |
戻り値 |
tmDest の値 |
無効なパラメーター ハンドラーを呼び出す |
|---|---|---|---|---|
NULL |
任意 | EINVAL |
変更されていません | はい |
NULL ではありません (有効なメモリを指します) |
NULL |
EINVAL |
すべてのフィールドが -1 に設定される | はい |
NULL ではありません (有効なメモリを指します) |
0 より小さいか、または _MAX__TIME64_T を超えている |
EINVAL |
すべてのフィールドが -1 に設定される | いいえ |
「パラメーターの検証」で説明されているように、最初の 2 つのエラー条件により、無効なパラメーター ハンドラーが呼び出されます。 実行の継続が許可された場合、これらの関数は errno を EINVAL に設定し、EINVAL を返します。
解説
localtime_s 関数では、time_t 値として格納されている時間を変換して、その結果を tm 型の構造体として格納します。
time_t 型の値 sourceTime は、UTC の 1970 年 1 月 1 日午前 0 時 (00:00:00) からの経過秒数を表します。 この値は、多くの場合、 time 関数から取得されます。
localtime_s は、ユーザーが最初にグローバル環境変数 TZ を設定している場合、ローカル タイム ゾーンに合わせて修正します。
TZ を設定すると、他の 3 つの環境変数 (_timezone、_daylight、_tzname) も自動的に設定されます。
TZ変数が設定されていない場合、localtime_sは、コントロール パネルの日付/時刻アプリケーションで指定されたタイム ゾーン情報の使用を試みます。 この情報を取得できない場合は、太平洋タイム ゾーンを示すPST8PDTが既定で使用されます。 これらの変数の説明については、_tzset を参照してください。
TZ は、Microsoft 拡張機能であり、localtime の ANSI 標準定義の一部ではありません。
Note
対象の環境によって、夏時間が有効かどうか判断されます。
_localtime64_s 構造体を使用する __time64_t は、UTC の 3001 年 1 月 18 日の 23 時 59 分 59 秒までの日付を表すことができます。それに対して、_localtime32_s は、UTC の 2038 年 1 月 18 日の 23 時 59 分 59 秒までしか表すことができません。
localtime_s は _localtime64_s と評価されるインライン関数であり、time_t は __time64_t と等価です。
time_tを古い 32 ビット time_tとしてコンパイラに強制的に解釈させる必要がある場合は、_USE_32BIT_TIME_Tを定義できます。これにより、localtime_sが_localtime32_sに評価されます。 2038 年 1 月 18 日以降にアプリケーションが失敗する可能性があり、64 ビット プラットフォームでは許可されないため、 _USE_32BIT_TIME_Tしないことをお勧めします。
tm 型の構造体のフィールドには、次の値が格納されます。各値は 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 |
夏時間が有効な場合は正の値。夏時間が有効でない場合は 0。夏時間の状態が不明な場合は負の値。 |
TZ 環境変数が設定されている場合、C ランタイム ライブラリでは、米国に適合した規則を前提に夏時間 (DST) を計算します。
既定では、この関数のグローバル状態の適用対象は、アプリケーションになります。 この動作を変更するには、「CRT でのグローバル状態」を参照してください。
Note
Windows SDK バージョン 10.0.26100.6901 と Visual Studio 2026 以降を一緒に使用する場合、 localtime_s は static inline されなくなります (内部リンケージ)。 代わりに、 inline (外部リンケージ) です。
前の動作に戻すには、CRT ヘッダーを含める前に #define _STATIC_INLINE_UCRT_FUNCTIONS=1 します。 既定では、_STATIC_INLINE_UCRT_FUNCTIONS は 0 に設定されています。
この変更により、UCRT が C++ 標準に準拠し、C++ モジュールとの互換性が向上します。
要件
| ルーチンによって返される値 | 必須の C ヘッダー | 必須の C++ ヘッダー |
|---|---|---|
localtime_s、 _localtime32_s、 _localtime64_s |
<time.h> |
<ctime> または <time.h> |
互換性の詳細については、「 Compatibility」を参照してください。
例
// crt_localtime_s.c
// This program uses _time64 to get the current time
// and then uses _localtime64_s() to convert this time to a structure
// representing the local time. The program converts the result
// from a 24-hour clock to a 12-hour clock and determines the
// proper extension (AM or PM).
#include <stdio.h>
#include <string.h>
#include <time.h>
int main( void )
{
struct tm newtime;
char am_pm[] = "AM";
__time64_t long_time;
char timebuf[26];
errno_t err;
// Get time as 64-bit integer.
_time64( &long_time );
// Convert to local time.
err = _localtime64_s( &newtime, &long_time );
if (err)
{
printf("Invalid argument to _localtime64_s.");
exit(1);
}
if( newtime.tm_hour > 12 ) // Set up extension.
strcpy_s( am_pm, sizeof(am_pm), "PM" );
if( newtime.tm_hour > 12 ) // Convert from 24-hour
newtime.tm_hour -= 12; // to 12-hour clock.
if( newtime.tm_hour == 0 ) // Set hour to 12 if midnight.
newtime.tm_hour = 12;
// Convert to an ASCII representation.
err = asctime_s(timebuf, 26, &newtime);
if (err)
{
printf("Invalid argument to asctime_s.");
exit(1);
}
printf( "%.19s %s\n", timebuf, am_pm );
}
Fri Apr 25 01:19:27 PM
関連項目
時間管理
asctime_s, _wasctime_s
ctime、 _ctime32、 _ctime64、 _wctime、 _wctime32、 _wctime64
_ftime、 _ftime32、 _ftime64
gmtime_s、 _gmtime32_s、 _gmtime64_s
localtime、 _localtime32、 _localtime64
time、 _time32、 _time64
_tzset