共用方式為


ctime_s、 _ctime32_s、 _ctime64_s、 _wctime_s、 _wctime32_s、 _wctime64_s

將時間值轉換為字串,以及調整區域的本地時間設定。 這些是舊版 ctime、 _ctime64、 _wctime、 _wctime64 中所述的安全性增強功能與安全性功能,則在 CRT 中

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

參數

  • [] outbuffer
    必須足以容納 26 個字元。 變數的指標,字元的字串結果,或NULL如果:

    • time表示 1970 年 1 月 1 日午夜 UTC 之前的日期。

    • 如果您使用_ctime32_s或_wctime32_s和time代表 03: 14: 07 以後 2038 年 1 月 19 日之後的日期。

    • 如果您使用_ctime64_s或_wctime64_s和time表示 23: 59: 59,3000 年 12 月 31 UTC 之後的日期。

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

  • [in] numberOfElements
    緩衝區的大小。

  • [in] 在 time
    預存時間的指標。

傳回值

如果成功的話,則為零。 如果有不正確的參數受限於失敗時,不正確的參數處理常式會叫用,如所述參數驗證。 如果執行,則允許繼續執行,則會傳回錯誤碼。 錯誤代碼被定義在 ERRNO 中。H。 如需這些錯誤的清單,請參閱 errno。 下表顯示實際的錯誤碼,針對每個錯誤條件擲回。

錯誤狀況

buffer

numberOfElements

time

Return

在 [值buffer

NULL

任何

任何

EINVAL

不能修改

不NULL (指向有效的記憶體)

0

任何

EINVAL

不能修改

不NULL

0 < 大小 < 26

任何

EINVAL

空字串

不NULL

> = 26

NULL

EINVAL

空字串

不NULL

> = 26

< 0

EINVAL

空字串

備註

ctime_s函式將轉換的時間值儲存為 time_t 轉換為字元字串的結構。 time值通常取自呼叫時間,會傳回從午夜到現在所經過的秒數 (00: 00: 00)、 1970 年 1 月 1,國際標準時間 (UTC)。 傳回值的字串包含完全 26 個字元,其格式:

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

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

根據當地時間的區域設定時也調整轉換後的字元字串。 請參閱time, _ftime,以及 localtime32_s 有關如何設定當地時間的函式和 _tzset 函式定義的時區環境和全域變數的相關資訊。

_wctime32_s與_wctime64_s是寬字元版本_ctime32_s和_ctime64_s。 傳回為寬字元字串的指標。 Otherwise, _ctime64_s, _wctime32_s, and _wctime64_s behave identically to _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++ 中,使用這些函式已經過簡化的樣板的多載 ; 多載可以緩衝區長度自動推斷,而不必指定 size 引數。 如需詳細資訊,請參閱 安全範本多載

泛用文字常式對應

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

.NET Framework 對等用法

請參閱

參考

時間管理

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