1834

9 分钟

#C 语言标准库函数 asctime

/********************************************* * @brief 将日历时间 struct tm 转换为文本 * @param timeptr 要被转换的时间 * @return 转换后的时间戳 ********************************************/ char* asctime(const struct tm* timeptr);

说明

将日历时间从 tm 转换为文本形式,格式为 Www Mmm dd hh:mm:ss yyyy\n(25 个字符)。

  • Www - 三个字母形式的英文星期名:Mon, Tue, Wed, Thu, Fri, Sat, Sun
  • Mmm - 三个字母形式的英文月份名:Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
  • dd - 两个数字的日期
  • hh - 两个数字的小时(24 小时制)
  • mm - 两个数字的分
  • ss - 两个数字的秒
  • yyyy - 四个数字的年

这个函数在 C23 中已被废弃,建议使用 strftimeasctime_s 函数替代。

参数

  • timeptr - 要被转换的日历时间

返回值

  • 返回转换后的文本(指向静态数据的指针)

#示例

#include <stdio.h> #include <time.h> int main(void) { struct tm tm = { .tm_year = 125, // 2025 年 .tm_mon = 4, // 5 月 .tm_mday = 15, .tm_hour = 5, .tm_min = 15, .tm_sec = 55, }; printf("%s", asctime(&tm)); return 0; }

运行结果:

Sun May 15 05:15:55 2025

#推荐阅读

#参考标准

  • C17 standard (ISO/IEC 9899:2018):
    • 7.27.2.1 The asctime function (p: 287)
  • C11 standard (ISO/IEC 9899:2011):
    • 7.27.2.1 The asctime function (p: 392-393)
  • 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

创建于 2025/10/16

更新于 2025/10/16