ctime_s, _ctime32_s, _ctime64_s, _wctime_s, _wctime32_s, _wctime64_s

將時間值轉換為字串並針對當地時區設定調整。 這些函式是 、 _wctime_ctime64 、 的版本 ctime_wctime64 具有 CRT 中安全性功能中所述 的安全性增強功能。

語法

errno_t ctime_s(
   char* buffer,
   size_t numberOfElements,
   const time_t *sourceTime
);
errno_t _ctime32_s(
   char* buffer,
   size_t numberOfElements,
   const __time32_t *sourceTime
);
errno_t _ctime64_s(
   char* buffer,
   size_t numberOfElements,
   const __time64_t *sourceTime )
;
errno_t _wctime_s(
   wchar_t* buffer,
   size_t numberOfElements,
   const time_t *sourceTime
);
errno_t _wctime32_s(
   wchar_t* buffer,
   size_t numberOfElements,
   const __time32_t *sourceTime
);
errno_t _wctime64_s(
   wchar_t* buffer,
   size_t numberOfElements,
   const __time64_t *sourceTime
);
template <size_t size>
errno_t _ctime32_s(
   char (&buffer)[size],
   const __time32_t *sourceTime
); // C++ only
template <size_t size>
errno_t _ctime64_s(
   char (&buffer)[size],
   const __time64_t *sourceTime
); // C++ only
template <size_t size>
errno_t _wctime32_s(
   wchar_t (&buffer)[size],
   const __time32_t *sourceTime
); // C++ only
template <size_t size>
errno_t _wctime64_s(
   wchar_t (&buffer)[size],
   const __time64_t *sourceTime
); // C++ only

參數

buffer
必須足以容納 26 個字元。 字元字串結果的指標,如果 NULL

  • sourceTime 代表 1970 年 1 月 1 日午夜 (UTC) 以前的日期。

  • 如果您使用 _ctime32_s_wctime32_ssourceTime 代表 2038 年 1 月 18 日 23:59:59 (UTC) 以後的日期。

  • 如果您使用 _ctime64_s_wctime64_ssourceTime 代表 3000 年 12 月 31 日 23:59:59 (UTC) 以後的日期。

  • 如果您使用 _ctime_s_wctime_s,這些函式是先前函式的包裝函式。 請參閱<備註>一節。

numberOfElements
緩衝區的大小。

sourceTime
預存時間的指標。

傳回值

如果成功,則為零。 如果因為參數無效而失敗,則會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行,則會傳回錯誤碼。 錯誤碼定義于 ERRNO 中。H;如需這些錯誤的清單,請參閱 errno 。 下表顯示每個錯誤條件所擲回的實際錯誤碼。

錯誤條件

buffer numberOfElements sourceTime 傳回 buffer 中的值
NULL 任意 任意 EINVAL 未修改
NULL (指向有效的記憶體) 0 任意 EINVAL 未修改
不是 NULL 0 < 大小 < 26 任意 EINVAL 空字串
不是 NULL >= 26 NULL EINVAL 空字串
不是 NULL >= 26 < 0 EINVAL 空字串

備註

ctime_s 式會將儲存為 time_t 結構的時間值轉換成字元字串。 值 sourceTime 通常是從 呼叫 time 取得,其會傳回自午夜 (00:00:00)之後經過的秒數,1970 年 1 月 1 日,國際標準時間 (UTC)。 傳回值字串剛好包含 26 個字元,且具有以下格式:

Wed Jan 2 02:03:55 1980\n\0

使用 24 小時制。 所有欄位都具有固定寬度。 新行字元 ('\n') 和 null 字元 ('\0') 佔用字串的最後兩個位置。

已轉換的字元字串也會根據當地時區設定調整。 如需設定當地時間的資訊,請參閱 time_ftimelocaltime 函式。 如需定義時區環境和全域變數的詳細資訊,請參閱 函式 _tzset

_wctime32_s_wctime64_s 是寬字元版本的 _ctime32_s_ctime64_s,並會傳回寬字元字串的指標。 否則,_ctime64_s_wctime32_s_wctime64_s 的行為與 _ctime32_s 相同。

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

在 C++ 中,使用這些函式已透過範本多載簡化;多載可自動推斷緩衝區長度,因而不需要指定大小引數。 如需詳細資訊,請參閱 保護範本多載

這些函式的偵錯程式庫版本會先將緩衝區填入0xFE。 若要停用此行為,請使用 _CrtSetDebugFillThreshold

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

泛型文字常式對應

TCHAR.H 常式 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_tctime_s ctime_s ctime_s _wctime_s
_tctime32_s _ctime32_s _ctime32_s _wctime32_s
_tctime64_s _ctime64_s _ctime64_s _wctime64_s

需求

常式 必要的標頭
ctime_s, _ctime32_s, _ctime64_s <time.h>
_wctime_s, _wctime32_s, _wctime64_s <time.h > 或 < wchar.h>

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

程式庫

所有版本的 C 執行階段程式庫

範例

// crt_wctime_s.c
// This program gets the current
// time in time_t form and then uses _wctime_s to
// display the time in string form.

#include <time.h>
#include <stdio.h>

#define SIZE 26

int main( void )
{
   time_t ltime;
   wchar_t buf[SIZE];
   errno_t err;

   time( &ltime );

   err = _wctime_s( buf, SIZE, &ltime );
   if (err != 0)
   {
      printf("Invalid Arguments for _wctime_s. Error Code: %d\n", err);
   }
   wprintf_s( L"The time is %s\n", buf );
}
The time is Fri Apr 25 13:03:39 2003

另請參閱

時間管理
asctime_s, _wasctime_s
ctime, _ctime32, _ctime64, _wctime, _wctime32, _wctime64
_ftime, _ftime32, _ftime64
gmtime_s, _gmtime32_s, _gmtime64_s
localtime_s, _localtime32_s, _localtime64_s
time, _time32, _time64