asctime_r ()- 将时间转换为字符串 (可重新启动)

格式

#include <time.h>
char *asctime_r(const struct tm *tm, char *buf);

语言级别

XPG4

线程安全

描述

此函数是 asctime() 函数的可重新启动版本。

asctime_r() 函数将存储为 tm指向的结构的时间转换为字符串。 您可以从对 gmtime_r()gmtime64_r()localtime_r()localtime64_r()的调用中获取 tm 值。

asctime_r() 生成的字符串结果正好包含 26 个字符,并且具有以下格式:
   "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n"
以下是返回的字符串的示例:
   Sat Jul 16 02:03:55 1994\n\0
or
   Sat Jul 16  2:03:55 1994\n\0

asctime_r() 函数使用 24 小时制格式。 这些日子缩写为: SunMonTueWedThuFriSat。 月份缩写为: JanFebMarAprMayJunJulAugSepOctNovDec。 所有字段都具有恒定宽度。 只有一个数字的日期前面有零或空格。 换行符 (\n) 和空字符 (\0) 占据字符串的最后两个位置。

时间和日期函数从全球时间 00:00:00 开始, 1970 年 1 月 1。

返回值

asctime_r() 函数返回指向生成的字符串的指针。 如果该函数不成功,那么它将返回 NULL。

示例

此示例轮询系统时钟并打印给出当前时间的消息。
#include <time.h>
#include <stdio.h>
 
int main(void)
{
    struct tm *newtime;
    time_t ltime;
    char  mybuf[50];
 
/* Get the time in seconds */
    time(&ltime);
/* Convert it to the structure tm */
    newtime = localtime_r(&ltime());
/* Print the local time as a string */
    printf("The current date and time are %s",
             asctime_r(newtime, mybuf));
}
 
/****************  Output should be similar to  ******************
The current date and time are Fri Sep 16 132951 1994
*/

相关信息