linux系統時間獲取方式


Linux 操作系統計算系統時間:
主要函數:time  localtime  gmtime  asctime  ctime  mktime  
                  difftime  strftime gmstrftime
1.time()函數
  原型:time_t time(time_t * timer)
  功能:返回一個time_t類型的數據,表示從CUT時間1970年1月1日00:00:00(稱為UNIX系統的Epoch時間)到當前時刻的秒數.
       然后調用localtime將time_t所表示的CUT時間轉換為本地時間(我們是+8區,比CUT多8個小時)並轉成struct tm類型,    
       該類型的各數據成員分別表示年月日時分秒。
2.localtime()函數
  原型:struct tm *localtime(const time_t *clock);
  返回值:返回指向tm 結構體的指針   
  功能:把從1970-1-1零點零分到當前時間系統所偏移的秒數時間轉換為日歷時間.轉換過時區!(Fri Oct 13 17:36:29 2017)
3.gmtime()函數
  原型:struct tm *gmtime(long *clock);
  返回值:返回指向tm 結構體的指針
  功能:所指的time_t結構中的信息(從1970-1-1零點零分到當前時間系統所偏移的秒數)轉換成真實世界所使用的時間日期表示  
          方法,為經過時區轉換!比localtime小整整8小時!(Fri Oct 13 09:36:29 2017)
4.asctime()函數
  原型:char *asctime(const struct tm *tblock);
  功能:轉換日期和時間為相應的字符串(如:Fri Oct 13 17:36:29 2017)
5.ctime()函數
  原型:char *ctime(const time_t *time);
  功能:把日期和時間轉換為字符串。(如:Fri Oct 13 17:36:29 2017)
6.mktime()函數
  原型:time_t mktime(strcut tm * timeptr);
  功能:將所指的tm結構數據轉換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經過的秒數。
7.difftime()函數
  原型:double difftime(time_t time1, time_t time0);
  功能:計算時間間隔,以秒為單位,且只能精確到秒.
8.strftime()函數
  原型:size_t strftime(char *strDest,size_t maxsize,const char *format,const struct tm *timeptr);
  功能:格式化一個時間字符串
  返回值:該函數返回向strDest指向的字符串中放置的字符數。
  說明:類似於sprintf():識別以百分號(%)開始的格式命令集合,格式化輸出結果放在一個字符串中。
    %a 星期幾的簡寫
  %A 星期幾的全稱
  %b 月份的簡寫
  %B 月份的全稱
  %c 標准的日期的時間串
  %C 年份的后兩位數字
  %d 十進制表示的每月的第幾天
  %D 月/天/年
  %e 在兩字符域中,十進制表示的每月的第幾天
  %F 年-月-日
  %g 年份的后兩位數字,使用基於周的年
  %G 年份,使用基於周的年
  %h 簡寫的月份名
  %H 24小時制的小時
  %I 12小時制的小時
  %j 十進制表示的每年的第幾天
  %m 十進制表示的月份
  %M 十時制表示的分鍾數
  %n 新行符
  %p 本地的AM或PM的等價顯示
  %r 12小時的時間
  %R 顯示小時和分鍾:hh:mm
  %S 十進制的秒數
  %t 水平制表符
  %T 顯示時分秒:hh:mm:ss
  %u 每周的第幾天,星期一為第一天 (值從0到6,星期一為0)
  %U 第年的第幾周,把星期日作為第一天(值從0到53)
  %V 每年的第幾周,使用基於周的年
  %w 十進制表示的星期幾(值從0到6,星期天為0)
  %W 每年的第幾周,把星期一做為第一天(值從0到53)
  %x 標准的日期串
  %X 標准的時間串
  %y 不帶世紀的十進制年份(值從0到99)
  %Y 帶世紀部分的十制年份
  %z,%Z 時區名稱,如果不能得到時區名稱則返回空字符。
  %% 百分號

  提示:與 gmstrftime() 的行為相同,不同的是返回時間是本地時間。

9.gettimeofday()函數精確到微秒

  原型:int gettimeofday(struct  timeval*tv,struct  timezone *tz )

  功能:在使用gettimeofday()函數時,第二個參數一般都為空,我們一般只是為了獲得當前時間,而不用獲得timezone的數值

10.getSystemTime


