asctime, asctime_r, asctime_s
来自cppreference.com
| 定义于头文件 <time.h>
|
||
| char* asctime ( const struct tm* time_ptr ); |
(1) | |
| char* asctime_r( const struct tm* time_ptr, char* buf ); |
(2) | (C23 起) |
| errno_t asctime_s( char* buf, rsize_t bufsz, const struct tm* time_ptr ); |
(3) | (C11 起) |
1) 转换日历时间 tm 为以下固定的 25 字符表示形式: Www Mmm dd hh:mm:ss yyyy\n
Www——来自 time_ptr->tm_wday 的星期之日的三字母英文缩写,Mon、Tue、Wed、Thu、Fri、Sat、Sun之一。Mmm——来自 time_ptr->tm_mon 的月名的三字母英文缩写,Jan、Feb、Mar、Apr、May、Jun、Jul、Aug、Sep、Oct、Nov、Dec之一。dd——来自 timeptr->tm_mday 的 2 位月之日,如同由 sprintf 以 %2d 打印hh——来自 timeptr->tm_hour 的 2 位时,如同由 sprintf 以 %.2d 打印mm——来自 timeptr->tm_min 的 2 位分,如同由 sprintf 以 %.2d 打印ss——来自 timeptr->tm_sec 的 2 位秒,如同由 sprintf 以 %.2d 打印yyyy——来自 timeptr->tm_year + 1900 的 4 位年,如同由 sprintf 以 %4d 打印
若 *time_ptr 的任何成员在其正常范围外则行为未定义
若 time_ptr->tm_year 所指示的历年拥有多于 4 位数或小于 1000 年则行为未定义。
函数不支持本地化,且不能移除换行符。
函数修改静态存储且非线程安全。
2) 同 (1) ,除了写入消息到用户提供的存储
buf 中,保证它是空终止的。 若存储始于
buf 的可用大小小于 26 字节则行为未定义。3) 同 (1) ,除了写入消息到用户提供的存储
buf 中,保证它是空终止的。且在运行是检测下列错误并调用当前安装的制约处理函数:
-
buf或time_ptr为空指针 -
bufsz小于 26 或大于 RSIZE_MAX - 不是所有 *time_ptr 的成员都在其正常范围内
- time_ptr->tm_year 所指示的年小于 0 或大于 9999
-
- 同所有边界检查函数,
asctime_s仅若实现定义 __STDC_LIB_EXT1__ 且用户在包含time.h前定义 __STDC_WANT_LIB_EXT1__ 为整数常量 1 才保证可用。
参数
| time_ptr | - | 指向指定待打印时间的 tm 对象的指针 |
| buf | - | 指向用户提供的至少有 26 字节的缓冲区的指针 |
| bufsz | - | 用户提供的缓冲区大小 |
返回值
2)
buf ,在返回后指向保有如上描述的日期与时间的静态空终止字符串。3) 成功时为零,失败时为非零,并设置 buf[0] 为零(除非
buf 为空指针或 bufsz 为零或大于 RSIZE_MAX )。注解
asctime 返回指向静态数据的指针从而不是线程安全的。 POSIX 标记此函数为过时并推荐用 strftime 代替。 C 标准亦推荐用 strftime 代替 asctime 、 asctime_r 及 asctime_s 因为 strftime 更灵活且为本地环境相关。
POSIX 限制未定义行为仅为当输出字符串将会长于 25 个字符、当 timeptr->tm_wday 或 timeptr->tm_mon 不在期待范围内,或当 timeptr->tm_year 超出 INT_MAX-1990 。
一些实现处置 timeptr->tm_mday==0 为表示前一月的最后一日。
示例
运行此代码
可能的输出:
Tue May 26 21:51:50 2015 Tue May 26 21:51:50 2015
引用
- C17 standard (ISO/IEC 9899:2018):
- 7.27.2.1 The asctime function (p: 287)
- K.3.8.2.1 The asctime_s function (p: 453-454)
- C11 standard (ISO/IEC 9899:2011):
- 7.27.2.1 The asctime function (p: 392-393)
- K.3.8.2.1 The asctime_s function (p: 624-625)
- C99 standard (ISO/IEC 9899:1999):
- 7.23.3.1 The asctime function (p: 341-342)
- C89/C90 standard (ISO/IEC 9899:1990):
- 4.12.3.1 The asctime function
参阅
| (C23)(C11) |
将 struct time_t 对象转换成文本表示 (函数) |
将 struct tm 对象转换成自定义文本表示 (函数) |