_strtime、_wstrtime_strtime, _wstrtime

将时间复制到缓冲区。Copy the time to a buffer. 这些函数的更安全版本已经发布;请参阅 _strtime_s、_wstrtime_sMore secure versions of these functions are available; see _strtime_s, _wstrtime_s.


char *_strtime(  
   char *timestr   
wchar_t *_wstrtime(  
   wchar_t *timestr   
template <size_t size>  
char *_strtime(  
   char (&timestr)[size]  
); // C++ only  
template <size_t size>  
wchar_t *_wstrtime(  
   wchar_t (&timestr)[size]  
); // C++ only  


时间字符串。Time string.

返回值Return Value

返回一个指向结果字符串 timestr 的指针。Returns a pointer to the resulting character string timestr.


_strtime函数将当前的本地时间复制到缓冲区的指向timestrThe _strtime 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.

_wstrtime_strtime 的宽字符版本;_wstrtime 的参数和返回值都是宽字符字符串。_wstrtime is a wide-character version of _strtime; the argument and return value of _wstrtime are wide-character strings. 否则,这些函数具有相同的行为。如果 timestrNULL 指针,或者如果 timestr 格式不正确,则将调用无效的参数处理程序,如参数验证中所述。These functions behave identically otherwise.If timestr is NULL pointer or if timestr is formatted incorrectly, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则这些函数返回 NULL,并且如果 timestr 为 NULL,则将 errno 设置为 EINVAL,或者如果 timestr 的格式不正确,则将 errno 设置为 ERANGEIf the exception is allowed to continue, these functions return a NULL and set errno to EINVAL if timestr was a NULL or set errno to ERANGE if timestr is formatted incorrectly.

在 C++ 中,这些函数具有模板重载,以调用这些函数的更新、更安全副本。In C++, these functions have template overloads that invoke the newer, secure counterparts of these functions. 有关详细信息,请参阅 Secure Template OverloadsFor more information, see Secure Template Overloads.

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

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tstrtime _strtime _strtime _wstrtime


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

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


// crt_strtime.c  
// compile with: /W3  

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

int main( void )  
   char tbuffer [9];  
   _strtime( tbuffer ); // C4996  
   // Note: _strtime is deprecated; consider using _strtime_s instead  
   printf( "The current time is %s \n", tbuffer );  
The current time is 14:21:44  

请参阅See Also

时间管理 Time Management
asctime、_wasctime asctime, _wasctime
ctime、_ctime32、_ctime64、_wctime、_wctime32、_wctime64 ctime, _ctime32, _ctime64, _wctime, _wctime32, _wctime64
gmtime、_gmtime32、_gmtime64 gmtime, _gmtime32, _gmtime64
localtime、_localtime32、_localtime64 localtime, _localtime32, _localtime64
mktime、_mktime32、_mktime64 mktime, _mktime32, _mktime64
time、_time32、_time64 time, _time32, _time64