linux 下定時計時操作__linux

來源:互聯網
上載者:User

一、用select()函數實現非阻塞時的等待時間,用到結構體struct timeval {},這裡就不多說了。

二、用gettimeofday()可獲得微妙級(0.000001秒)的系統時間,調用兩次gettimeofday(),前後做減法,從而達到定時或者計算時間的目的。

原型:int gettimeofday(struct timeval *tv,struct timezone *tz),會把目前的時間tv所指的結構返回,當地時區的資訊則放到tz所指的結構中。這兩個結構都放在/usr/include/sys/time.h 中。

#include <stdio.h> 
#include <stdlib.h>//malloc要用,沒有的話,會有警告資訊:隱式聲明與內建函數'malloc'不相容。不過警告資訊不用管也沒事

#include <assert.h>
#include <sys/time.h>

int main()
{
float time_use=0;
struct timeval start;
struct timeval end;
//struct timezone tz; //後面有說明
gettimeofday(&start,NULL);//gettimeofday(&start,&tz);結果一樣
printf("start.tv_sec:%d\n",start.tv_sec);
printf("start.tv_usec:%d\n",start.tv_usec);

sleep(3);
gettimeofday(&end,NULL);
printf("end.tv_sec:%d\n",end.tv_sec);
printf("end.tv_usec:%d\n",end.tv_usec);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %f\n",time_use);

//輸出:time_use is 3001410.000000

//下面的採用指標的方式也可以,但是要注意指標類型若不分配記憶體的話,編譯正確,但是運行結果會不對

/*************************************************

struct timeval *start;
struct timeval *end;
//struct timezone *tz;

start=(struct timeval *)malloc( sizeof(struct timeval) );
assert(start!=NULL);
end=(struct timeval *)malloc( sizeof(struct timeval) );
assert(start!=NULL);
gettimeofday(start,NULL);//gettimeofday(start,tz);

printf("start->tv_sec:%d\n",start->tv_sec); //printf("(*start).tv_sec:%d\n",(*start).tv_sec);一樣
printf("start->tv_usec:%d\n",start->tv_usec);

//printf("tz->tz_minuteswest:%d\n",tz->tz_minuteswest);
//printf("tv->tz_dsttime:%d\n",tz->tz_dsttime);
sleep(3);
gettimeofday(end,NULL);//gettimeofday(end,tz);
printf("end->tv_sec:%d\n",end->tv_sec);
printf("end->tv_usec:%d\n",end->tv_usec);
time_use=(end->tv_sec-start->tv_sec)*1000+(end->tv_usec-start->tv_usec)/1000;//毫秒
printf("time_use is %f\n",time_use);

//輸出:time_use is 3001.000000

**********************************************/
/*****************************
struct timeval 

time_t tv_sec; // seconds 
suseconds_t tv_usec; // 微妙10-6
};

struct timezone
{
   int tz_minuteswest;//和格林威治時間差了多少分鐘
   int tz_dsttime;    //日光節約時間的狀態
}
******************************/

}

三、最小到秒的時間的擷取

int time(char cnt) 
{

time_t t; //執行個體化time_t結構

struct tm *timenow1; //執行個體化tm結構指標
struct tm *timenow2; //執行個體化tm結構指標    

time(&t);//time函數讀取現在的時間(國際標準時間非北京時間),然後傳值給t
timenow1=localtime(&t); //localtime函數把從time取得的時間t換算成你電腦中的時間(就是你設定的地區)
printf("the current time1 is: %02d:%02d:%02d\n",timenow1->tm_hour,timenow1->tm_min,timenow1- >tm_sec); 
printf("Local time1 is %s\n",asctime(timenow1)); //上句中asctime函數把時間轉換成字元,通過printf()函數輸出

time(&t);

timenow2=localtime(&t);
printf("the current time2 is: %02d:%02d:%02d\n",timenow2->tm_hour,timenow2->tm_min,timenow1->tm_sec); 
printf("Local time2 is %s\n",asctime(timenow2));

if((timenow2->tm_hour==timenow1->tm_hour) && (timenow2->tm_min==timenow1->tm_min))
{
    n=timenow2->tm_sec-timenow2->tm_sec;
    printf("n is %d\n",n);//秒
   }

//註:如果想獲得國際標準時間,將localtime換成gmtime函數
//註:time_t是一個在time.h中定義好的結構體。而tm結構體的原形如下:

/*
struct tm //最小到秒,#include <time.h> 
{
   int tm_sec;//seconds 0-61
   int tm_min;//minutes 1-59
   int tm_hour;//hours 0-23
   int tm_mday;//day of the month 1-31
   int tm_mon;//months since jan 0-11
   int tm_year;//years from 1900
   int tm_wday;//days since Sunday, 0-6
   int tm_yday;//days since Jan 1, 0-365
   int tm_isdst;//Daylight Saving time indicator
};
*/
}

四、納秒

函數原型:int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)

其中參數timespec定義是:

struct timespec 

         time_t tv_sec; /* seconds * /
         long tv_nsec; /* nanoseconds * /
}

實際應用(部分,不完整):

struct timesepc req; 
struct timespec rem;

int ret; 

req.tv_sec = 2;   //這就表示2秒 
req.tv_nsec = 0; 

ret = nanosleep(&req, &rem); 
  
if (ret < 0) 

//.... 
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.