_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

将指针返回到生成的字符字符串timestrReturns 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: 其中hh是表示二十四小时制的小时数的两个数字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 P.M.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. 否则这些函数具有相同行为。These functions behave identically otherwise. 如果timestrNULL指针或如果timestr的格式错误,无效参数处理程序调用中所述,参数验证If timestr is a NULL pointer or if timestr is formatted incorrectly, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许异常若要继续,则这些函数将返回NULL并设置errnoEINVAL如果timestrNULL或设置errnoERANGE如果timestr的格式不正确。If 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_tstrtime _strtime_strtime _strtime_strtime _wstrtime_wstrtime


例程Routine 必需的标头Required header
_strtime_strtime <time.h><time.h>
_wstrtime_wstrtime <time.h> 或 <wchar.h><time.h> or <wchar.h>

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


// 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、_wasctimeasctime, _wasctime
ctime、_ctime32、_ctime64、_wctime、_wctime32、_wctime64ctime, _ctime32, _ctime64, _wctime, _wctime32, _wctime64
gmtime、_gmtime32、_gmtime64gmtime, _gmtime32, _gmtime64
localtime、_localtime32、_localtime64localtime, _localtime32, _localtime64
mktime、_mktime32、_mktime64mktime, _mktime32, _mktime64
time、_time32、_time64time, _time32, _time64