asctime ()- 將時間轉換為字串

格式

#include <time.h>
char *asctime(const struct tm *time);

語言層次

ANSI

安全執行緒

請改用 asctime_r()

說明

asctime() 函數會將時間 (儲存為 time所指向的結構) 轉換為字串。 您可以從對 gmtime()gmtime64()localtime()localtime64() 函數的呼叫取得 time 值。

asctime() 產生的字串結果正好包含 26 個字元,且格式如下:
   "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n"
下列是傳回的字串範例:
   Sat Jul 16 02:03:55 1994\n\0
or
   Sat Jul 16  2:03:55 1994\n\0

asctime() 函數使用 24 小時制格式。 日期縮寫為: SunMonTueWedThuFriSat。 月份縮寫為: JanFebMarAprMayJunJulAugSepOctNovDec。 所有欄位都具有常數寬度。 只有一個數字的日期前面會有零或空格。 換行字元 (\n) 和空值字元 (\0) 佔據字串的最後兩個位置。

時間和日期函數開始於 1970 年 1 月 1 日世界標準時間 00:00:00。

回覆值

asctime() 函數會傳回結果字串的指標。 如果函數不成功,則會傳回 NULL。
附註: asctime()ctime() 函數及其他時間函數可以使用一般靜態配置的緩衝區來保留傳回字串。 對其中一個函數的每一個呼叫可能會毀損前一個呼叫的結果。 asctime_r()ctime_r()gmtime_r()localtime_r() 函數不使用一般靜態配置的緩衝區來保留傳回字串。 如果需要重新進入,這些函數可以用來取代 asctime()ctime()gmtime()localtime() 函數。

範例

此範例會輪詢系統時鐘,並列印提供現行時間的訊息。
#include <time.h>
#include <stdio.h>
 
int main(void)
{
    struct tm *newtime;
    time_t ltime;
 
/* Get the time in seconds */
    time(&ltime);
/* Convert it to the structure tm */
    newtime = localtime(&ltime);
 
        /* Print the local time as a string */
    printf("The current date and time are %s",
             asctime(newtime));
}
 
/****************  Output should be similar to:  ******************
The current date and time are Fri Sep 16 13:29:51 1994
*/

相關資訊