您的位置:首页 > 其它

GetCurrentTime(),GetLocalTime(),GetSystemTime()之间的区别

2014-08-05 09:39 507 查看
MFC下,常用的获取时间方式有以下几种三种:GetSystemTime,GetLocalTime,GetCurrentTime。
SYSTEMTIME sysTm, locTm;

 GetSystemTime(&sysTm);                           //获取格林威治标准时间,与北京时间相差8小时

 GetLocalTime(&locTm);                               //获取本时区时间。如中国即为东八区北京时间

 CTime tm = CTime::GetCurrentTime();       //获取当前时间与1970年1月1日8am的秒数差。

1. GetCurrentTime()

    GetCurrentTime()只和16位版本的windows兼容,在32位windows下最好用gettickcount();

Returns a CTime object that represents the current time.

static CTime WINAPI GetCurrentTime( ) throw( );



Remarks

Returns the current system date and time in Coordinated Universal Time (UTC).


Example

C++

CTime t = CTime::GetCurrentTime();


2. GetLocalTime()

    GetLocalTime()在不同的机器中会有不同的结果,这和你在控制面板中的时区设置有关. 该函数是获取的系统当前所属时区的时间, 比如说, 在北京时区, 那么获取的该时间的时间.


GetLocalTime function

Retrieves the current local date and time.

To retrieve the current date and time in Coordinated Universal Time (UTC) format, use the GetSystemTime function.


Syntax

C++

void WINAPI GetLocalTime(
_Out_  LPSYSTEMTIME lpSystemTime
);



Parameters

lpSystemTime [out]

A pointer to a SYSTEMTIME structure
to receive the current local date and time.


Return value

This function does not return a value.

3. GetSystemTime()
    
     GetSystemTime()获取的格林尼治时间, 是全球标准时间.
      
      SYSTEMTIME stUTC;
      GetSystemTime(&stUTC);
      TCHAR chBuf[nBufSize];
      wsprintf(chBuf,_T("UTC: %u/%u/%u %u:%u:%u:%u %d\r\n"),             
                  stUTC.wYear, stUTC.wMonth, stUTC.wDay,
                  stUTC.wHour, stUTC.wMinute, stUTC.wSecond,
                  stUTC.wMilliseconds,stUTC.wDayOfWeek);


GetSystemTime function

Retrieves the current system date and time. The system time is expressed in Coordinated Universal Time (UTC).

To retrieve the current system date and time in local time, use the GetLocalTime function.


Syntax

C++

void WINAPI GetSystemTime(
_Out_  LPSYSTEMTIME lpSystemTime
);



Parameters

lpSystemTime [out]

A pointer to a SYSTEMTIME structure to receive
the current system date and time. The lpSystemTimeparameter must not be NULL. Using NULL will result in an access violation.


Return value

This function does not return a value or provide extended error information.

4. GetTickCount()

     GetTickCount()获取的是从设备开机后的毫秒数. 不包括系统的挂起时间.

      主要的应用:
          dwOldTime = GetTickCount();
          DoSomeThing();
          dwTimeElapsed = GetTickCount() - dwOldTime;
      获取某段程序执行所需的时间.

Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days.


Syntax

C++

DWORD WINAPI GetTickCount(void);



Parameters

This function has no parameters.


Return value

The return value is the number of milliseconds that have elapsed since the system was started.


Remarks

The resolution of the GetTickCount function is limited to the resolution of the system timer, which is typically in the range of 10 milliseconds to 16 milliseconds. The resolution of the GetTickCount function is not affected
by adjustments made by the GetSystemTimeAdjustment function.

The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days. To avoid this problem, use the GetTickCount64 function.
Otherwise, check for an overflow condition when comparing times.

If you need a higher resolution timer, use a multimedia timer or a high-resolution
timer.

To obtain the time elapsed since the computer was started, retrieve the System Up Time counter in the performance data in the registry key HKEY_PERFORMANCE_DATA. The value returned is an 8-byte value. For more information, see Performance
Counters.

To obtain the time the system has spent in the working state since it was started, use theQueryUnbiasedInterruptTime function.

5. 更好的办法:“now函数” 
    例如:formatdatetime('yyyy ''年'' m ''月'' d ''日''dddd '+'hh:mm:ssAM/PM',now); 
    输出结果: 2001年5月8日星期一19:35:40 PM
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: