久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

linux有沒有獲取時間的函數(shù)

166次閱讀
沒有評論

共計(jì) 3045 個字符,預(yù)計(jì)需要花費(fèi) 8 分鐘才能閱讀完成。

本篇內(nèi)容介紹了“l(fā)inux 有沒有獲取時間的函數(shù)”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

linux 有獲取時間的函數(shù)。linux 常用的時間函數(shù):1、time()函數(shù),獲取當(dāng)前的時間;2、“l(fā)ocaltime_r”()和 localtime()函數(shù),取得當(dāng)?shù)啬壳皶r間和日期;3、gettimeofday()函數(shù),也可以獲取當(dāng)前的時間。

本教程操作環(huán)境:linux7.3 系統(tǒng)、Dell G3 電腦。

linux 獲取時間的函數(shù)

常用的時間函數(shù)介紹:

time() 函數(shù)獲取當(dāng)前時間

#include  time.h 
time_t time(time_t *t); 
/*  此函數(shù)會返回從公元 1970 年 1 月 1 日的 UTC 時間從 0 時 0 分 0 秒算起到現(xiàn)在所經(jīng)過的秒數(shù)。 *  如果 t   并非空指針的話,此函數(shù)也會將返回值存到 t 指針?biāo)傅膬?nèi)存。 */

實(shí)例代碼:

#include  stdio.h 
#include  string.h 
#include  time.h 
int main()
 time_t sec;
 sec = time((time_t *)NULL);
 printf(%d\n , (int)sec);
 return 0;
}

運(yùn)行結(jié)果:

localtime_r() localtime()取得當(dāng)?shù)啬壳皶r間和日期

#include  time.h 
 
 struct tm *localtime(const time_t *timep);
 struct tm *localtime_r(const time_t *timep, struct tm *result);
 
/* 該函數(shù)將有 time 函數(shù)獲取的值 timep 轉(zhuǎn)換真實(shí)世界所使用的時間日期表示方法,然后將結(jié)果由結(jié)構(gòu) tm 返回 */
/** 需要注意的是 localtime 函數(shù)可以將時間轉(zhuǎn)換本地時間,但是 localtime 函數(shù)不是線程安全的。多線程應(yīng)用里面,應(yīng)該用 localtime_r 函數(shù)替代 localtime 函數(shù),因?yàn)?localtime_r 是線程安全的 **/

實(shí)例代碼:

#include  stdio.h 
#include  string.h 
#include  time.h 
int main()
 time_t tmp; 
 struct tm *timp; 
 
 time(tmp); 
 timp = localtime(tmp);
 
 printf(%d-%d-%d %d:%d:%d\n , (1900 + timp- tm_year), ( 1 + timp- tm_mon), timp- tm_mday,
 (timp- tm_hour), timp- tm_min, timp- tm_sec); 
 return 0;
}

運(yùn)行結(jié)果:

asctime()  asctime_r() 將時間和日期以字符串格式返回

#include  time.h 
 
 struct tm *gmtime(const time_t *timep);
 struct tm *gmtime_r(const time_t *timep, struct tm *result);
 
 char *asctime(const struct tm *tm);
 char *asctime_r(const struct tm *tm, char *buf);
 
 
 *gmtime 是把日期和時間轉(zhuǎn)換為格林威治 (GMT) 時間的函數(shù)。 * 將參數(shù) time  所指的 time_t  結(jié)構(gòu)中的信息轉(zhuǎn)換成真實(shí)世界所使用的時間日期表示方法,然后將結(jié)果由結(jié)構(gòu) tm 返回
 *asctime  將時間以換為字符串字符串格式返回  
 */

實(shí)例代碼:

#include  stdio.h 
#include  string.h 
#include  time.h  
 
int main() 
{ 
 time_t timp; 
 time(timp);
 
 printf(%s\n , asctime(gmtime( timp))); 
 
 return 0;
}

運(yùn)行結(jié)果:(asctime 獲取的字符串自己帶有換行符)

ctime(),ctime_r() 將時間和日期以字符串格式表示

#include  time.h 
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);

 *ctime()將參數(shù) timep 所指的 time_t 結(jié)構(gòu)中的信息轉(zhuǎn)換成真實(shí)世界所使用的時間日期表示方法, * 然后將結(jié)果以字符串形態(tài)返回  */

實(shí)例代碼:

#include  stdio.h 
#include  string.h 
#include  time.h  
int main(void) 
{ 
 time_t tmp;
 
 time(tmp); 
 printf(%s\n , ctime( tmp));
 
 return 0; 
}

運(yùn)行結(jié)果:(ctime 獲取的字符串自己帶有換行符)

mktime() 將時間結(jié)構(gòu)體 struct tm 的值轉(zhuǎn)化為經(jīng)過的秒數(shù)

 #include  time.h 
 time_t mktime(struct tm *tm);
 * 將時間結(jié)構(gòu)體 struct tm 的值轉(zhuǎn)化為經(jīng)過的秒數(shù)
 */

實(shí)例代碼:

#include  stdio.h 
#include  string.h 
#include  time.h  
 
int main() 
{ 
 time_t tmp; 
 struct tm *timp; 
 time(tmp); 
 timp = localtime(tmp); 
 tmp = mktime(timp);
 
 printf(%d\n , (int)tmp); 
 
 return 0;
}

運(yùn)行結(jié)果:

gettimeofday() 獲取當(dāng)前時間

#include  sys/time.h 
int gettimeofday(struct timeval *tv, struct timezone *tz);
 
struct timeval { time_t tv_sec; /* seconds (秒)*/
 suseconds_t tv_usec; /* microseconds(微秒) */
 };
struct timezone {
 int tz_minuteswest; /* minutes west of Greenwich */
 int tz_dsttime; /* type of DST correction */
 };
 *gettimeofday 函數(shù)獲取當(dāng)前時間存于 tv 結(jié)構(gòu)體中,相應(yīng)的時區(qū)信息則存于 tz 結(jié)構(gòu)體中
 * 需要注意的是 tz 是依賴于系統(tǒng),不同的系統(tǒng)可能存在獲取不到的可能,因此通常設(shè)置為 NULL
 */

實(shí)例代碼:

#include  stdio.h 
#include  string.h 
#include  sys/time.h 
int main()
 struct timeval tv; 
 
 gettimeofday(tv, NULL);
 printf(tv_sec = %d\n , (int)tv.tv_sec);
 printf(tv_usec = %d\n , (int)tv.tv_usec);
 
 return 0;
}

運(yùn)行結(jié)果:

“l(fā)inux 有沒有獲取時間的函數(shù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-08-01發(fā)表,共計(jì)3045字。
轉(zhuǎn)載說明:除特殊說明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 翁牛特旗| 长岛县| 隆尧县| 阿拉善盟| 沙坪坝区| 阿巴嘎旗| 墨江| 双桥区| 惠安县| 聂荣县| 三江| 广河县| 龙州县| 凤阳县| 教育| 久治县| 科技| 蒙城县| 建湖县| 辽阳市| 建德市| 恩平市| 当雄县| 横峰县| 谢通门县| 银川市| 阿尔山市| 禹城市| 昆明市| 邵阳县| 阿尔山市| 禹城市| 喀什市| 芮城县| 大田县| 凯里市| 新郑市| 宜兴市| 二手房| 石嘴山市| 申扎县|