??C語(yǔ)言中char *ctime(const time_t *time);
函數(shù)將參數(shù)timep
所指的time_t
結(jié)構(gòu)中的信息轉(zhuǎn)換成真實(shí)世界所使用的時(shí)間日期表示方法,然后將結(jié)果以字符串形態(tài)返回。此函數(shù)已經(jīng)由時(shí)區(qū)轉(zhuǎn)換成當(dāng)?shù)貢r(shí)間睁蕾,輸出4個(gè)字節(jié)日期字符串格式為"Wed Jun 30 21 :49 :08 2020\n
"。
??為了去掉輸出日期中的換行符债朵,如下編程:
/*=========================================
* Copyright (c) 2020, 逐風(fēng)墨客
* All rights reserved.
*
* 文件名稱(chēng):study_nontime.c
* 運(yùn)行環(huán)境:Linux操作系統(tǒng)
* 功能描述:去掉顯示時(shí)間的換行符子眶!
=========================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // 調(diào)用sleep()函數(shù)
#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;
}
/*******************************************
* 函數(shù)介紹:const char *show_realtime(time_t t)
* 輸入?yún)?shù):t-時(shí)間種子
* 輸出參數(shù):無(wú)
* 返回值:buf-不帶換行符的字符串時(shí)間
*******************************************/
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;
}
??程序運(yùn)行結(jié)果: