将时间复制到缓冲区。 提供这些函数的更安全版本;请参阅 _strtime_s、_wstrtime_s。
语法
char *_strtime(
char *timestr
);
wchar_t *_wstrtime(
wchar_t *timestr
);
template <size_t size>
char *_strtime(
char (×tr)[size]
); // C++ only
template <size_t size>
wchar_t *_wstrtime(
wchar_t (×tr)[size]
); // C++ only
参数
timestr
时间字符串。
返回值
返回一个指向结果字符串 timestr 的指针。
注解
_strtime 函数将当前的本地时间复制到 timestr 所指向的缓冲区。 时间的格式为 hh:mm:ss,其中 hh 是表示 24 小时制中的小时的两位数。 mm 是表示过去一小时的分数的两位数,ss 是表示秒数的两位数。 例如,字符串 18:23:44 表示下面 6 点 23 分 44 秒。缓冲区长度必须至少为 9 个字节。
_wstrtime 是 _strtime 的宽字符版本;_wstrtime 的参数和返回值都是宽字符字符串。 否则这些函数具有相同行为。 如果 timestr 是 NULL 指针,或者 timestr 格式不正确,会调用无效的参数处理程序,如参数验证中所述。 如果允许继续存在异常,这些函数会返回 NULL,并且将 errno 设置为 EINVAL(如果 timestr 是 NULL),或将 errno 设置为 ERANGE(如果 timestr 格式不正确)。
在 C++ 中,这些函数具有模板重载,以调用这些函数的更新、更安全副本。 有关详细信息,请参阅安全模板重载。
默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态。
一般文本例程映射
| TCHAR.H 例程 | _UNICODE 和 _MBCS 未定义 |
_MBCS 已定义 |
_UNICODE 已定义 |
|---|---|---|---|
_tstrtime |
_strtime |
_strtime |
_wstrtime |
要求
| 例程 | 必需的标头 |
|---|---|
_strtime |
<time.h> |
_wstrtime |
<time.h> 或 <wchar.h> |
有关兼容性的详细信息,请参阅 兼容性。
示例
// 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