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


C語言 asctime用法及代碼示例

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

用法:

char* asctime (const struct tm * timeptr);
將tm結構轉換為字符串
解釋內容tm指向的結構時間點作為日曆時間並將其轉換為包含相應日期和時間的人類可讀版本的C-string。

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

Www Mmm dd hh:mm:ss yyyy

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

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

它的定義行為等同於:
/* 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;
}


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

參數

timeptr
指向一個指針tm包含日曆時間的結構,該時間細分為各個部分(請參見struct tm)。

返回值

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

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

示例

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



相關用法


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