asctime_s, _wasctime_s

转换 tm 机制为字符字符串。 这些功能是 asctime, _wasctime 的版本与安全增强的 CRT中的安全功能如中所述。

errno_t asctime_s( 
   char* buffer,
   size_t numberOfElements,
   const struct tm *_tm 
);
errno_t _wasctime_s( 
   wchar_t* buffer,
   size_t numberOfElements
   const struct tm *_tm 
);
template <size_t size>
errno_t asctime_s( 
   char (&buffer)[size],
   const struct tm *_tm 
); // C++ only
template <size_t size>
errno_t _wasctime_s( 
   wchar_t (&buffer)[size],
   const struct tm *_tm 
); // C++ only

参数

  • buffer
    [out] 若要存储字符字符串结果的缓冲区的指针。 此函数采用了指向具有 numberOfElements指定范围内的活动内存位置。

  • numberOfElements
    [in] 用于的缓冲区的大小存储结果。

  • _tm
    [in] 时间/日期结构。 此函数采用了指向有效的 structtm 对象。

返回值

零,如果成功。 如果出现错误,无效参数调用处理程序,如 参数验证所述。 如果执行允许继续,则返回值是错误代码。 错误代码。 ERRNO.H. 定义。 有关更多信息,请参见 errno常数。 为每个错误条件返回的实际错误代码如下表所示。

错误状态

buffer

numberOfElements

tm

Return

在 buffer的值

NULL

任意

任意

EINVAL

不修改

不是NULL (指向有效的内存)

0

任意

EINVAL

不修改

不是 NULL

0AMP_LT 范围 AMP_LT 26

任意

EINVAL

空字符串

不是 NULL

AMP_GT= 26

NULL

EINVAL

空字符串

不是 NULL

AMP_GT= 26

无效时间结构或不足时的元素的大小值

EINVAL

空字符串

备注

wasctime_s 的错误状态类似于 asctime_s ,以及大小限制在运行度量。

备注

asctime 函数转换为结构存储的时间转换为字符字符串。 _tm 值在调用通常获取到 gmtimelocaltime。 两个函数来填充 tm 结构,对于 TIME.H. 定义。

timeptr 成员

tm_hour

小时数自午夜 (0-23)

tm_isdst

正,如果夏时制实际上是;0,如果夏时制不起作用;负,如果夏时制的状态是未知的。 C 运行库假定实现的夏时制的计算美国的规则 (DST)。

tm_mday

日月 (1-31)

tm_min

分钟数小时 (0-59) 之后

tm_mon

月份 (0-11;一月 = 0)

tm_sec

秒数分钟 (0-59) 之后

tm_wday

每周日期 (0-6;sunday = 0)

tm_yday

日、 (0-365;一月 1 日 = 0 日)

tm_year

年份 (减号 1900 的当前年份)

转换后的字符字符串具体取决于本地时区设置也会调整。 有关定义时区环境和全局变量的信息,请参见 time, _time32, _time64_ftime, _ftime32, _ftime64localtime_s, _localtime32_s, _localtime64_s 功能有关配置本地时间和 _tzset 功能的信息。

asctime_s 产生的结果字符串只包含 26 个字符并具有以下形式 Wed Jan 02 02:03:55 1980\n\0。 使用 24 小时制。 所有字段都有一个固定宽度。 新行字符和在 null 字符占据将字符串中的前两个位置。 作为第二个参数传递的值应为大的至少为。 如果小于,错误代码, EINVAL,将返回。

_wasctime_sasctime_s的宽字符版本。 _wasctime_sasctime_s 否则具有相同的行为。

一般文本例程映射

TCHAR.H 实例

未定义的 _UNICODE _MBCS

定义的 _MBCS

定义的 _UNICODE

_tasctime_s

asctime_s

asctime_s

_wasctime_s

在 C++ 中,使用这些功能由模板重载简化;重载可以自动推断缓冲区长度,而无需指定范围参数。 有关更多信息,请参见 安全模板重载

要求

实例

必需的头

asctime_s

time.h

_wasctime_s

time.h 或 wchar.h

安全性

如果缓冲区指针不是 NULL ,并且指针不指向有效的缓冲区,该函数会复盖任何位置。 这也可能导致访问冲突。

,如果传入的范围参数大于缓冲区,的实际大小大于 缓冲区溢出 会发生此错误。

示例

使用 asctime_s 功能,此程序将个长整数 aclock将系统时,将其转换为结构 newtime 然后将其转换为输出的字符串形式,。

// crt_asctime_s.c
#include <time.h>
#include <stdio.h>

struct tm newtime;
__time32_t aclock;

int main( void )
{
   char buffer[32];
   errno_t errNum;
   _time32( &aclock );   // Get time in seconds.
   _localtime32_s( &newtime, &aclock );   // Convert time to struct tm form.

   // Print local time as a string.

   errNum = asctime_s(buffer, 32, &newtime);
   if (errNum)
   {
       printf("Error code: %d", (int)errNum);
       return 1;
   }
   printf( "Current date and time: %s", buffer );
   return 0;
}
  

.NET Framework 等效项

请参见

参考

时间线

ctime_s, _ctime32_s, _ctime64_s, _wctime_s, _wctime32_s, _wctime64_s

_ftime, _ftime32, _ftime64

gmtime_s, _gmtime32_s, _gmtime64_s

localtime_s, _localtime32_s, _localtime64_s

time, _time32, _time64

_tzset