time_t 時間值轉換為 tm 結構,並更正當地時區。 這些函式是的版本 localtime _localtime32 _localtime64 具有CRT 中安全性功能中所述 的安全性增強功能。

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
預存時間的指標。

如果成功,則為零。 如果失敗,傳回的值會是錯誤碼。 錯誤碼定義於 中 Errno.h。 如需這些錯誤的清單,請參閱 errno

tmDest sourceTime tmDest 中的值 叫用無效的參數處理常式

前兩個錯誤狀況都會叫用無效的參數處理常式,如參數驗證中所述。 如果允許繼續執行,這些函式會將 errno 設為 EINVAL,並傳回 EINVAL

localtime_s 式會轉換儲存為 time_t 值的時間,並將結果儲存在 型 tm別的結構中。 time_tsourceTime 代表從 1970 年 1 月 1 日 UTC 午夜 (00: 00:00) 以來經過的秒數。 此值通常會從函 time 式取得。

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

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

Microsoft 專屬 localtime_s 版本的簽名與 C 標準版本不同。 為了實現符合標準的變體,請在包含 <time.h>之前定義_CRT_USE_CONFORMING_ANNEX_K_TIME為非零值。

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

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

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

如果 TZ 環境變數已設定,C 執行階段程式庫將假設適用於美國之規則,以實作日光節約時間 (DST) 的計算。

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

當您同時使用 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++ 標頭
// 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