asctime_wasctime

tm 时间结构转换为字符串。 提供这些函数的更安全版本;请参阅 asctime_s_wasctime_s

语法

char *asctime(
   const struct tm *timeptr
);
wchar_t *_wasctime(
   const struct tm *timeptr
);

参数

timeptr
时间/日期结构。

返回值

asctime 返回一个指向字符串结果的指针;_wasctime 返回一个指向宽字符串结果的指针。 没有错误返回值。

备注

提供这些函数的更安全版本;请参阅 asctime_s_wasctime_s

asctime 函数将存储为结构的时间转换为字符串。 通常情况下,timeptr 值通过调用 gmtimelocaltime 获取,这两个函数均返回一个指向 TIME_H 中定义的 tm 结构的指针。

timeptr 成员
tm_hour 午夜以后的小时数 (0-23)
tm_isdst 如果夏令时生效,则为正;如果夏令时不生效,则为 0;如果夏令时状态未知,则为负。 C 运行时库假设使用美国规则实现夏令时 (DST) 的计算。
tm_mday 月份日期 (1-31)
tm_min 整点后的分钟数 (0-59)
tm_mon 月(0-11;1 月 = 0)
tm_sec 整分后的秒数 (0-59)
tm_wday 每周的某一日(0-6;星期天 = 0)
tm_yday 一年的某一天(0-365;1 月 1 日 = 0)
tm_year 年(当前年份减去 1900)

有关配置本地时间的信息,请参阅 time_ftimelocaltime 函数。 有关定义时区环境和全局变量的信息,请参阅 _tzset 函数。

asctime 生成的字符串结果正好包含 26 个字符,格式为 Wed Jan 2 02:03:55 1980\n\0。 使用 24 小时制。 所有字段都具有固定宽度。 换行符和空字符占据字符串的最后两个位置。 asctime 使用单个静态分配的缓冲区来保存返回的字符串。 每次调用此函数都会破坏上一次调用的结果。

_wasctimeasctime 的宽字符版本,否则行为与 asctime 相同。

这些函数验证其参数。 如果 timeptr 是空指针或包含超出范围的值,则调用无效参数处理程序,如参数验证中所述。 如果允许继续执行,则函数将返回 NULL,并且将 errno 设置为 EINVAL

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_tasctime asctime asctime _wasctime

要求

例程 必需的标头
asctime <time.h>
_wasctime <time.h><wchar.h>

示例

此程序以长整型 aclock 格式进行系统时间设置,将其转换为 newtime 结构,然后使用 asctime 函数将其转换为字符串形式以进行输出。

// crt_asctime.c
// compile with: /W3

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

int main( void )
{
    struct tm   *newTime;
    time_t      szClock;

    // Get time in seconds
    time( &szClock );

    // Convert time to struct tm form
    newTime = localtime( &szClock );

    // Print local time as a string.
    printf_s( "Current date and time: %s", asctime( newTime ) ); // C4996
    // Note: asctime is deprecated; consider using asctime_s instead
}
Current date and time: Sun Feb  3 11:38:58 2002

另请参阅

工时管理
ctime_ctime32_ctime64_wctime_wctime32_wctime64
_ftime_ftime32_ftime64
gmtime_gmtime32_gmtime64
localtime_localtime32_localtime64
time_time32_time64
_tzset
asctime_s_wasctime_s