vc擷取時間函數總結

來源:互聯網
上載者:User

如何擷取時間?精度如何?                                                                   

A:

1
使用time_t time( time_t * timer )       精確到秒

  計算時間差使用double
difftime( time_t timer1, time_t timer0 )

2
使用clock_t clock() 得到的是CPU時間       精確到1/CLOCKS_PER_SEC秒

3
使用DWORD GetTickCount() 得到的是系統啟動並執行時間 精確到毫秒

4
如果使用MFC的CTime類,可以用CTime::GetCurrentTime() 精確到秒

5
要擷取高精度時間,可以使用

      
BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)擷取系統的計數器的頻率

      
BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)擷取計數器的值

      
然後用兩次計數器的差除以Frequency就得到時間。

6
還有David的文章中提到的方法:

      
Multimedia Timer Functions

      
The following functions are used with multimedia timers.

      
timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime

      
timeGetTime/timeKillEvent/TimeProc/timeSetEvent     精度很高

Q:GetTickCount()函數,說是毫秒記數,是真的嗎,還是精確到55毫秒?

A:

GetTickCount()和GetCurrentTime()都只精確到55ms(1個tick就是55ms)。如果要精確到毫秒,應該使用timeGetTime函數或QueryPerformanceCounter函數。具體例子可以參考QA001022
"VC++中使用高精度定時器"、QA001813
"如何在Windows實現準確的定時"和QA004842
"timeGetTime函數延時不準"。

Q:vc++怎樣擷取系統時間,傳回值是什麼類型的變數呢?

GetSystemTime返回的是格林威志標準時間

GetLocalTime,和上面用法一樣,返回的是你所在地區的時間,中國返回的是北京時間

VOID
GetSystemTime(

LPSYSTEMTIME
lpSystemTime // address of system time structure

);

函數就可以獲得了,其中LPSYSTEMTIME
是個結構體

含:年,月,日,周幾,小時,分,秒,毫秒。

以下是Time的MSDN文檔:

Compatibility in
the Introduction.

Libraries

 

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail
version

 

Return
Value

time
returns the time in elapsed seconds. There is no error return.

Parameter

timer

Storage
location for time

Remarks

The
time function returns the number of seconds elapsed since midnight (00:00:00),
January 1, 1970, coordinated universal time, according to the system clock. The
return value is stored in the location given by timer. This parameter may be
NULL, in which case the return value is not stored.

  Example

/*
TIMES.C illustrates various time and date functions including:

 *       
time              _ftime            ctime         asctime

 *       
localtime         gmtime            mktime        _tzset

 *       
_strtime          _strdate          strftime

 *

 *
Also the global variable:

 *       
_tzname

 */

#include
<time.h>

#include
<stdio.h>

#include
<sys/types.h>

#include
<sys/timeb.h>

#include
<string.h>

void
main()

{

     
char tmpbuf[128], ampm[] = "AM";

     
time_t ltime;

     
struct _timeb tstruct;

     
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };

     
/* Set time zone from TZ environment variable. If TZ is not set,

      
* the operating system is queried to obtain the default value

      
* for the variable.

      
*/

     
_tzset();

     
/* Display operating system-style date and time. */

     
_strtime( tmpbuf );

     
printf( "OS time:/t/t/t/t%s/n", tmpbuf );

     
_strdate( tmpbuf );

     
printf( "OS date:/t/t/t/t%s/n", tmpbuf );

     
/* Get UNIX-style time and display as number and string. */

     
time( &ltime );

     
printf( "Time in seconds since UTC 1/1/70:/t%ld/n", ltime );

     
printf( "UNIX time and date:/t/t/t%s", ctime( &ltime ) );

     
/* Display UTC. */

     
gmt = gmtime( &ltime );

     
printf( "Coordinated universal time:/t/t%s", asctime( gmt ) );

     
/* Convert to time structure and adjust for PM if necessary. */

     
today = localtime( &ltime );

     
if( today->tm_hour > 12 )

     
{

    
strcpy( ampm, "PM" );

    
today->tm_hour -= 12;

     
}

     
if( today->tm_hour == 0 )    /* Adjust if midnight hour. */

    
today->tm_hour = 12;

     
/* Note how pointer addition is used to skip the first 11

      
* characters and printf is used to trim off terminating

      
* characters.

      
*/

     
printf( "12-hour time:/t/t/t/t%.8s %s/n",

        
asctime( today ) + 11, ampm );

     
/* Print additional time information. */

     
_ftime( &tstruct );

     
printf( "Plus milliseconds:/t/t/t%u/n", tstruct.millitm );

     
printf( "Zone difference in seconds from UTC:/t%u/n",

              
tstruct.timezone );

     
printf( "Time zone name:/t/t/t/t%s/n", _tzname[0] );

     
printf( "Daylight savings:/t/t/t%s/n",

              
tstruct.dstflag ? "YES" : "NO" );

     
/* Make time for noon on Christmas, 1993. */

     
if( mktime( &xmas ) != (time_t)-1 )

    
printf( "Christmas/t/t/t/t%s/n", asctime( &xmas ) );

     
/* Use time structure to build a customized time string. */

     
today = localtime( &ltime );

     
/* Use strftime to build a customized time string. */

     
strftime( tmpbuf, 128,

          
"Today is %A, day %d of %B in the year %Y./n", today );

     
printf( tmpbuf );

}

 

Output

OS
time:                                  21:51:03

OS
date:                                  05/03/94

Time
in seconds since UTC 1/1/70:         768027063

UNIX
time and date:                       Tue May 03 21:51:03 1994

Coordinated
universal time:               Wed May 04 04:51:03 1994

12-hour
time:                             09:51:03 PM

Plus
milliseconds:                        279

Zone
difference in seconds from UTC:      480

Time
zone name:                          

Daylight
savings:                         YES

Christmas                                
Sat Dec 25 12:00:00 1993

Today
is Tuesday, day 03 of May in the year 1994.

 

聯繫我們

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