Home C&C++函数库 c++ 语法 程序源码 Linux C库

asctime

函数   <ctime>

char* asctime (const struct tm * timeptr);

转换tm结构为字符串

将timeptr指向的tm结构的内容解释为日历时间,并将其转换为包含人类可读的相应日期和时间版本的C-string。

返回的字符串格式如下:

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字符串,以人类可读的格式包含日期和时间信息。

返回值指向一个内部数组,该数组的有效性或值可能会被后续调用asctimectime所改变。

☣  示例



/* 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;
}

输出:
The current date/time is: Wed Feb 13 15:46:11 2013

↭  数据竞争



函数访问timeptr所指向的对象。

函数也访问和修改一个共享的内部缓冲区,这可能会导致对asctimectime的并发调用的数据竞争。 一些库提供了避免这种数据竞争的替代函数:asctime_r(不可移植)。

❆  异常(c++)



无抛出保证:此函数从不抛出异常。

🍄  另请参阅



ctime 将time_t值转换为字符串(function )
gmtime 将time_t转换为tm作为UTC时间(function )
localtime 将time_t转换为tm作为当地时间(function )
time 获取当前时间(function )

联系我们 免责声明 关于CandCplus 网站地图