time,gettimeofday,clock_gettime,_ftime
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                time,gettimeofday,clock_gettime,_ftime
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                
                            
                            
                             1 time()提供了秒級(jí)的精確度  
 2   
 3 1、頭文件 <time.h>  
 4 2、函數(shù)原型  
 5 time_t time(time_t * timer)   
 6 函數(shù)返回從TC1970-1-1 0:0:0開始到現(xiàn)在的秒數(shù)  
 7   
 8 用time()函數(shù)結(jié)合其他函數(shù)(如:localtime、gmtime、asctime、ctime)可以獲得當(dāng)前系統(tǒng)時(shí)間或是標(biāo)準(zhǔn)時(shí)間。  
 9  
10 #include <time.h>  
11 #include <stdio.h>   
12 int main(void)  
13 {  
14     time_t t;   
15     t = time(NULL);  
16     printf("The number of seconds since January 1, 1970 is %ld",t);  
17       
18     return 0;  
19 }   
20  
21 #include <stdio.h>  
22 #include <stddef.h>  
23 #include <time.h>   
24 int main(void)  
25 {  
26     time_t timer;//time_t就是long int 類型   
27     struct tm *tblock;  
28     timer = time(NULL);//這一句也可以改成time(&timer);   
29     tblock = localtime(&timer);  
30     printf("Local time is: %s/n",asctime(tblock));  
31       
32     return 0;  
33 }    
                        
                        
                        ?
