Share via


localtime, _localtime32, _localtime64

轉換時間值,並依本地時區進行校正。 這些函式有更安全的版本可供使用;請參閱 、、 _localtime64_s_localtime32_slocaltime_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 日午夜之前。

  • 2038 年 1 月 19 日 03:14:07 UTC 之後 (使用_time32time32_t)。

  • 3000 年 12 月 31 日 23:59:59 UTC 之後 (使用_time64__time64_t)。

使用 __time64_t 結構的_localtime64,允許表示至國際標準時間 (UTC) 3000 年 12 月 31 日 23:59:59 為止的日期,而 _localtime32 則表示至 2038 年 1 月 18 日23:59:59 UTC為止的日期。

localtime 是評估為 _localtime64 的內嵌函式,而 time_t 相當於 __time64_t。 如果您要強制編譯器將 time_t 解譯為舊的 32 位元 time_t,您可以定義 _USE_32BIT_TIME_T_USE_32BIT_TIME_T 會導致 localtime 評估為 _localtime32 。 不建議使用 _USE_32BIT_TIME_T ,因為您的應用程式可能會在 2038 年 1 月 18 日之後失敗,而且 64 位平臺上不允許它。

結構類型的 tm 欄位會儲存下列值,每個值都是 int

欄位 描述
tm_sec 分鐘後幾秒(0 - 59)。
tm_min 小時後分鐘 (0 - 59)。
tm_hour 午夜(0 - 23)以來的小時。
tm_mday 月日 (1 - 31)。
tm_mon 月 (0 - 11;January = 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 別 的結構中。 longsourceTime 代表從 1970 年 1 月 1 日 UTC 午夜 (00: 00:00) 以來經過的秒數。 此值通常會從 函 time 式取得。

32 位元和 64 位元版本的 gmtimemktimemkgmtime,和 localtime 進行轉換時每個執行緒都使用單一的 tm 結構。 每呼叫其中一個此類常式會導致先前呼叫結果的終結。

如果使用者第一次設定全域環境變數 TZlocaltime 會為本地時區進行校正。 當已設定 TZ 時,其他三個環境變數 (_timezone_daylight_tzname) 也會自動設定。 TZ如果未設定變數, localtime 請嘗試使用 主控台 中日期/時間應用程式中指定的時區資訊。 如果無法取得這項資訊,預設會使用表示太平洋時區的PST8PDT。 如需這些變數的描述,請參閱 _tzsetTZ 是 Microsoft 延伸模組,且並不屬於 ANSI 標準定義的 localtime

注意

目標環境應該嘗試判斷日光節約時間是否生效。

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

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

需求

常式 必要的 C 標頭 必要的 C++ 標頭
localtime, _localtime32, _localtime64 <time.h> <ctime><time.h>

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

範例

// 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