站長資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

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

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

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

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

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

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

        1. time() 函數(shù)獲取當(dāng)前時(shí)間
          #include <time.h> time_t time(time_t *t);    /* 此函數(shù)會(huì)返回從公元1970年1月1日的UTC時(shí)間從0時(shí)0分0秒算起到現(xiàn)在所經(jīng)過的秒數(shù)。  * 如果t 并非空指針的話,此函數(shù)也會(huì)將返回值存到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("%dn", (int)sec);      return 0; }

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

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

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

          #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í)世界所使用的時(shí)間日期表示方法,然后將結(jié)果由結(jié)構(gòu)tm返回*/  /**需要注意的是localtime函數(shù)可以將時(shí)間轉(zhuǎn)換本地時(shí)間,但是localtime函數(shù)不是線程安全的。 多線程應(yīng)用里面,應(yīng)該用localtime_r函數(shù)替代localtime函數(shù),因?yàn)閘ocaltime_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:%dn", (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é)果:

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

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

          #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是把日期和時(shí)間轉(zhuǎn)換為格林威治(GMT)時(shí)間的函數(shù)。  *將參數(shù)time 所指的time_t 結(jié)構(gòu)中的信息轉(zhuǎn)換成真實(shí)世界所使用的時(shí)間日期表示方法,然后將結(jié)果由結(jié)構(gòu)tm返回  *asctime 將時(shí)間以換為字符串字符串格式返回   */

          實(shí)例代碼:

          #include <stdio.h> #include <string.h> #include <time.h>     int main()  {        time_t timp;        time(&timp);         printf("%sn", asctime(gmtime(&timp)));             return 0; }

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

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

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

          #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í)世界所使用的時(shí)間日期表示方法,  *然后將結(jié)果以字符串形態(tài)返回  */

          實(shí)例代碼:

          #include <stdio.h> #include <string.h> #include <time.h>     int main(void)    {        time_t tmp;       time(&tmp);        printf("%sn", ctime(&tmp));          return 0;   }

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

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

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

           #include <time.h>  time_t mktime(struct tm *tm);  /*  *將時(shí)間結(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("%dn", (int)tmp);             return 0; }

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

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

        6. gettimeofday() 獲取當(dāng)前時(shí)間

          #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)前時(shí)間存于tv結(jié)構(gòu)體中,相應(yīng)的時(shí)區(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 = %dn", (int)tv.tv_sec);     printf("tv_usec = %dn", (int)tv.tv_usec);          return 0; }

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

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

        推薦學(xué)習(xí):Linux視頻教程

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
        主站蜘蛛池模板: 无码国产亚洲日韩国精品视频一区二区三区 | 色一乱一伦一图一区二区精品| 国产精品你懂的| 亚洲精品无码久久久久| 国产叼嘿久久精品久久| 99久久人妻无码精品系列| 亚洲国产综合精品一区在线播放| 国产成人精品福利网站在线观看 | 久久99精品久久久久久久不卡| 久久精品国产WWW456C0M| 在线观看日韩精品| 97精品国产福利一区二区三区| 亚洲AV无码久久精品蜜桃| 精品精品国产国产| 6080亚洲精品午夜福利| 国产成人精品久久免费动漫| 无码人妻精品一区二区三区在线| 久久久久久亚洲精品不卡 | 四虎永久在线精品国产免费| 精品国产福利一区二区| 亚洲AV日韩精品久久久久久| 亚洲国产精品综合久久一线| 免费人妻精品一区二区三区| 国产欧美在线观看精品一区二区| 91麻豆精品一二三区在线| 欧美精品亚洲精品日韩| 国产成人精品日本亚洲专| 99精品久久精品| 国产成人精品免费午夜app| 国产精品成人观看视频免费| 成人久久精品一区二区三区| 99久久免费国产精精品| 91精品一区国产高清在线| www.亚洲精品| 青青青国产依人精品视频| 日韩麻豆国产精品欧美| 亚洲精品线在线观看| 99久久免费国产精品| 国产精品性爱| 精品久久久久久无码人妻蜜桃| 欧美精品一区二区在线精品 |