_strtime_s、_wstrtime_s

复制当前时间到缓冲区。 _strtime, _wstrtimeCRT 中的安全功能所述。

errno_t _strtime_s(
   char *buffer,
   size_t numberOfElements
);
errno_t _wstrtime_s(
   wchar_t *buffer,
   size_t numberOfElements
);
template <size_t size>
errno_t _strtime_s(
   char (&buffer)[size]
); // C++ only
template <size_t size>
errno_t _wstrtime_s(
   wchar_t (&buffer)[size]
); // C++ only

参数

  • [out] buffer
    缓冲区的长度至少10个字节,其中写入时间。

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

返回值

如果成功,是0。

如果错误情况发生,则将调用无效参数处理程序,如 参数验证 所述。 如果失败,返回值是错误代码。 错误代码定义在ERRNO.H中;有关此函数发生的具体错误,请参见下表。 有关错误代码的更多信息,请参见 errno 常数

错误情况

buffer

numberOfElements

返回

buffer 的内容

NULL

(任意)

EINVAL

未被修改

非 NULL (指向有效的缓冲区)

0

EINVAL

未被修改

非 NULL (指向有效的缓冲区)

0 < size < 9

EINVAL

空字符串

非 NULL (指向有效的缓冲区)

Size > 9

0

按指定的备注格式化当前时间

安全问题

如果 numberOfElements 参数大于 9.,传入缓冲区无效的非空值会导致访问冲突。

为numberOfElements 大于缓冲区的实际大小的值会导致缓冲区溢出。

备注

_strtime 和 _wstrtime函数提供更安全版本。 _strtime_s 函数复制当前本地时间到由 timestr指向的缓冲区*。*时间格式为hh:mm:ss,其中hh 是以 24 小时形式表示的两位数字的小时数,mm 是表示分钟的两位数,ss 是表示秒的两位数。 例如,字符串 18:23:44 表示下午 6 点 23 分钟 44 秒钟。缓冲区长度必须至少为 9 字节;实际大小由第二个参数指定。

_wsetlocale_wstrtime 是 _strtime 的宽字符版本,_wstrtime 参数和 的返回值都是宽字符字符串。 否则这些函数具有相同行为。

在 C++ 中,使用这些函数由模板重载简化;重载可以自动推导出缓冲区长度 (不再需要指定大小参数),并且它们可以自动用以更新、更安全的对应物替换旧的、不安全的函数。 有关更多信息,请参见安全模板重载

通用文本程序映射:

TCHAR.H 例程

未定义的_UNICODE & _MBCS

已定义 _MBCS

已定义 _UNICODE

_tstrtime_s

_strtime_s

_strtime_s

_wstrtime_s

要求

例程

必需的标头

_strtime_s

<time.h>

_wstrtime_s

<time.h> or <wchar.h>

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

示例

// strtime_s.c

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

int main()
{
    char tmpbuf[9];
    errno_t err;

    // Set time zone from TZ environment variable. If TZ is not set,
    // the operating system is queried to obtain the default value 
    // for the variable. 
    //
    _tzset();

    // Display operating system-style date and time. 
    err = _strtime_s( tmpbuf, 9 );
    if (err)
    {
       printf("_strdate_s failed due to an invalid argument.");
      exit(1);
    }
    printf( "OS time:\t\t\t\t%s\n", tmpbuf );
    err = _strdate_s( tmpbuf, 9 );
    if (err)
    {
       printf("_strdate_s failed due to an invalid argument.");
       exit(1);
    }
    printf( "OS date:\t\t\t\t%s\n", tmpbuf );

}
  

.NET Framework 等效项

请参见

参考

时间管理

asctime_s、_wasctime_s

ctime_s、_ctime32_s、_ctime64_s、_wctime_s、_wctime32_s、_wctime64_s

gmtime_s、_gmtime32_s、_gmtime64_s

localtime_s、_localtime32_s、_localtime64_s

mktime、_mktime32、_mktime64

time、_time32、_time64

_tzset