C 庫函數 - asctime()
描述
C 庫函數 char *asctime(const struct tm *timeptr) 返回一個指向字串的指針,它代表了結構 struct timeptr 的日期和時間。
聲明
下麵是 asctime() 函數的聲明。
char *asctime(const struct tm *timeptr)
參數
timeptr 是指向 tm 結構的指針,包含了分解為如下各部分的日曆時間:
struct tm { int tm_sec; /* 秒,範圍從 0 到 59 */ int tm_min; /* 分,範圍從 0 到 59 */ int tm_hour; /* 小時,範圍從 0 到 23 */ int tm_mday; /* 一月中的第幾天,範圍從 1 到 31 */ int tm_mon; /* 月份,範圍從 0 到 11 */ int tm_year; /* 自 1900 起的年數 */ int tm_wday; /* 一周中的第幾天,範圍從 0 到 6 */ int tm_yday; /* 一年中的第幾天,範圍從 0 到 365 */ int tm_isdst; /* 夏令時 */ };
返回值
該函數返回一個 C 字串,包含了可讀格式的日期和時間資訊 Www Mmm dd hh:mm:ss yyyy,其中,Www 表示星期幾,Mmm 是以字母表示的月份,dd 表示一月中的第幾天,hh:mm:ss 表示時間,yyyy 表示年份。
實例
下麵的實例演示了 asctime() 函數的用法。
實例
#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
struct tm t;
t.tm_sec = 10;
t.tm_min = 10;
t.tm_hour = 6;
t.tm_mday = 25;
t.tm_mon = 2;
t.tm_year = 89;
t.tm_wday = 6;
puts(asctime(&t));
return(0);
}
#include <string.h>
#include <time.h>
int main()
{
struct tm t;
t.tm_sec = 10;
t.tm_min = 10;
t.tm_hour = 6;
t.tm_mday = 25;
t.tm_mon = 2;
t.tm_year = 89;
t.tm_wday = 6;
puts(asctime(&t));
return(0);
}
讓我們編譯並運行上面的程式,這將產生以下結果:
Sat Mar 25 06:10:10 1989