_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  


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

[in] numberOfElements[in] numberOfElements
缓冲区的大小。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

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

安全性问题Security Issues

如果 numberOfElements 参数大于 9,则为缓冲区传递一个无效的非 NULL 值将导致访问冲突。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_wstrtime 的更安全的版本。These 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:ss,其中 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 表示下午 6 点 23 分 44 秒。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 _strtime_s _strtime_s _wstrtime_s


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

有关其他兼容性信息,请参见“简介”中的 兼容性For additional compatibility information, see Compatibility in the Introduction.


// 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_s asctime_s, _wasctime_s
ctime_s、_ctime32_s、_ctime64_s、_wctime_s、_wctime32_s、_wctime64_s ctime_s, _ctime32_s, _ctime64_s, _wctime_s, _wctime32_s, _wctime64_s
gmtime_s、_gmtime32_s、_gmtime64_s gmtime_s, _gmtime32_s, _gmtime64_s
localtime_s、_localtime32_s、_localtime64_s localtime_s, _localtime32_s, _localtime64_s
mktime、_mktime32、_mktime64 mktime, _mktime32, _mktime64
time、_time32、_time64 time, _time32, _time64