asctime, _wasctime

tm 時間結構轉換成字元字串。 這些函式有更安全的版本可供使用;請參閱 asctime_s_wasctime_s

語法

char *asctime(
   const struct tm *timeptr
);
wchar_t *_wasctime(
   const struct tm *timeptr
);

參數

timeptr
時間/日期結構。

傳回值

asctime 會傳回字元字串結果的指標,_wasctime 會傳回寬字元字串結果的指標。 沒有錯誤傳回值。

備註

這些函式有更安全的版本可供使用;請參閱 asctime_s_wasctime_s

asctime 函式會將儲存為結構的時間轉換為字元字串。 值 timeptr 通常是從 呼叫 gmtimelocaltime 取得,這兩者都會傳回 TIME.H 中定義的結構指標 tm

timeptr 成員
tm_hour 午夜以來的小時 (0-23)
tm_isdst 如果日光節約時間生效,則為正數;如果日光節約時間無效,則為 0;如果日光節約時間的狀態未知,則為負數。 C 執行階段程式庫會在實作日光節約時間 (DST) 的計算時,使用美國的規則。
tm_mday 月日 (1-31)
tm_min 小時後幾分鐘 (0-59)
tm_mon 月 (0-11;January = 0)
tm_sec 分鐘後秒 (0-59)
tm_wday 一周中的一天 (0-6;星期日 = 0)
tm_yday 一年中的一天 (0-365;1 月 1 = 0)
tm_year 年份 (目前年份減去 1900 年)

如需設定當地時間的相關資訊,請參閱 time_ftimelocaltime 函式。 如需定義時區環境和全域變數的相關資訊,請參閱 函式 _tzset

asctime 所產生的字串結果剛好包含 26 個字元,且具有以下格式 Wed Jan 2 02:03:55 1980\n\0。 使用 24 小時制。 所有欄位都具有固定寬度。 新行字元和 Null 字元佔用字串的最後兩個位置。 asctime 使用單一的靜態配置緩衝區來容納傳回的字串。 每次呼叫此函式都會導致先前呼叫結果的終結。

_wasctime 是 的寬字元版本 asctime ,否則的行為與 asctime 相同。

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

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

泛型文字常式對應

TCHAR.H 常規 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_tasctime asctime asctime _wasctime

需求

常式 必要的標頭
asctime <time.h>
_wasctime <time.h><wchar.h>

範例

此程式會將系統時間放在長整數 aclock 中,將它轉譯為 結構 newtime ,然後使用 函式將它轉換成字串格式以供輸出 asctime

// crt_asctime.c
// compile with: /W3

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

int main( void )
{
    struct tm   *newTime;
    time_t      szClock;

    // Get time in seconds
    time( &szClock );

    // Convert time to struct tm form
    newTime = localtime( &szClock );

    // Print local time as a string.
    printf_s( "Current date and time: %s", asctime( newTime ) ); // C4996
    // Note: asctime is deprecated; consider using asctime_s instead
}
Current date and time: Sun Feb  3 11:38:58 2002

另請參閱

時間管理
ctime, _ctime32, _ctime64, _wctime, _wctime32, _wctime64
_ftime, _ftime32, _ftime64
gmtime, _gmtime32, _gmtime64
localtime, _localtime32, _localtime64
time, _time32, _time64
_tzset
asctime_s, _wasctime_s