ctime, _ctime32, _ctime64, _wctime, _wctime32, _wctime64ctime, _ctime32, _ctime64, _wctime, _wctime32, _wctime64

将时间值转换为字符串,并调整本地时区设置。Convert a time value to a string and adjust for local time zone settings. 提供这些函数的更安全版本;请参阅 ctime_s、_ctime32_s、_ctime64_s、_wctime_s、_wctime32_s、_wctime64_sMore secure versions of these functions are available; see ctime_s, _ctime32_s, _ctime64_s, _wctime_s, _wctime32_s, _wctime64_s.

语法Syntax

char *ctime( const time_t *sourceTime );
char *_ctime32( const __time32_t *sourceTime );
char *_ctime64( const __time64_t *sourceTime );
wchar_t *_wctime( const time_t *sourceTime );
wchar_t *_wctime32( const __time32_t *sourceTime );
wchar_t *_wctime64( const __time64_t *sourceTime );

参数Parameters

sourceTimesourceTime
指向存储时间将转换的指针。Pointer to stored time to convert.

返回值Return Value

指向字符串结果的指针。A pointer to the character string result. NULL时将返回:NULL will be returned if:

  • sourceTime表示日期早于 1970 年 1 月 1 日午夜 UTC。sourceTime represents a date before midnight, January 1, 1970, UTC.

  • 如果你使用 _ctime32_wctime32sourceTime表示 23:59:59 2038 年 1 月 18 日,UTC 之后的日期。If you use _ctime32 or _wctime32 and sourceTime represents a date after 23:59:59 January 18, 2038, UTC.

  • 如果你使用 _ctime64_wctime64sourceTime表示 23:59:59,3000 年 12 月 31 日,UTC 之后的日期。If you use _ctime64 or _wctime64 and sourceTime represents a date after 23:59:59, December 31, 3000, UTC.

ctime是内联函数计算结果为 _ctime64time_t等效于 __time64_tctime is an inline function which evaluates to _ctime64 and time_t is equivalent to __time64_t. 如果需要强制编译器将解释time_t为旧的 32 位time_t,你可以定义 _USE_32BIT_TIME_TIf you need to force the compiler to interpret time_t as the old 32-bit time_t, you can define _USE_32BIT_TIME_T. 这将导致这样做ctime计算结果为 _ctime32Doing this will cause ctime to evaluate to _ctime32. 不建议这样做,因为应用程序可能会在 2038 年 1 月 18 日后失效;且在 64 位平台上不允许使用它。This is not recommended because your application may fail after January 18, 2038, and it is not allowed on 64-bit platforms.

备注Remarks

Ctime函数将转换为存储的时间值time_t转换为字符串的值。The ctime function converts a time value stored as a time_t value into a character string. SourceTime值通常从调用中获取时间,它将返回自午夜以来经过的秒数 (00: 00:00)、 1970 年 1 月 1 日协调世界时 (UTC)。The sourceTime value is usually obtained from a call to time, which returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC). 返回值字符串正好包含 26 个字符,且格式为:The return value string contains exactly 26 characters and has the form:

Wed Jan 02 02:03:55 1980\n\0

使用 24 小时制。A 24-hour clock is used. 所有字段都具有固定宽度。All fields have a constant width. 换行符 ('\n') 和空字符 ('\0') 占据字符串的最后两个位置。The newline character ('\n') and the null character ('\0') occupy the last two positions of the string.

转换的字符串同时根据本地时区设置进行调整。The converted character string is also adjusted according to the local time zone settings. 请参阅时间_ftime,和localtime有关如何配置本地时间的函数和_tzset函数有关定义时区环境和全局变量的详细信息。See the time, _ftime, and localtime functions for information on configuring the local time and the _tzset function for details about defining the time zone environment and global variables.

调用ctime修改使用的单个静态分配的缓冲区gmtimelocaltime函数。A call to ctime modifies the single statically allocated buffer used by the gmtime and localtime functions. 每次调用这些例程都会破坏上一次调用的结果。Each call to one of these routines destroys the result of the previous call. ctime共享具有静态缓冲区asctime函数。ctime shares a static buffer with the asctime function. 因此,调用ctime销毁任何以前调用的结果asctimelocaltime,或gmtimeThus, a call to ctime destroys the results of any previous call to asctime, localtime, or gmtime.

_wctime_wctime64是宽字符版本的ctime_ctime64; 返回一个指向宽字符字符串。_wctime and _wctime64 are the wide-character version of ctime and _ctime64; returning a pointer to wide-character string. 否则为 _ctime64_wctime,和 _wctime64行为ctimeOtherwise, _ctime64, _wctime, and _wctime64 behave identically to ctime.

这些函数验证其参数。These functions validate their parameters. 如果sourceTime是 null 指针,或如果sourceTime值为负中, 所述,这些函数将调用无效参数处理程序,参数验证.If sourceTime is a null pointer, or if the sourceTime value is negative, these functions invoke the invalid parameter handler, as described in Parameter Validation. 如果允许执行继续,函数将返回NULL并设置errnoEINVALIf execution is allowed to continue, the functions return NULL and set errno to EINVAL.

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

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tctime_tctime ctimectime ctimectime _wctime_wctime
_tctime32_tctime32 _ctime32_ctime32 _ctime32_ctime32 _wctime32_wctime32
_tctime64_tctime64 _ctime64_ctime64 _ctime64_ctime64 _wctime64_wctime64

要求Requirements

例程Routine 必需的标头Required header
ctimectime <time.h><time.h>
_ctime32_ctime32 <time.h><time.h>
_ctime64_ctime64 <time.h><time.h>
_wctime_wctime <time.h> 或 <wchar.h><time.h> or <wchar.h>
_wctime32_wctime32 <time.h> 或 <wchar.h><time.h> or <wchar.h>
_wctime64_wctime64 <time.h> 或 <wchar.h><time.h> or <wchar.h>

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

示例Example

// 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( &ltime );
   printf( "The time is %s\n", _ctime64( &ltime ) ); // C4996
   // Note: _ctime64 is deprecated; consider using _ctime64_s
}
The time is Wed Feb 13 16:04:43 2002

请参阅See also

时间管理Time Management
asctime、_wasctimeasctime, _wasctime
ctime_s、_ctime32_s、_ctime64_s、_wctime_s、_wctime32_s、_wctime64_sctime_s, _ctime32_s, _ctime64_s, _wctime_s, _wctime32_s, _wctime64_s
_ftime, _ftime32, _ftime64_ftime, _ftime32, _ftime64
gmtime、_gmtime32、_gmtime64gmtime, _gmtime32, _gmtime64
localtime、_localtime32、_localtime64localtime, _localtime32, _localtime64
time、_time32、_time64time, _time32, _time64