日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux time()函数解析

發布時間:2023/12/9 linux 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux time()函数解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

time() 函數語法如下:

所需頭文件#include <time.h>
函數原型time_t time(time_t * timer)
參數說明timer=NULL時得到機器日歷時間;
timer=時間數值時,用于設置日歷時間;
函數返回值機器日歷時間

? ? ? 功能:?獲取當前的系統時間,返回的結果是一個time_t類型,其實就是一個大整數,其值表示從CUT(Coordinated Universal Time)時間1970年1月1日00:00:00(稱為UNIX系統的Epoch時間)到當前時刻的秒數。然后調用localtime將time_t所表示的CUT時間轉換為本地時間(我們是+8區,比CUT多8個小時)并轉成struct tm類型。

? ? ?struct tm 類型的各數據成員分別表示年月日時分秒。

[cpp]?view plaincopy
  • struct??tm??
  • {??
  • ???????int?tm_sec;??
  • ???????int?tm_min;??
  • ???????int?tm_hour;??
  • ???????int?tm_mday;??
  • ???????int?tm_mon;??
  • ???????int?tm_year;??
  • ???????int?tm_wday;??
  • ???????int?tm_yday;??
  • ???????int?tm_isdst;??
  • };???
  • 補充說明:time函數的原型也可以理解為 long time(long *tloc),即返回一個long型整數。因為在time.h這個頭文件中time_t 實際上就是:

    [cpp]?view plaincopy
  • #ifndef?_TIME_T_DEFINED??
  • #define?_TIME_T_DEFINED?/*?avoid?multiple?defines?of?time_t?*/??
  •   ??
  • ????typedef?long?time_t;?/*?time?value?*/??
  • ??
  • #endif??

  • 函數應用舉例

    程序例1

    ? ? ? ? ?time函數獲得日歷時間。日歷時間,是用“從一個標準時間點到此時的時間經過的秒數”來表示的時間。這個標準時間點對不同的編譯器來說會有所不同,但對一個編譯系統來說,這個標準時間點是不變的,該編譯系統中的時間對應的日歷時間都通過該標準時間點來衡量,所以可以說日歷時間是“相對時間”,但是無論你在哪一個時區,在同一時刻對同一個標準時間點來說,日歷時間都是一樣的。

    [cpp]?view plaincopy
  • #include?<time.h>??
  • #include?<stdio.h>??
  • ??
  • int?main(void)??
  • {??
  • ????time_t?t;???
  • <span?style="white-space:pre">????</span>t?=?time(NULL);??
  • ??
  •   ??printf("The?number?of?seconds?since?January?1,?1970?is?%ld",t);??
  • ??
  • ????return?0;??
  • }??
  • 執行結果如下:

    [cpp]?view plaincopy
  • fs@ubuntu:~/qiang/time$?./time1???
  • The?number?of?seconds?since?January?1,?1970?is?1452345470??
  • fs@ubuntu:~/qiang/time$???

  • 程序例2:

    ? ? ? ? ?time函數也常用于隨機數的生成,用日歷時間作為種子。

    [cpp]?view plaincopy
  • #include?<stdio.h>??
  • #include?<time.h>??
  • #include<stdlib.h>??
  • ??
  • int?main(void)??
  • {??
  • ????int?i;??
  • ????srand((unsigned)?time(NULL));??
  • ????printf("ten?random?numbers?from?0?to?99:\n");??
  • ??
  • ????for(i?=?0;i?<?10;i++)??
  • ????{??
  • ????????printf("%d\n",rand()%100);??
  • ????}??
  • ??
  • ????return?0;??
  • }??
  • 執行結果如下:

    [cpp]?view plaincopy
  • fs@ubuntu:~/qiang/time$?./time2??
  • ten?random?numbers?from?0?to?99:??
  • 22??
  • 0??
  • 58??
  • 7??
  • 29??
  • 90??
  • 74??
  • 7??
  • 95??
  • 55??
  • fs@ubuntu:~/qiang/time$???

  • 程序例3:

    ? ? ? ? 用time() 函數結合其他函數(如:localtime、gmtime、asctime、ctime)可以獲得當前系統時間或是標準時間

    1)localtime() 函數

    ? ? ? ??函數功能:返回一個以tm結構表達的機器時間信息;

    所需頭文件#include <time.h>
    函數原型struct tm *localtime(const time_t *timep)
    參數說明timerp為time(NULL)獲得的日歷時間;
    函數返回值?以tm結構表達的時間;

    使用示例:

    [cpp]?view plaincopy
  • #include?<time.h>???
  • #include?<stdio.h>???
  • ??
  • int?main()??
  • {??
  • ????time_t?timer;??
  • ????struct?tm?*tblock;??
  • ????timer?=?time(NULL);??
  • ????tblock?=?localtime(&timer);??
  • ??
  • ????printf("Local?time?is:?%s",asctime(tblock));??
  • ??????
  • ????return?0;???
  • }???
  • 執行結果如下:

    [cpp]?view plaincopy
  • fs@ubuntu:~/qiang/time$?./localtime???
  • Local?time?is:?Sat?Jan??9?21:37:13?2016??
  • fs@ubuntu:~/qiang/time$???

  • 2)asctime() 函數

    ? ? ? ?函數功能:asctime()將參數timeptr所指的tm結構中的信息轉換成真實世界所使用的時間日期表示方法,然后將結果以字符串形態返回。此函數已經由時區轉換成當地時間,字符串格式為: "Wed Jun 30 21:49:08 1993/n"?

    所需頭文件#include <time.h>
    函數原型?char *asctime(struct tm ?*ptr)
    參數說明ptr 為 struct tm 類型的時間結構體
    函數返回值返回的時間字符串格式為:星期,月,日,小時:分:秒,年

    示例如下:

    [cpp]?view plaincopy
  • #include?<stdio.h>???
  • #include?<string.h>???
  • #include?<time.h>??
  • ??
  • int?main()???
  • {??
  • ????struct?tm?t;??
  • ????char?str[80];??
  • ????t.tm_sec?=?1;??
  • ????t.tm_min?=?3;??
  • ????t.tm_hour?=?7;??
  • ????t.tm_mday?=?22;??
  • ????t.tm_mon?=?11;??
  • ????t.tm_year?=?56;??
  • ????t.tm_wday?=?4;??
  • ????t.tm_yday?=?0;??
  • ????t.tm_isdst?=?0;??
  • ????strcpy(str,asctime(&t));??
  • ??????
  • ????printf("%s",str);??
  • ??
  • ????return?0;???
  • }???
  • 執行結果如下:

    [cpp]?view plaincopy
  • fs@ubuntu:~/qiang/time$?./asctime???
  • Thu?Dec?22?07:03:01?1956??
  • fs@ubuntu:~/qiang/time$???

  • 3)ctime() 函數

    函數功能:ctime () 將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然后將結果以字符串形態返回。此函數已經由時區轉換成當地時間,字符串格式為"Wed Jun 30 21 :49 :08 1993/n"。若再調用相關的時間日期函數,此字符串可能會被破壞。

    所需頭文件#include <time.h>
    函數原型char *ctime(const time_t *timep);
    參數說明timep 是由 time(NULL) 得到的日歷時間;
    函數返回值返回字符串格式:星期,月,日,小時:分:秒,年

    示例如下:

    [cpp]?view plaincopy
  • #include?<stdio.h>???
  • #include?<time.h>???
  • int?main()???
  • {??
  • ????time_t?t;??
  • ????time(&t);??
  • ??????
  • ????printf("Today's?date?and?time:?%s",ctime(&t));??
  • ????return?0;???
  • }???
  • 執行結果如下:

    [cpp]?view plaincopy
  • fs@ubuntu:~/qiang/time$?./ctime???
  • Today's?date?and?time:?Sat?Jan??9?21:53:51?2016??
  • fs@ubuntu:~/qiang/time$ ??
  • 總結

    以上是生活随笔為你收集整理的Linux time()函数解析的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。