相關類型:
1.time_t實際上是長整數類型,定義為:typedef long time_t; 
2.timeval是一個結構體,在time.h中定義為:
struct timeval
{
     __time_t tv_sec;                /* Seconds. */
     __suseconds_t tv_usec;      /* Microseconds. */
};
其中,tv_sec為Epoch(1970-1-1零點零分)到創建struct timeval時的秒數,tv_usec為微秒數,即秒后面的零頭。
3.tm是一個結構體,定義為:
struct tm
{
    int tm_sec;      /*代表目前秒數,正常范圍為0-59,但允許至61秒 */
    int tm_min;     /*代表目前分數,范圍0-59*/
    int tm_hour;   /* 從午夜算起的時數,范圍為0-23 */
    int tm_mday;  /* 目前月份的日數,范圍01-31 */
    int tm_mon;   /*代表目前月份,從一月算起,范圍從0-11 */
    int tm_year;   /*從1900 年算起至今的年數*/
    int tm_wday;   /* 一星期的日數,從星期一算起,范圍為0-6。*/
    int tm_yday;   /* Days in year.[0-365] */
    int tm_isdst;   /*日光節約時間的旗標DST. [-1/0/1]*/

};

 

測試代碼:

time:

 

[objc]  view plain  copy
 
  1. #include<stdio.h>  
  2. #include<time.h>  
  3. int main()  
  4. {  
  5. time_t t;  
  6. t=time(NULL);  
  7. printf("the number of seconds since 1970-01-01 00:00 is:%d\n",t);  
  8. return 0;  
  9. }  
結果:

 

[root@libmaster time]# ./a.out 
the number of seconds since 1970-01-01 00:00 is:1507889358

localtime gmtime ctime asctime  tzset

 

[objc]  view plain  copy
 
  1. #include<time.h>  
  2. #include<stdio.h>  
  3. #include<stdlib.h>  
  4. int main(int argc, const charchar *argv[])  
  5. {  
  6.     struct tmtm *gmt, *local;  
  7.     time_t tt;  
  8.     tzset();//void tzset(void);設置時間環境變量-時區  
  9.     tt=time(NULL);//等價於time(&tt);  
  10.     charchar *str=ctime(&tt);  
  11.         printf("ctime is:%s",str);  
  12.     local=localtime(&tt);  
  13.     printf("%4d年%02d月%02d日 %2d:%2d:%2d\n",local->tm_year+1900,local->tm_mon+1,local->tm_mday,local->tm_hour,local->tm_min,local->tm_sec);  
  14.     printf("lcoaltime is:%s",asctime(local));  
  15.     gmt=gmtime(&tt);  
  16.         printf("gmtime is:%s",asctime(gmt));  
  17.         return 0;  
  18. }  
結果:

[root@libmaster time]# ./a.out 
ctime is:Mon Oct 16 10:40:39 2017
2017年10月16日 10:40:39
lcoaltime is:Mon Oct 16 10:40:39 2017
gmtime is:Mon Oct 16 02:40:39 2017

difftime

 

[objc]  view plain  copy
 
  1. #include <stdio.h>  
  2. #include <time.h>  
  3. int main(){  
  4.     time_t t_start, t_end;  
  5.     t_start = time(NULL) ;  
  6.     sleep(5);  
  7.     t_end = time(NULL) ;  
  8.     printf("time: %.0f s\n", difftime(t_end,t_start)) ;  
  9.     return 0;  
  10. }  
結果:

[root@libmaster time]# ./a.out 
time: 5 s

gettimeofday

 

[objc]  view plain  copy
 
  1. #include <stdio.h>  
  2. #include <sys/time.h>  
  3. int main() {  
  4.     struct timeval start, end;  
  5.     gettimeofday( &start, NULL );  
  6.     sleep(3);  
  7.     gettimeofday( &end, NULL );  
  8.     int timeuse = 11000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;  
  9.     printf("time: %d us\n", timeuse);  
  10.     return 0;  
  11. }  
結果:

[root@libmaster time]# ./a.out 
time: 3000205 us

getSystemTime

 

[objc]  view plain  copy
 
  1. #include <stdio.h>  
  2. #include <sys/timeb.h>  
  3. long long getSystemTime() {  
  4.     struct timeb t;  
  5.     ftime(&t);  
  6.     return 11000 * t.time + t.millitm;  
  7. }  
  8. int main() {  
  9.     long long start=getSystemTime();  
  10.     sleep(3);  
  11.     long long end=getSystemTime();  
  12.     printf("time: %lld ms\n", end-start);  
  13.     return 0;  
  14. }  
結果:

[root@libmaster time]# ./a.out 
time: 3001 ms



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM