asctime() 函数在 time.h 头文件中定义。此函数返回系统定义的本地时间。实际上,asctime() 函数返回一个指向字符串的指针,该字符串表示结构体 'timeptr' 参数的时间和日期。
char *asctime(const struct tm *timeptr); #where second should be a pointer
asctime() 函数接受一个参数 'timeptr',它是一个指向包含日历时间的结构体的指针。日历时间的组成部分是:
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 */
};
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| timeptr | 指向要转换的 tm 对象的指针 | 必需 |
该函数将日期和时间信息作为字符串返回,格式为 **Www Mmm dd hh:mm:ss yyyy**。
| 输入 | 返回值 |
|---|---|
| 如果参数 | 日历时间 |
#include <stdio.h>
#include <time.h>
int main()
{
struct tm* p;
time_t ltm;
ltm = time(NULL);
p = localtime(<);
// using the asctime() function
printf("%s", asctime(p));
return 0;
}
输出
Wed Aug 14 04:21:25 2019
#include <stdio.h>
#include <time.h>
int main (){
struct tm t;
t.tm_sec = 10;
t.tm_min = 10;
t.tm_hour = 6; t.tm_m
t.tm_mday = 25;
t.tm_year = 89;
t.tm_wday = 6;
puts(asctime(&t));
return(0);
}
输出
Sat Mar 25 06:10:10 1989