?
1 _ftime()提供毫秒級(jí)的精確度 2 3 1、頭文件 <sys/types.h> and <sys/timeb.h> 4 2、函數(shù)原型 5 void _ftime(struct _timeb *timeptr); 6 參數(shù)說明: 7 struct _timeb 8 { 9 time_t time; 10 unsigned short millitm; 11 short timezone; 12 short dstflag; 13 }; 14 15 #include <stdio.h> 16 #include <sys/timeb.h> 17 #include <time.h> 18 19 void main( void ) 20 { 21 struct _timeb timebuffer; 22 char *timeline; 23 24 _ftime( &timebuffer ); 25 timeline = ctime( & ( timebuffer.time ) ); 26 27 printf( "The time is %.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20] ); 28 }?
?
1 gettimeofday()提供了微秒級(jí)的精確度 2 3 1、頭文件 <time.h> 4 2、函數(shù)原型 5 int gettimeofday(struct timeval *tv, struct timezone *tz); 6 7 gettimeofday()會(huì)把目前的時(shí)間由tv所指的結(jié)構(gòu)返回,當(dāng)?shù)貢r(shí)區(qū)的信息則放到tz所指的結(jié)構(gòu)中(可用NULL)。 8 參數(shù)說明: 9 timeval結(jié)構(gòu)定義為: 10 struct timeval 11 { 12 long tv_sec; /*秒*/ 13 long tv_usec; /*微秒*/ 14 }; 15 timezone 結(jié)構(gòu)定義為: 16 struct timezone 17 { 18 int tz_minuteswest; /*和Greenwich 時(shí)間差了多少分鐘*/ 19 int tz_dsttime; /*日光節(jié)約時(shí)間的狀態(tài)*/ 20 }; 21 上述兩個(gè)結(jié)構(gòu)都定義在/usr/include/sys/time.h。tz_dsttime 所代表的狀態(tài)如下 22 DST_NONE /*不使用*/ 23 DST_USA /*美國*/ 24 DST_AUST /*澳洲*/ 25 DST_WET /*西歐*/ 26 DST_MET /*中歐*/ 27 DST_EET /*東歐*/ 28 DST_CAN /*加拿大*/ 29 DST_GB /*大不列顛*/ 30 DST_RUM /*羅馬尼亞*/ 31 DST_TUR /*土耳其*/ 32 DST_AUSTALT /*澳洲(1986年以后)*/ 33 34 返回值: 成功則返回0,失敗返回-1,錯(cuò)誤代碼存于errno。附加說明EFAULT指針tv和tz所指的內(nèi)存空間超出存取權(quán)限。 35 36 #include<stdio.h> 37 #include<time.h> 38 int main(void) 39 { 40 struct timeval tv; 41 struct timezone tz; 42 43 gettimeofday (&tv , &tz); 44 45 printf(“tv_sec; %d/n”, tv,.tv_sec) ; 46 printf(“tv_usec; %d/n”,tv.tv_usec); 47 48 printf(“tz_minuteswest; %d/n”, tz.tz_minuteswest); 49 printf(“tz_dsttime, %d/n”,tz.tz_dsttime); 50 51 return 0; 52 }?
1 clock_gettime( ) 提供了納秒級(jí)的精確度 2 3 1、頭文件 <time.h> 4 2、編譯&鏈接。在編譯鏈接時(shí)需加上 -lrt ;因?yàn)樵趌ibrt中實(shí)現(xiàn)了clock_gettime函數(shù) 5 3、函數(shù)原型 6 int clock_gettime(clockid_t clk_id, struct timespect *tp); 7 參數(shù)說明: 8 clockid_t clk_id 用于指定計(jì)時(shí)時(shí)鐘的類型,有以下4種: 9 CLOCK_REALTIME:系統(tǒng)實(shí)時(shí)時(shí)間,隨系統(tǒng)實(shí)時(shí)時(shí)間改變而改變,即從UTC1970-1-1 0:0:0開始計(jì)時(shí),中間時(shí)刻如果系統(tǒng)時(shí)間被用戶該成其他,則對(duì)應(yīng)的時(shí)間相應(yīng)改變 10 CLOCK_MONOTONIC:從系統(tǒng)啟動(dòng)這一刻起開始計(jì)時(shí),不受系統(tǒng)時(shí)間被用戶改變的影響 11 CLOCK_PROCESS_CPUTIME_ID:本進(jìn)程到當(dāng)前代碼系統(tǒng)CPU花費(fèi)的時(shí)間 12 CLOCK_THREAD_CPUTIME_ID:本線程到當(dāng)前代碼系統(tǒng)CPU花費(fèi)的時(shí)間 13 struct timespect *tp用來存儲(chǔ)當(dāng)前的時(shí)間,其結(jié)構(gòu)如下: 14 struct timespec 15 { 16 time_t tv_sec; /* seconds */ 17 long tv_nsec; /* nanoseconds */ 18 }; 19 返回值。0成功,-1失敗 20 21 #include<stdio.h> 22 #include<time.h> 23 int main() 24 { 25 struct timespec ts; 26 27 clock_gettime(CLOCK_REALTIME, &ts); 28 printf("CLOCK_REALTIME: %d, %d", ts.tv_sec, ts.tv_nsec); 29 30 clock_gettime(CLOCK_MONOTONIC, &ts);//打印出來的時(shí)間跟 cat /proc/uptime 第一個(gè)參數(shù)一樣 31 printf("CLOCK_MONOTONIC: %d, %d", ts.tv_sec, ts.tv_nsec); 32 33 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); 34 printf("CLOCK_PROCESS_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec); 35 36 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); 37 printf("CLOCK_THREAD_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec); 38 39 printf("/n%d/n", time(NULL)); 40 41 return 0; 42 } 43 /proc/uptime里面的兩個(gè)數(shù)字分別表示: 44 the uptime of the system (seconds), and the amount of time spent in idle process (seconds). 45 把第一個(gè)數(shù)讀出來,那就是從系統(tǒng)啟動(dòng)至今的時(shí)間,單位是秒?
轉(zhuǎn)載于:https://www.cnblogs.com/li-hao/archive/2013/04/26/3044438.html
總結(jié)
以上是生活随笔為你收集整理的time,gettimeofday,clock_gettime,_ftime的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Windows Error Report
- 下一篇: 安卓开发环境搭建(转)
