Windows中如何计算时间间隔(2)使用SYSTEMTIME和FILETIME

SYSTEMTIME和FILETIME是两个结构体,定义如下:

typedef struct _SYSTEMTIME { 
  WORD wYear; 
  WORD wMonth; 
  WORD wDayOfWeek; 
  WORD wDay; 
  WORD wHour; 
  WORD wMinute; 
  WORD wSecond; 
  WORD wMilliseconds; 
} SYSTEMTIME;
 
This structure is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601.
typedef struct _FILETIME { 
  DWORD dwLowDateTime; 
  DWORD dwHighDateTime; 
} FILETIME; 

Members

dwLowDateTime
Specifies the low 32 bits of the Win32 date/time value.
dwHighDateTime
Specifies the upper 32 bits of the Win32 date/time value.

获取相对时间的常用方法如下,.

The following list shows tasks to perform instead:

  • 使用SystemTimeToFileTime()转换SYSTEMTIME 为 FILETIME 结构.
  • FILETIME 的值赋给 ULARGE_INTEGER
  • 使用ULARGE_INTEGER中的64位值计算差值。
typedef union _ULARGE_INTEGER {
  struct {
    DWORD LowPart;
    DWORD HighPart;
  };
  struct {
    DWORD LowPart;
    DWORD HighPart;
  } u;
  ULONGLONG QuadPart;
} ULARGE_INTEGER, 
*PULARGE_INTEGER;

 

Members

LowPart
Low-order 32 bits.
HighPart
High-order 32 bits.
u
LowPart
Low-order 32 bits.
HighPart
High-order 32 bits.
QuadPart Unsigned 64-bit integer.

 

代码如下:

SYSTEMTIME sTime1;
SYSTEMTIME sTime2;
...
...

ULARGE_INTEGER fTime1;/*FILETIME*/
ULARGE_INTEGER fTime2;/*FILETIME*/


SystemTimeToFileTime(&sTime1,(FILETIME*)&fTime1);
SystemTimeToFileTime(&sTime2,(FILETIME*)&fTime2);

unsigned __int64 dft=fTime2.QuadPart-fTime1.QuadPart;

阅读更多
个人分类: C++/MFC WINDOWS C++/SDK
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