将时间值转换为字符串,并调整本地时区设置。 提供这些函数的更安全版本;请参阅 ctime_s、_ctime32_s、_ctime64_s、_wctime_s、_wctime32_s、_wctime64_s。
语法
char *ctime( const time_t *sourceTime ); // See note in remarks section about linkage
char *_ctime32( const __time32_t *sourceTime );
char *_ctime64( const __time64_t *sourceTime );
wchar_t *_wctime( const time_t *sourceTime ); // See note in remarks section about linkage
wchar_t *_wctime32( const __time32_t *sourceTime );
wchar_t *_wctime64( const __time64_t *sourceTime );
参数
sourceTime
指向要转换的存储时间的指针。
返回值
指向字符串结果的指针。 在以下情况下返回 NULL:
sourceTime表示 1970 年 1 月 1 日午夜前的日期(UTC 时间)。使用
_ctime32或_wctime32,且sourceTime表示 2038 年 1 月 18 日 23:59:59 后的日期(UTC 时间)。使用
_ctime64或_wctime64,且sourceTime表示 3000 年 12 月 31 日 23:59:59 后的日期(UTC 时间)。
ctime 是计算出 _ctime64 的内联函数,且 time_t 等同于 __time64_t。 如果需要强制编译器将 time_t 解释为旧的 32 位 time_t,你可以定义 _USE_32BIT_TIME_T。 此宏导致 ctime 计算结果为 _ctime32。 不建议使用,因为应用程序可能会在 2038 年 1 月 18 日后失效;且在 64 位平台上不允许使用它。
备注
ctime 函数将存储为 time_t 值的时间值转换为字符串。 通常情况下,通过对 sourceTime 的调用获取 time 值,它返回自协调世界时 (UTC) 1970 年 1 月 1 日午夜 (00:00:00) 以来经过的秒数。 返回值字符串正好包含 26 个字符,且格式为:
Wed Jan 02 02:03:55 1980\n\0
使用 24 小时制。 所有字段都具有固定宽度。 换行符 ('\n') 和空字符 ('\0') 占据字符串的最后两个位置。
转换的字符串同时根据本地时区设置进行调整。 若要了解如何配置本地时间,请参阅 time、_ftime 和 localtime 函数。 若要详细了解如何定义时区环境和全局变量,请参阅 _tzset 函数。
调用 ctime 会修改由 gmtime 和 localtime 函数使用的单个静态分配的缓冲区。 每次调用这些例程都会破坏上一次调用的结果。
ctime 与 asctime 函数共享静态缓冲区。 因此,调用 ctime 会破坏任何上一次调用 asctime、localtime 或 gmtime 的结果。
_wctime 和 _wctime64 是 ctime 和 _ctime64 的宽字符版本;返回指向宽字符串的指针。 否则,_ctime64、_wctime 和 _wctime64 的行为与 ctime 完全相同。
这些函数验证其参数。 如果 sourceTime 为空指针,或 sourceTime 值为负值,这些函数将调用无效参数处理程序,如参数验证中所述。 如果允许执行继续,则这些函数返回 NULL,并且将 errno 设置为 EINVAL。
默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态。
注释
使用 Windows SDK 版本 10.0.26100.6901 和 Visual Studio 2026 或更高版本时, ctime_wctime 不再 static inline (内部链接)。 相反,它们是 inline (外部链接)。
若要返回到以前的行为, #define _STATIC_INLINE_UCRT_FUNCTIONS=1 请先包括任何 CRT 标头。 默认情况下,_STATIC_INLINE_UCRT_FUNCTIONS 设置为 0。
此更改增加了与 C++ 标准的 UCRT 一致性,并提高了与C++模块的兼容性。
一般文本例程映射
TCHAR.H 例程 |
_UNICODE 和 _MBCS 未定义 |
_MBCS 已定义 |
_UNICODE 已定义 |
|---|---|---|---|
_tctime |
ctime |
ctime |
_wctime |
_tctime32 |
_ctime32 |
_ctime32 |
_wctime32 |
_tctime64 |
_ctime64 |
_ctime64 |
_wctime64 |
要求
| 例程 | 必需的标头 |
|---|---|
ctime |
<time.h> |
_ctime32 |
<time.h> |
_ctime64 |
<time.h> |
_wctime |
<time.h> 或 <wchar.h> |
_wctime32 |
<time.h> 或 <wchar.h> |
_wctime64 |
<time.h> 或 <wchar.h> |
有关兼容性的详细信息,请参阅 兼容性。
示例
// crt_ctime64.c
// compile with: /W3
/* This program gets the current
* time in _time64_t form, then uses ctime to
* display the time in string form.
*/
#include <time.h>
#include <stdio.h>
int main( void )
{
__time64_t ltime;
_time64( <ime );
printf( "The time is %s\n", _ctime64( <ime ) ); // C4996
// Note: _ctime64 is deprecated; consider using _ctime64_s
}
The time is Wed Feb 13 16:04:43 2002
另请参阅
工时管理
%>
ctime_s, _ctime32_s, _ctime64_s, _wctime_s, _wctime32_s, _wctime64_s
.- .
.- .
.- .
.- .