C++ Reference: Standard C++ Library reference: C Library: ctime: asctime

C++官网参考链接:https://cplusplus.com/reference/ctime/asctime/

函数
<ctime>
asctime
char* asctime (const struct tm * timeptr);
将tm结构转换为字符串
timeptr指向的tm结构的内容解释为日历时间,并将其转换为包含相应日期和时间的人类可读版本的C字符串。
返回的字符串格式如下: 
Www Mmm dd hh:mm:ss yyyy
其中Www是工作日,Mmm是月份(用字母表示),dd是月份的日期,hh:mm:ss是时间,yyyy是年份。
字符串后面跟着一个换行字符('\n'),并以空字符结束。
它的定义行为等价于:
char* asctime(const struct tm *timeptr)
{
  static const char wday_name[][4] = {
    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  };
  static const char mon_name[][4] = {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  };
  static char result[26];
  sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
    wday_name[timeptr->tm_wday],
    mon_name[timeptr->tm_mon],
    timeptr->tm_mday, timeptr->tm_hour,
    timeptr->tm_min, timeptr->tm_sec,
    1900 + timeptr->tm_year);
  return result;
}
有关自定义日期格式的替代选项,请参阅strftime

形参 
timeptr
指向tm结构的指针,该结构 (参见struct tm)包含分解为其组件的日历时间。

返回值
一个C字符串,以人类可读的格式包含日期和时间信息。
返回值指向一个内部数组,其有效性或值可能被后续对asctime或ctime的调用所改变。 

用例
/* asctime example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, time, localtime, asctime */

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "The current date/time is: %s", asctime (timeinfo) );

  return 0;
}
输出:

数据竞争
该函数访问timeptr所指向的对象。
该函数还访问和修改共享内部缓冲区,这可能导致同时调用asctime或ctime时的数据竞争。有些库提供了避免这种数据竞争的替代函数:asctime_r(不可移植)。

异常(C++) 
无抛出保证:此函数从不抛出异常。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_40186813

你的能量无可限量。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值