_strtime_s、_wstrtime_s_strtime_s, _wstrtime_s

将当前时间复制到缓冲区。Copy the current time to a buffer. 这些版本的 _strtime、_wstrtime 具有安全性增强功能,如 CRT 中的安全性功能中所述。These are versions of _strtime, _wstrtime with security enhancements as described in Security Features in the CRT.


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


至少为 10 个字节长的缓冲区,将在其中写入时间。A buffer, at least 10 bytes long, where the time will be written.

缓冲区的大小。The size of the buffer.

返回值Return Value

如果成功,则返回 0。Zero if successful.

如果出现错误条件,则将调用无效的参数处理程序,如参数验证中所述。If an error condition occurs, the invalid parameter handler is invoked, as described in Parameter Validation. 如果失败,则返回值为错误代码。The return value is an error code if there is a failure. 错误代码在 ERRNO.H 中定义;有关此函数生成的确切错误,请参阅下表。Error codes are defined in ERRNO.H; see the following table for the exact errors generated by this function. 有关错误代码的详细信息,请参阅 errno 常量For more information on error codes, see errno Constants.

错误条件Error Conditions

bufferbuffer numberOfElementsnumberOfElements 返回Return 内容缓冲区Contents of buffer
NULLNULL (任意数值)(any) EINVALEINVAL 未修改Not modified
NULL (指向有效的缓冲区)Not NULL (pointing to valid buffer) 00 EINVALEINVAL 未修改Not modified
NULL (指向有效的缓冲区)Not NULL (pointing to valid buffer) 0 < 大小 < 90 < size < 9 EINVALEINVAL 空字符串Empty string
NULL (指向有效的缓冲区)Not NULL (pointing to valid buffer) 大小 > 9Size > 9 00 注解中指定的当前时间格式Current time formatted as specified in the remarks

安全性问题Security Issues

传入无效非NULL值的缓冲区将导致访问冲突,如果numberOfElements参数大于 9。Passing in an invalid non-NULL value for the buffer will result in an access violation if the numberOfElements parameter is greater than 9.

为传递值numberOfElements大于缓冲区的实际大小将导致缓冲区溢出。Passing a value for numberOfElements that is greater than the actual size of the buffer will result in buffer overrun.


这些函数提供更多安全版本_strtime_wstrtimeThese functions provide more secure versions of _strtime and _wstrtime. _Strtime_s函数将当前的本地时间复制到由指向的缓冲区timestrThe _strtime_s function copies the current local time into the buffer pointed to by timestr. 时间格式为hh: mm: 其中hh是两位数字,表示 24 小时制的小时mm是两位数表示小时,并且分钟ss是两位数表示秒。The time is formatted as hh:mm:ss where hh is two digits representing the hour in 24-hour notation, mm is two digits representing the minutes past the hour, and ss is two digits representing seconds. 例如,字符串18:23:44表示 23 分 44 秒下午 6 点For example, the string 18:23:44 represents 23 minutes and 44 seconds past 6 P.M. 缓冲区必须至少为 9 个字节长;实际大小由第二个参数指定。The buffer must be at least 9 bytes long; the actual size is specified by the second parameter.

_wstrtime是宽字符版本 _strtime; 的自变量和返回值 _wstrtime都是宽字符字符串。_wstrtime is a wide-character version of _strtime; the argument and return value of _wstrtime are wide-character strings. 否则这些函数具有相同行为。These functions behave identically otherwise.

在 C++ 中,使用这些函数由模板重载简化;重载可以自动推导出缓冲区长度 (不再需要指定大小自变量),并且它们可以自动用以更新、更安全的对应物替换旧的、不安全的函数。In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. 有关详细信息,请参阅 Secure Template OverloadsFor more information, see Secure Template Overloads.

一般文本例程映射:Generic-Text Routine Mapping:

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tstrtime_s_tstrtime_s _strtime_s_strtime_s _strtime_s_strtime_s _wstrtime_s_wstrtime_s


例程所返回的值Routine 必需的标头Required header
_strtime_s_strtime_s <time.h><time.h>
_wstrtime_s_wstrtime_s <time.h> 或 <wchar.h><time.h> or <wchar.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


// 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.

    // Display operating system-style date and time.
    err = _strtime_s( tmpbuf, 9 );
    if (err)
       printf("_strdate_s failed due to an invalid argument.");
    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.");
    printf( "OS date:\t\t\t\t%s\n", tmpbuf );

OS time:            14:37:49
OS date:            04/25/03

请参阅See also

时间管理Time Management
asctime_s、_wasctime_sasctime_s, _wasctime_s
ctime_s、_ctime32_s、_ctime64_s、_wctime_s、_wctime32_s、_wctime64_sctime_s, _ctime32_s, _ctime64_s, _wctime_s, _wctime32_s, _wctime64_s
gmtime_s、_gmtime32_s、_gmtime64_sgmtime_s, _gmtime32_s, _gmtime64_s
localtime_s、_localtime32_s、_localtime64_slocaltime_s, _localtime32_s, _localtime64_s
mktime、_mktime32、_mktime64mktime, _mktime32, _mktime64
time、_time32、_time64time, _time32, _time64