C语言中char *ctime(const time_t *time);
函数将参数timep
所指的time_t
结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,输出4个字节日期字符串格式为"Wed Jun 30 21 :49 :08 2020\n
"。
为了去掉输出日期中的换行符,如下编程:
/*=========================================
* Copyright (c) 2020, 逐风墨客
* All rights reserved.
*
* 文件名称:study_nontime.c
* 运行环境:Linux操作系统
* 功能描述:去掉显示时间的换行符!
=========================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // 调用sleep()函数
#include <time.h>
const char *show_realtime(time_t t);
int main(void)
{
time_t t = 0;
printf("The current time is : %s\n", show_realtime(t));
sleep(5);
printf("The current time after 5 seconds is : %s\n", show_realtime(t));
return 0;
}
/*******************************************
* 函数介绍:const char *show_realtime(time_t t)
* 输入参数:t-时间种子
* 输出参数:无
* 返回值:buf-不带换行符的字符串时间
*******************************************/
const char *show_realtime(time_t t)
{
static char buf[32];
char *p = NULL;
time(&t);
strcpy(buf, ctime(&t));
p = strchr(buf, '\n');
*p = '\0';
return buf;
}
程序运行结果: