函式
<ctime>

asctime

char* asctime (const struct tm * timeptr);
將 tm 結構轉換為字串
解釋由 timeptr 指向的 tm 結構的內容,並將其轉換為一個 C 字串,其中包含該日期和時間的易讀版本。

返回的字串格式如下:

Www Mmm dd hh:mm:ss yyyy


其中 Www 是星期幾,Mmm 是月份(字母表示),dd 是月份中的第幾天,hh:mm:ss 是時間,yyyy 是年份。

字串後跟一個換行符('\n')並以空字元終止。

其行為定義為等同於:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char* asctime(const struct tm *timeptr)
{
  static const char wday_name[][4] = {
    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  };
  static const char mon_name[][4] = {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  };
  static char result[26];
  sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
    wday_name[timeptr->tm_wday],
    mon_name[timeptr->tm_mon],
    timeptr->tm_mday, timeptr->tm_hour,
    timeptr->tm_min, timeptr->tm_sec,
    1900 + timeptr->tm_year);
  return result;
}

對於自定義日期格式的替代方法,請參閱 strftime

引數

timeptr
指向一個 tm 結構體的指標,該結構體包含一個分解為其各組成部分的日曆時間(請參見 struct tm)。

返回值

一個 C 字串,包含易讀格式的日期和時間資訊。

返回的值指向一個內部陣列,其有效性或值可能會被後續對 asctimectime 的任何呼叫所更改。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* asctime example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, time, localtime, asctime */

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "The current date/time is: %s", asctime (timeinfo) );

  return 0;
}

輸出

The current date/time is: Wed Feb 13 15:46:11 2013


資料競爭

該函式訪問 timeptr 所指向的物件。
該函式還會訪問並修改一個共享的內部緩衝區,這可能導致對 asctimectime 的併發呼叫發生資料競爭。某些庫提供了避免這種資料競爭的替代函式:asctime_r(不可移植)。

異常 (C++)

無異常保證:此函式從不丟擲異常。

另見