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\0asctime_r() 函数使用 24 小时制格式。 这些日子缩写为: Sun, Mon, Tue, Wed, Thu, Fri和 Sat。 月份缩写为: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov和 Dec。 所有字段都具有恒定宽度。 只有一个数字的日期前面有零或空格。 换行符 (\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(<ime);
/* Convert it to the structure tm */
newtime = localtime_r(<ime());
/* 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
*/相关信息
- asctime ()-将时间转换为字符串
- ctime ()-将时间转换为字符串
- ctime64()-将时间转换为字符串
- ctime64_r()-将时间转换为字符串 (可重新启动)
- ctime_r ()-将时间转换为字符串 (可重新启动)
- gmtime ()-转换时间
- gmtime64()-转换时间
- gmtime64_r()-转换时间 (可重新启动)
- gmtime_r ()-转换时间 (可重新启动)
- localtime ()-转换时间
- localtime64()-转换时间
- localtime64_r()-转换时间 (可重新启动)
- localtime_r ()-转换时间 (可重新启动)
- mktime ()-转换本地时间
- mktime64()-转换本地时间
- strftime ()-将日期/时间转换为字符串
- time ()-确定当前时间
- printf ()-打印格式化字符
- <time.h>