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

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

        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)前的時間。

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

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

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

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

        1. 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("%dn", (int)sec);      return 0; }

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

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

        2. 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)閘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ù)嗎

        3. 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("%sn", asctime(gmtime(&timp)));             return 0; }

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

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

        4. 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("%sn", ctime(&tmp));          return 0;   }

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

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

        5. 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("%dn", (int)tmp);             return 0; }

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

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

        6. 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 = %dn", (int)tv.tv_sec);     printf("tv_usec = %dn", (int)tv.tv_usec);          return 0; }

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

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

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

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號
        主站蜘蛛池模板: 2022国产精品福利在线观看| 久久久精品午夜免费不卡| 精品久久久久久中文字幕人妻最新| 91久久精品国产免费直播| 久久久久成人精品无码中文字幕 | 国内精品久久久久国产盗摄 | 99久久精品国产麻豆| 亚洲精品成人无码中文毛片不卡| 国产精品va在线观看无码| 97久久综合精品久久久综合| 日韩国产成人精品视频| 亚洲av永久无码精品漫画| 无码人妻精品一区二区蜜桃百度 | 久久精品亚洲福利| 国产叼嘿久久精品久久| 亚洲国产综合91精品麻豆| 国产精品对白交换视频| 久久夜色精品国产噜噜亚洲AV| 亚洲国产午夜中文字幕精品黄网站 | 国产精品亚洲精品| 国产精品无码无需播放器| 亚洲中文精品久久久久久不卡| 久久久久国产精品嫩草影院| 国产亚洲精品拍拍拍拍拍| 国产午夜精品久久久久九九电影| 中文字幕一区二区精品区| 色播精品免费小视频| 久久久国产精品福利免费| 久久精品国产精品亚洲精品| 久久成人影院精品777| 热re99久久精品国产99热| 亚洲精品免费在线观看| 91久久精品国产91性色也| 国产精品二区观看| 国产精品小黄鸭一区二区三区| 国产精品玖玖美女张开腿让男人桶爽免费看| 66精品综合久久久久久久| 国产成人精品综合久久久| 国产一区二区精品久久凹凸| 精品亚洲欧美无人区乱码| 日韩精品欧美亚洲|