localtime_s
, _localtime32_s
, _localtime64_s
time_t
時刻値を tm
構造体に変換し、ローカル タイム ゾーンに合わせて修正します。 これらの関数は、CRT のlocaltime
_localtime64
_localtime32
セキュリティ機能の説明に従ってセキュリティが強化されたバージョンのバージョンです。
構文
errno_t localtime_s(
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 |
any | EINVAL |
Not modified | はい |
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
対象の環境によって、夏時間が有効かどうか判断されます。
__time64_t
構造体を使用する _localtime64_s
は、UTC の 3001 年 1 月 18 日の 23 時 59 分 59 秒までの日付を表すことができます。それに対して、_localtime32_s
は、UTC の 2038 年 1 月 18 日の 23 時 59 分 59 秒までしか表すことができません。
localtime_s
は _localtime64_s
と評価されるインライン関数であり、time_t
は __time64_t
と等価です。 コンパイラが古い 32 ビットtime_t
として解釈time_t
するように強制する必要がある場合は、次のように評価_localtime32_s
する原因localtime_s
となる定義_USE_32BIT_TIME_T
を行うことができます。 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 のグローバル状態」を参照してください。
必要条件
ルーチンによって返される値 | 必須の 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
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示