ctime_s、_ctime32_s、_ctime64_s、_wctime_s、_wctime32_s、_wctime64_s

将时间值转换为字符串并调整为本地时区设置。 这些是 ctime,_ctime64,_wctime,_wctime64 的安全改进的版本,如 CRT 中的安全功能所述。

errno_t ctime_s( 
   char* buffer,
   size_t numberOfElements,
   const time_t *time 
);
errno_t _ctime32_s( 
   char* buffer,
   size_t numberOfElements,
   const __time32_t *time 
);
errno_t _ctime64_s( 
   char* buffer,
   size_t numberOfElements,
   const __time64_t *time )
;
errno_t _wctime_s( 
   wchar_t* buffer,
   size_t numberOfElements,
   const time_t *time 
);
errno_t _wctime32_s( 
   wchar_t* buffer,
   size_t numberOfElements,
   const __time32_t *time 
);
errno_t _wctime64_s( 
   wchar_t* buffer,
   size_t numberOfElements,
   const __time64_t *time 
);
template <size_t size>
errno_t _ctime32_s( 
   char (&buffer)[size],
   const __time32_t *time 
); // C++ only
template <size_t size>
errno_t _ctime64_s( 
   char (&buffer)[size],
   const __time64_t *time
); // C++ only
template <size_t size>
errno_t _wctime32_s( 
   wchar_t (&buffer)[size],
   const __time32_t *time 
); // C++ only
template <size_t size>
errno_t _wctime64_s( 
   wchar_t (&buffer)[size],
   const __time64_t *time 
); // C++ only

参数

  • [out] buffer
    必须足够大以包含 26 个字符。 指向字符串结果的指针或 NULL,如果:

    • time 表示在1970 年 1 月 1 日,午夜,UTC。

    • 如果使用 _ctime32_s 或 _wctime32_s, time 表示 2038 年 1 月 19 日 03:14:07之后的日期。

    • 如果使用 _ctime64_s 或 _wctime64_s, time 表示 3000,12月 31 日, 23:59:59, UTC之后的日期。

    • 如果使用 _ctime_s 或 _wctime_s,这些函数被打包给之前的函数。 请参见“备注”部分。

  • [in] numberOfElements
    缓冲区的大小。

  • [in] time
    存储时间的指针。

返回值

如果成功,是0。 如果由于一个或多个无效参数出现错误,则调用无效参数处理程序,如 参数验证所述。 如果允许代码继续执行,则返回错误代码。 错误代码在ERRNO.H 中定义;有关这些错误列表,请参见 errno。 下表中显示了每个错误状态抛出的实际错误代码。

错误情况

buffer

numberOfElements

time

返回

buffer中的值

NULL

any

any

EINVAL

未被修改

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

0

any

EINVAL

未被修改

非 NULL

0< 尺寸 < 26

any

EINVAL

空字符串

非 NULL

>= 26

NULL

EINVAL

空字符串

非 NULL

>= 26

< 0

EINVAL

空字符串

备注

ctime_s 函数转换时间值存储为 time_t 值到字符串。 time 值一直通过调用 时间获得,返回从(00:00 :00),1970 年 1 月 1 日,协调通用时间 (UTC)开始的秒数。 返回字符串正好包含 26 个字符并具有以下形式:

Wed Jan 02 02:03:55 1980\n\0

使用 24 小时制。 所有字段都具有一个常数的宽度。 换行符 ("\n")和空字符 (“\0") 占用字符串中的后两个位置。

转换的字符串本地根据的时区设置也会调整。 请参见 time、_ftimelocaltime32_s 有关配置本地时间的函数,以及 _tzset 有关环境定义时区和全局变量信息的函数。

_wctime32_s 和 _wctime64_s是_ctime32_s 和_ctime64_s的宽字符版本;返回宽字符串指针。 除此以外,_ctime64_s , _wctime32_s和 _wctime64_s的行为与_ctime32_s完全相同。

ctime_s 是个内联函数,计算结果为 _ctime64_s,并且,time_t 与 __time64_t等效。 如果你需要强制编译器将 time_t 编译为旧 的32 位 time_t,你可以将 _USE_32BIT_TIME_T定义。 这样做将导致 ctime_s 计算结果为 _ctime32_s。 因为该应用程序能在 2038 年 1 月 18 日之后可能失败,而且64 位平台上是不允许的,所以不建议这样做。

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

一般文本例程映射

TCHAR.H 例程

未定义的 _UNICODE 和 _MBCS

已定义 _MBCS

已定义 _UNICODE

_tctime_s

ctime_s

ctime_s

_wctime_s

_tctime32_s

_ctime32_s

_ctime32_s

_wctime32_s

_tctime64_s

_ctime64_s

_ctime64_s

_wctime64_s

要求

例程

必需的标头

ctime_s,

_ctime32_s,

_ctime64_s

<time.h>

_wctime_s,

_wctime32_s,

_wctime64_s

<time.h> or <wchar.h>

有关其他兼容性信息,请参见“简介”中的兼容性

C 运行时库的所有版本。

示例

// crt_wctime_s.c
/* This program gets the current
 * time in time_t form and then uses _wctime_s to
 * display the time in string form.
 */

#include <time.h>
#include <stdio.h>

#define SIZE 26

int main( void )
{
   time_t ltime;
   wchar_t buf[SIZE];
   errno_t err;

   time( &ltime );

  
   err = _wctime_s( buf, SIZE, &ltime );
   if (err != 0)
   {
      printf("Invalid Arguments for _wctime_s. Error Code: %d\n", err);
   }
   wprintf_s( L"The time is %s\n", buf );
}

示例输出

The time is Fri Apr 25 13:03:39 2003

.NET Framework 等效项

请参见

参考

时间管理

asctime_s、_wasctime_s

ctime、_ctime32、_ctime64、_wctime、_wctime32、_wctime64

_ftime、_ftime32、_ftime64

gmtime_s、_gmtime32_s、_gmtime64_s

localtime_s、_localtime32_s、_localtime64_s

time、_time32、_time64