电竞比分网-中国电竞赛事及体育赛事平台

分享

time

 海漩渦 2016-03-18
  1. #include <stdio.h> 
  2.   #include <stdlib.h> /* 包含標準庫頭文件 */ 
  3.   #include <sys/time.h> 
  4.     
  5.   int main(int argc, char **argv) 
  6.   { 
  7.   struct timeval start,stop,diff; 
  8.   gettimeofday(&start,0); 
  9.                     //做你要做的事... 
  10.   gettimeofday(&stop,0); 
  11.   timeval_subtract(&diff,&start,&stop); 
  12.                     printf("總計用時:%d 微秒\n",diff.tv_usec); 
  13.   } 
  14.     
  15.   /** 
  16.       * 計算兩個時間的間隔,得到時間差 
  17.       * @param struct timeval* resule 返回計算出來的時間 
  18.       * @param struct timeval* x 需要計算的前一個時間 
  19.       * @param struct timeval* y 需要計算的后一個時間 
  20.       * return -1 failure ,0 success 
  21.   **/ 
  22.   int timeval_subtract(struct timeval* result, struct timeval* x, struct timeval* y) 
  23.   { 
  24.         int nsec; 
  25.     
  26.         if ( x->tv_sec>y->tv_sec ) 
  27.                   return -1; 
  28.     
  29.         if ( (x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec) ) 
  30.                   return -1; 
  31.     
  32.         result->tv_sec = ( y->tv_sec-x->tv_sec ); 
  33.         result->tv_usec = ( y->tv_usec-x->tv_usec ); 
  34.     
  35.         if (result->tv_usec<0) 
  36.         { 
  37.                   result->tv_sec -= 1; 
  38.                   result->tv_usec+=1000000; 
  39.         } 
  40.     
  41.         return 0; 
  42.   }


=========================================================================
  1. --------------------------------------------------
  2. #include <sys/time.h> 
  3.   #include <unistd.h> 
  4.   int gettimeofday(struct timeval *tv,struct timezone *tz); 
  5.   功能:將目前的時間以tv所指的結(jié)構(gòu)返回。 
  6.   struct timeval{ 
  7.           long tv_sec;//秒 
  8.           long tv_usec;//微秒 
  9.   }; 
  10.   將其中的tv_usec轉(zhuǎn)換到毫秒即可。 
  11.   timezone結(jié)構(gòu)自己查吧
  12.  
  13.  
  14.  
  15. 附錄:
  16. -------------------------------------------------
  17. Linux下獲得系統(tǒng)時間的C語言的實現(xiàn)方法 
  18.   #include<time.h> //C語言的頭文件 
  19.   #include<stdio.h> //C語言的I/
  20.     
  21.   void main() 
  22.   { 
  23.   time_t now; //實例化time_t結(jié)構(gòu) 
  24.   struct tm *timenow; //實例化tm結(jié)構(gòu)指針 
  25.   time(&now); 
  26.   //time函數(shù)讀取現(xiàn)在的時間(國際標準時間非北京時間),然后傳值給now 
  27.     
  28.   timenow = localtime(&now); 
  29.   //localtime函數(shù)把從time取得的時間now換算成你電腦中的時間(就是你設(shè)置的地區(qū)) 
  30.     
  31.   printf("Local time is %s\n",asctime(timenow)); 
  32.   //上句中asctime函數(shù)把時間轉(zhuǎn)換成字符,通過printf()函數(shù)輸出 
  33.   } 
  34.     
  35.   注釋:time_t是一個在time.h中定義好的結(jié)構(gòu)體。而tm結(jié)構(gòu)體的原形如下: 
  36.     
  37.   struct tm 
  38.   { 
  39.       int tm_sec;//seconds 0-61 
  40.       int tm_min;//minutes 1-59 
  41.       int tm_hour;//hours 0-23 
  42.       int tm_mday;//day of the month 1-31 
  43.       int tm_mon;//months since jan 0-11 
  44.       int tm_year;//years from 1900 
  45.       int tm_wday;//days since Sunday, 0-
  46.       int tm_yday;//days since Jan 1, 0-365 
  47.       int tm_isdst;//Daylight Saving time indicator 
  48.   };

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多