linux/unix 下精確擷取系統時間

來源:互聯網
上載者:User

在分析LIVE555代碼時 偶獲:

if (fIsFirstPacket) {    // Record the fact that we're starting to play now:    gettimeofday(&fNextSendTime, NULL);  }

UNIX及Linux的時間系統是由「新紀元時間」Epoch開始計算起,單位為秒。Epoch是指定為1970年1月1日淩晨零點零分零秒,格林威治時間。目前大部份的UNIX系統都是用32位來記錄時間,正值表示為1970以後,負值則表示1970年以前。

下面看下time.h中的time函數,sys/time.h中的gettimeofday函數

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

int main(){
    time_t tt;
    struct timeval tv;
    tt=time(NULL);
    gettimeofday(&tv,NULL);
    printf("time_t sec is %d\n",tt);
    printf("gettimeofday tv_sec is %d, usec is %d \n",tv.tv_sec,tv.tv_usec);
}

PS: time 與 gettimeofday 兩個函數得到的都是從Epoch開始到當前的秒數(tt=tv.tv_sec),而後者還能得到更精細的微秒級結果,即tv_sec*(10^6)+tv_usec為從Epoch開始到當前的微秒數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.