函式
<ctime>

ctime

char* ctime (const time_t * timer);
將 time_t 值轉換為字串
timer 指向的值解釋為日曆時間,並將其轉換為一個 C 字串,該字串包含對應時間日期的可讀版本(本地時間)。

返回的字串格式如下:

Www Mmm dd hh:mm:ss yyyy


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

字串後面會跟一個換行符 ('\n'),並以空字元結尾。

此函式等同於:
1
asctime(localtime(timer))

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

引數

timer
指向 time_t 型別物件的指標,該物件包含一個時間值。
time_t 是一個基礎算術型別的別名,能夠表示由函式 time 返回的時間。

返回值

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

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

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
/* ctime example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, time, ctime */

int main ()
{
  time_t rawtime;

  time (&rawtime);
  printf ("The current local time is: %s", ctime (&rawtime));

  return 0;
}

輸出

The current local time is: Wed Feb 13 16:06:10 2013


資料競爭

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

異常 (C++)

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

另見