ctime
, _ctime32
, _ctime64
, _wctime
, _wctime32
, _wctime64
將時間值轉換為字串並針對當地時區設定調整。 這些函式有更安全的版本可供使用;請參閱 、、 _ctime32_s
、 _wctime_s
_ctime64_s
、 _wctime32_s
、 _wctime64_s
。 ctime_s
語法
char *ctime( const time_t *sourceTime );
char *_ctime32( const __time32_t *sourceTime );
char *_ctime64( const __time64_t *sourceTime );
wchar_t *_wctime( const time_t *sourceTime );
wchar_t *_wctime32( const __time32_t *sourceTime );
wchar_t *_wctime64( const __time64_t *sourceTime );
參數
sourceTime
要轉換之儲存時間的指標。
傳回值
字元字串結果的指標。 NULL
會在:
sourceTime
代表 1970 年 1 月 1 日午夜 (UTC) 以前的日期。_ctime32
您使用 或_wctime32
,並sourceTime
代表 2038 年 1 月 18 日 23:59:59 之後的日期。_ctime64
您可以使用 或_wctime64
,並sourceTime
代表 UTC 23:59:59、12 月 31 日、3000 年 12 月 31 日之後的日期。
ctime
是評估為 _ctime64
的內嵌函式,而 time_t
相當於 __time64_t
。 如果您要強制編譯器將 time_t
解譯為舊的 32 位元 time_t
,您可以定義 _USE_32BIT_TIME_T
。 這個宏會導致 ctime
評估為 _ctime32
。 不建議您使用它,因為您的應用程式可能會在 2038 年 1 月 18 日之後失敗,而且 64 位平臺上不允許它。
備註
函 ctime
式會將儲存為 time_t
值的時間值轉換成字元字串。 值 sourceTime
通常是從 呼叫 time
取得,其會傳回自午夜 (00:00:00)之後經過的秒數,1970 年 1 月 1 日,國際標準時間 (UTC)。 傳回值字串剛好包含 26 個字元,且具有以下格式:
Wed Jan 02 02:03:55 1980\n\0
使用 24 小時制。 所有欄位都具有固定寬度。 新行字元 ('\n') 和 null 字元 ('\0') 佔用字串的最後兩個位置。
已轉換的字元字串也會根據當地時區設定調整。 如需設定當地時間的資訊,請參閱 time
、 _ftime
和 localtime
函式。 如需定義時區環境和全域變數的詳細資訊,請參閱 函式 _tzset
。
呼叫 ctime
可修改 gmtime
和 localtime
函式所使用之單一靜態配置的緩衝區。 每呼叫其中一個此類常式會導致先前呼叫結果的終結。 ctime
會與 asctime
函式共用靜態緩衝區。 因此,呼叫 ctime
會終結任何先前對 asctime
、localtime
或 gmtime
的呼叫結果。
_wctime
和 _wctime64
是寬字元版本的 ctime
和 _ctime64
,並會傳回寬字元字串的指標。 否則,_ctime64
、_wctime
和 _wctime64
的行為與 ctime
相同。
這些函式會驗證它們的參數。 如果 sourceTime
是 Null 指標,或者 sourceTime
如果值為負數,則這些函式會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行,這些函式會傳回 NULL
,並將 errno
設定為 EINVAL
。
根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。
泛型文字常式對應
TCHAR.H 常式 | _UNICODE 和 _MBCS 未定義 |
_MBCS 定義 |
_UNICODE 定義 |
---|---|---|---|
_tctime |
ctime |
ctime |
_wctime |
_tctime32 |
_ctime32 |
_ctime32 |
_wctime32 |
_tctime64 |
_ctime64 |
_ctime64 |
_wctime64 |
需求
常式 | 必要的標頭 |
---|---|
ctime |
<time.h> |
_ctime32 |
<time.h> |
_ctime64 |
<time.h> |
_wctime |
<time.h > 或 < wchar.h> |
_wctime32 |
<time.h > 或 < wchar.h> |
_wctime64 |
<time.h > 或 < wchar.h> |
如需相容性詳細資訊,請參閱相容性。
範例
// crt_ctime64.c
// compile with: /W3
/* This program gets the current
* time in _time64_t form, then uses ctime to
* display the time in string form.
*/
#include <time.h>
#include <stdio.h>
int main( void )
{
__time64_t ltime;
_time64( <ime );
printf( "The time is %s\n", _ctime64( <ime ) ); // C4996
// Note: _ctime64 is deprecated; consider using _ctime64_s
}
The time is Wed Feb 13 16:04:43 2002
另請參閱
時間管理
asctime
, _wasctime
ctime_s
, _ctime32_s
, _ctime64_s
, _wctime_s
, _wctime32_s
, _wctime64_s
_ftime
, _ftime32
, _ftime64
gmtime
, _gmtime32
, _gmtime64
localtime
, _localtime32
, _localtime64
time
, _time32
, _time64
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應