當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C語言 ctime用法及代碼示例

C語言time頭文件(time.h)中ctime函數的用法及代碼示例。

用法:

char* ctime (const time_t * timer);
將time_t值轉換為字符串
解釋由所指向的值timer作為日曆時間,並將其轉換為C-string,其中包含以當地時間表示的相應時間和日期的人類可讀版本。

返回的字符串具有以下格式:

Www Mmm dd hh:mm:ss yyyy

在哪裏Www是工作日Mmm一個月(以字母為單位),dd一個月中的某天,hh:mm:ss時間,和yyyy那一年。

字符串後跟一個new-line字符('\n'),並以null-character終止。

此函數等效於:
/* 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;
}


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

參數

timer
指向類型的對象的指針time_t包含一個時間值。
time_t是基本麵的別名算術類型能夠代表函數返回的時間time

返回值

C-string包含人類可讀格式的日期和時間信息。

返回的值指向一個內部數組,該數組的有效性或值可通過隨後的任何調用來更改asctime或者ctime

示例

/* 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



相關用法


注:本文由純淨天空篩選整理自C標準庫大神的英文原創作品 C ctime function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。