Linux應用開發(六):時間編程

來源:互聯網
上載者:User

一、雞肋

   UTC,Coordinated Universal Time,國際標準時間間,就是大家熟知的格林威治時間(Greenwich Mean Time,GMT)

   Calendar Time:日曆時間,所謂的日曆時間就是用“從一個標準的時間點(如1970年1月1日0點),到此時經過的秒數”來表示的時間

二、時間編程

   1、擷取日曆時間:time_t time(time_t * tloc);

      怎麼用:

         (1)需要引入標頭檔:#include <time.h>

         (2)time_t,的原型是 typedef long time_t

         (3)擷取從1970年1月1日0點到現在所經曆的秒數

         (4)如果time的形參不為NULL,則此函數還可以把結果返回到tloc指向的變數中去

   2、時間轉化: 

         (1)struct tm * gmtime(const time_t * timep);

         (2)struct tm * localtime(const time_t * timep) 

      怎麼用:

         (1)將日曆時間轉化為grrnwich time(gm)格林威治時間,將結果儲存到TM結構體中

         (2)將日曆時間轉化為本地時間,將結果儲存到TM結構體中

         (3)看下tm結構的廬山真面目:              

struct tm{     int tm_sec;  //秒值     int tm_min;  //分鐘值     int tm_hour; //小時值     int tm_mday; //本月的第幾天     int tm_mon;  //本年的第幾月     int tm_year; //tm_year+1900 = 哪一年     int tm_wday; //本周第幾天     int tm_yday; //本年第幾天     int tm_isdst;//日光節約時間}

       3、時間顯示函數:

         (1)將tm格式的時間轉化為字串,如 Sat Jul 30 08:43:03 2005

            char * asctime(const struct tm * tm);

        (2)將日曆時間轉化為本地時間的字串形式,如Jul 30 08:43:03 2005

           
char * ctime(const time_t * timep);

   4、擷取今天的時間函數:int gettimeofday(struct timeval * tv,struct timezone * tz);

      怎麼用:

        (1)擷取從今天淩晨0點開始到現在所經曆的秒數和微秒數,將結果儲存到timeval這個結構中,它常用於計算事件的耗時、執行時間等。

       
(2)timeval的結構體樣子:

             stuct timeval{

               
int tv_sec; //秒數

               
int tv_usec;//微妙數

             }

   5、睡眠函數:

       
(1)使程式睡眠seconds秒:unsigned int sleep(unsigned int seconds) ;

       
(2)使程式睡眠usec微秒:void usleep(unsigned long usec);

 

          

聯繫我們

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