localtime, _localtime32, _localtime64

時刻値を変換し、ローカル タイム ゾーンに合わせて修正します。 これらの関数のセキュリティを強化したバージョンを使用できます。「localtime_s_localtime32_s_localtime64_s」を参照してください。

構文

struct tm *localtime( const time_t *sourceTime );
struct tm *_localtime32( const __time32_t *sourceTime );
struct tm *_localtime64( const __time64_t *sourceTime );

パラメーター

sourceTime
格納されている時刻へのポインター。

戻り値

構造体の結果へのポインターを返すか、関数に渡された日付が次の場合は NULL を返します。

  • 1970 年 1 月 1 日午前 0 時より前。

  • UTC の 2038 年 1 月 19 日 3 時 14 分 07 秒より後 (_time32time32_t を使用)。

  • UTC の 3000 年 12 月 31 日 23 時 59 分 59 秒より後 (_time64__time64_t を使用)。

_localtime64 では __time64_t 構造体を使用し、協定世界時 (UTC) の 3000 年 12 月 31 日の 23 時 59 分 59 秒までの日付を表すことができます。それに対して、_localtime32 は、UTC の 2038 年 1 月 18 日の 23 時 59 分 59 秒までを表します。

localtime_localtime64 と評価されるインライン関数であり、time_t__time64_t と等価です。 コンパイラが time_t を古い 32 ビットの time_tとして解釈するよう強制する必要がある場合には、 _USE_32BIT_TIME_Tを定義します。 _USE_32BIT_TIME_T が原因で localtime 次の値に評価されます _localtime32。 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) を計算します。

解説

localtime 関数では、time_t 値として格納されている時間を変換して、その結果を tm 型の構造体として格納します。 long 型の値 sourceTime は、UTC の 1970 年 1 月 1 日午前 0 時 (00:00:00) からの経過秒数を表します。 この値は、多くの場合、関数から取得されます time

32 ビット バージョンおよび 64 ビット バージョンの gmtimemktimemkgmtimelocaltime の各関数はすべて、1 スレッドあたり tm 構造体を 1 つ使用して変換を行います。 これらのルーチンを呼び出すたびに、前の呼び出しの結果は破棄されます。

localtime は、ユーザーが最初にグローバル環境変数 TZ を設定している場合、ローカル タイム ゾーンに合わせて修正します。 TZ を設定すると、他の 3 つの環境変数 (_timezone_daylight_tzname) も自動的に設定されます。 変数がTZ設定されていない場合は、localtimeコントロール パネルの日付/時刻アプリケーションで指定されたタイム ゾーン情報の使用を試みます。 この情報を取得できない場合は、太平洋タイム ゾーンを示すPST8PDTが既定で使用されます。 これらの変数の説明については、_tzset を参照してください。 TZ は、Microsoft 拡張機能であり、localtime の ANSI 標準定義の一部ではありません。

Note

対象の環境によって、夏時間が有効かどうか判断されます。

これらの関数では、パラメーターの検証が行われます。 sourceTime が Null ポインターの場合、または sourceTime 値が負の場合、「パラメーターの検証」に説明されているように、これらの関数では無効なパラメーター ハンドラーが呼び出されます。 実行の継続が許可された場合、関数は NULL を返し、errnoEINVAL に設定します。

既定では、この関数のグローバル状態の適用対象は、アプリケーションになります。 この動作を変更するには、「CRT のグローバル状態」を参照してください

必要条件

ルーチンによって返される値 必須の C ヘッダー 必須の C++ ヘッダー
localtime, _localtime32, _localtime64 <time.h> <ctime> または <time.h>

互換性の詳細については、「 Compatibility」を参照してください。

// crt_localtime.cpp
// compile with: /W3
// This program uses _time64 to get the current time
// and then uses localtime64() 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;

    _time64( &long_time );             // Get time as 64-bit integer.
                                       // Convert to local time.
    newtime = _localtime64( &long_time ); // C4996
    // Note: _localtime64 deprecated; consider _localetime64_s

    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;

    char buff[30];
    asctime_s( buff, sizeof(buff), newtime );
    printf( "%.19s %s\n", buff, am_pm );
}
Tue Feb 12 10:05:58 AM

関連項目

時間管理
asctime, _wasctime
ctime, _ctime32, _ctime64, _wctime, _wctime32, _wctime64
_ftime, _ftime32, _ftime64
gmtime, _gmtime32, _gmtime64
localtime_s, _localtime32_s, _localtime64_s
time, _time32, _time64
_tzset