乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C語言獲取系統(tǒng)時(shí)間的幾種方式

       semo_zhang 2011-12-16

      C語言獲取系統(tǒng)時(shí)間的幾種方式

       

      C語言中如何獲取時(shí)間?精度如何?
      1 使用time_t time( time_t * timer ) 精確到秒
      2 使用clock_t clock() 得到的是CPU時(shí)間 精確到1/CLOCKS_PER_SEC秒
      3 計(jì)算時(shí)間差使用double difftime( time_t timer1, time_t timer0 )
      4 使用DWORD GetTickCount() 精確到毫秒
      5 如果使用MFC的CTime類,可以用CTime::GetCurrentTime() 精確到秒
      6 要獲取高精度時(shí)間,可以使用
      BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)
      獲取系統(tǒng)的計(jì)數(shù)器的頻率
      BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
      獲取計(jì)數(shù)器的值
      然后用兩次計(jì)數(shù)器的差除以Frequency就得到時(shí)間。
      7 Multimedia Timer Functions
      The following functions are used with multimedia timers.
      timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime
      //*********************************************************************
      //用標(biāo)準(zhǔn)C實(shí)現(xiàn)獲取當(dāng)前系統(tǒng)時(shí)間的函數(shù)

      一.time()函數(shù)

           time(&rawtime)函數(shù)獲取當(dāng)前時(shí)間距1970年1月1日的秒數(shù),以秒計(jì)數(shù)單位,存于rawtime 中。
      #include "time.h"
      void main ()
      {
      time_t rawtime;
      struct tm * timeinfo;
      time ( &rawtime );
      timeinfo = localtime ( &rawtime );
      printf ( "/007The current date/time is: %s", asctime (timeinfo) );
      exit(0);
      }
      =================

      #include -- 必須的時(shí)間函數(shù)頭文件
      time_t -- 時(shí)間類型(time.h 定義是typedef long time_t; 追根溯源,time_t是long)

      struct tm -- 時(shí)間結(jié)構(gòu),time.h 定義如下:
      int tm_sec;
      int tm_min;
      int tm_hour;
      int tm_mday;
      int tm_mon;
      int tm_year;
      int tm_wday;
      int tm_yday;
      int tm_isdst;
      time ( &rawtime ); -- 獲取時(shí)間,以秒計(jì),從1970年1月一日起算,存于rawtime
      localtime ( &rawtime ); -- 轉(zhuǎn)為當(dāng)?shù)貢r(shí)間,tm 時(shí)間結(jié)構(gòu)
      asctime ()-- 轉(zhuǎn)為標(biāo)準(zhǔn)ASCII時(shí)間格式:
      星期 月 日 時(shí):分:秒 年

      -----------------------------------------------------------------------------
      二.clock()函數(shù),用clock()函數(shù),得到系統(tǒng)啟動以后的毫秒級時(shí)間,然后除以CLOCKS_PER_SEC,就可以換成“秒”,標(biāo)準(zhǔn)c函數(shù)。
      clock_t clock ( void );
      #include
      clock_t t = clock();
      long sec = t / CLOCKS_PER_SEC;
      他是記錄時(shí)鐘周期的,實(shí)現(xiàn)看來不會很精確,需要試驗(yàn)驗(yàn)證;
      ---------------------------------------------------------------------------
      三.gettime(&t); 據(jù)說tc2.0的time結(jié)構(gòu)含有毫秒信息
      #include
      #include
      int main(void)
      {
      struct time t;
      gettime(&t);
      printf("The current time is: %2d:%02d:%02d.%02d/n",
      t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);
      return 0;
      }
      time 是一個(gè)結(jié)構(gòu)體,, 其中成員函數(shù) ti_hund 是毫秒。。。

      --------------------------------------------------------------------------------
      四.GetTickCount(),這個(gè)是windows里面常用來計(jì)算程序運(yùn)行時(shí)間的函數(shù);
      DWORD dwStart = GetTickCount();
      //這里運(yùn)行你的程序代碼
      DWORD dwEnd = GetTickCount();
      則(dwEnd-dwStart)就是你的程序運(yùn)行時(shí)間, 以毫秒為單位
      這個(gè)函數(shù)只精確到55ms,1個(gè)tick就是55ms。
      --------------------------------------------------------------------------------
      五.timeGetTime()t,imeGetTime()基本等于GetTickCount(),但是精度更高
      DWORD dwStart = timeGetTime();
      //這里運(yùn)行你的程序代碼
      DWORD dwEnd = timeGetTime();
      則(dwEnd-dwStart)就是你的程序運(yùn)行時(shí)間, 以毫秒為單位
      雖然返回的值單位應(yīng)該是ms,但傳說精度只有10ms。
      =========================================
      //*****************************************************************Unix
      ##unix時(shí)間相關(guān),也是標(biāo)準(zhǔn)庫的
      //*********************************************************************
      1.timegm函數(shù)只是將struct tm結(jié)構(gòu)轉(zhuǎn)成time_t結(jié)構(gòu),不使用時(shí)區(qū)信息;
      time_t timegm(struct tm *tm);
      2.mktime使用時(shí)區(qū)信息
      time_t mktime(struct tm *tm);
      timelocal 函數(shù)是GNU擴(kuò)展的與posix函數(shù)mktime相當(dāng)
      time_t timelocal (struct tm *tm);
      3.gmtime函數(shù)只是將time_t結(jié)構(gòu)轉(zhuǎn)成struct tm結(jié)構(gòu),不使用時(shí)區(qū)信息;
      struct tm * gmtime(const time_t *clock);
      4.localtime使用時(shí)區(qū)信息
      struct tm * localtime(const time_t *clock);
      1.time獲取時(shí)間,stime設(shè)置時(shí)間
      time_t t;
      t = time(&t);
      2.stime其參數(shù)應(yīng)該是GMT時(shí)間,根據(jù)本地時(shí)區(qū)設(shè)置為本地時(shí)間;
      int stime(time_t *tp)
      3.UTC=true 表示采用夏時(shí)制;
      4.文件的修改時(shí)間等信息全部采用GMT時(shí)間存放,不同的系統(tǒng)在得到修改時(shí)間后通過localtime轉(zhuǎn)換成本地時(shí)間;
      5.設(shè)置時(shí)區(qū)推薦使用setup來設(shè)置;
      6.設(shè)置時(shí)區(qū)也可以先更變/etc/sysconfig/clock中的設(shè)置 再將ln -fs /usr/share/zoneinfo/xxxx/xxx /etc/localtime 才能重效
      time_t只能表示68年的范圍,即mktime只能返回1970-2038這一段范圍的time_t
      看看你的系統(tǒng)是否有time_t64,它能表示更大的時(shí)間范圍
      //***************************************************************windows
      ##Window里面的一些不一樣的
      //*********************************************************************

      一.CTime () 類
      VC編程一般使用CTime類 獲得當(dāng)前日期和時(shí)間

      CTime t = GetCurrentTime();
      SYSTEMTIME 結(jié)構(gòu)包含毫秒信息
      typedef struct _SYSTEMTIME {
      WORD wYear;
      WORD wMonth;
      WORD wDayOfWeek;
      WORD wDay;
      WORD wHour;
      WORD wMinute;
      WORD wSecond;
      WORD wMilliseconds;
      } SYSTEMTIME, *PSYSTEMTIME;
      SYSTEMTIME t1;
      GetSystemTime(&t1)
      CTime curTime(t1);
      WORD ms = t1.wMilliseconds;
      SYSTEMTIME sysTm;
      ::GetLocalTime(&sysTm);
      在time.h中的_strtime() //只能在windows中用
      char t[11];
      _strtime(t);
      puts(t);

      //*****************************
      獲得當(dāng)前日期和時(shí)間
      CTime tm=CTime::GetCurrentTime();
      CString str=tm.Format("%Y-%m-%d");
      在VC中,我們可以借助CTime時(shí)間類,獲取系統(tǒng)當(dāng)前日期,具體使用方法如下:
      CTime t = CTime::GetCurrentTime(); //獲取系統(tǒng)日期,存儲在t里面
      int d=t.GetDay(); //獲得當(dāng)前日期
      int y=t.GetYear(); //獲取當(dāng)前年份
      int m=t.GetMonth(); //獲取當(dāng)前月份
      int h=t.GetHour(); //獲取當(dāng)前為幾時(shí)
      int mm=t.GetMinute(); //獲取當(dāng)前分鐘
      int s=t.GetSecond(); //獲取當(dāng)前秒
      int w=t.GetDayOfWeek(); //獲取星期幾,注意1為星期天,7為星期六

      二.CTimeSpan類
      如果想計(jì)算兩段時(shí)間的差值,可以使用CTimeSpan類,具體使用方法如下:
      CTime t1( 1999, 3, 19, 22, 15, 0 );
      CTime t = CTime::GetCurrentTime();
      CTimeSpan span=t-t1; //計(jì)算當(dāng)前系統(tǒng)時(shí)間與時(shí)間t1的間隔
      int iDay=span.GetDays(); //獲取這段時(shí)間間隔共有多少天
      int iHour=span.GetTotalHours(); //獲取總共有多少小時(shí)
      int iMin=span.GetTotalMinutes();//獲取總共有多少分鐘
      int iSec=span.GetTotalSeconds();//獲取總共有多少秒


      ------------------------------------------------------------------------------

      三._timeb()函數(shù)
      _timeb定義在SYS/TIMEB.H,有四個(gè)fields
      dstflag
      millitm
      time
      timezone
      void _ftime( struct _timeb *timeptr );
      struct _timeb timebuffer;
      _ftime( &timebuffer );
      取當(dāng)前時(shí)間:文檔講可以到ms,有人測試,好象只能到16ms!

       

       

      四.設(shè)置計(jì)時(shí)器
      定義TIMER ID
      #define TIMERID_JISUANFANGSHI 2
      在適當(dāng)?shù)牡胤皆O(shè)置時(shí)鐘,需要開始其作用的地方;
      SetTimer(TIMERID_JISUANFANGSHI,200,NULL);
      在不需要定時(shí)器的時(shí)候的時(shí)候銷毀掉時(shí)鐘
      KillTimer(TIMERID_JISUANFANGSHI);
      對應(yīng)VC程序的消息映射
      void CJisuan::OnTimer(UINT nIDEvent)
      {switch(nIDEvent)}
      ---------------------------------------------------------------------------------------
      ##如何設(shè)定當(dāng)前系統(tǒng)時(shí)間---------------------------------------windows
      SYSTEMTIME m_myLocalTime,*lpSystemTime;
      m_myLocalTime.wYear=2003;
      m_myLocalTime.wM;
      m_myLocalTime.wDay=1;
      m_myLocalTime.wHour=0;
      m_myLocalTime.wMinute=0;
      m_myLocalTime.wSec;
      m_myLocalTime.wMillisec;
      lpSystemTime=&m_myLocalTime;
      if( SetLocalTime(lpSystemTime) ) //此處換成 SetSystemTime( )也不行
      MessageBox("OK !");
      else
      MessageBox("Error !");
      SYSTEMTIME m_myLocalTime,*lpSystemTime;
      m_myLocalTime.wYear=2003;
      m_myLocalTime.wM;
      m_myLocalTime.wDay=1;
      lpSystemTime=&m_myLocalTime;
      if( SetDate(lpSystemTime) ) //此處換成 SetSystemTime( )也不行
      MessageBox("OK !");
      else
      MessageBox("Error !");

       

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多