asctime() – C语言库函数
C库函数 char *asctime(const struct tm *timeptr) 返回一个指针,指向一个字符串,它表示的日期和时间结构struct timeptr。
声明
以下是asctime() 函数的声明。
char*asctime(conststruct tm *timeptr)
参数
timeptr 是指向 tm结构的指针包含一个日历时间分解成其组成部分,如下图所示:
struct tm {int tm_sec;/* seconds, range 0 to 59 */int tm_min;/* minutes, range 0 to 59 */int tm_hour;/* hours, range 0 to 23 */int tm_mday;/* day of the month, range 1 to 31 */int tm_mon;/* month, range 0 to 11 */int tm_year;/* The number of years since 1900 */int tm_wday;/* day of the week, range 0 to 6 */int tm_yday;/* day in the year, range 0 to 365 */int tm_isdst;/* daylight saving time */};
返回值
这个函数返回一个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);}
让我们编译和运行上面的程序,这将产生以下结果:
Sat Mar 25 06:10:10 1989