邀朋友种豆,一起分享吧
喜欢购买正品行货?那就去品牌街

FILETIME使用注意事项及两个FILETIME时间差计算方法及

来自:种豆 时间:2017-12-29 阅读:6040次 原文链接

FILETIME的时间单位

FILETIME表示的时间是从UTC1601年1月1日起的时间,时间单位为100ns

Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).

FileTimeToSystemTime的注意事项

FileTimeToSystemTime(__in  CONST FILETIME *lpFileTime,__out LPSYSTEMTIME lpSystemTime)

这是windows的一个API,用来将FILETIME转换为SYSTEMTIME,这个函数很有用它将FILETIME中的时间戳格式可以转换为时间格式,转换的时间是格林尼治时间,比北京时间慢8小时(北京时间=格林尼治时间+8),如果按北京时间算,应该加8小时。

HANDLE hFind;
WIN32_FIND_DATAW wfd;
//用于存储文件的相关信息
//wfd变量必须定义,否则当文件存在时,
//将因为没有存储的位置而出现异常
hFind = FindFirstFile(fileName, &wfd);
SYSTEMTIME systemTime;
char systemTimeBuf[255];
wchar_t wsystemTimeBuf[
255];
FileTimeToSystemTime(
&(wfd.ftLastWriteTime), &systemTime);
systemTime.wHour
= (systemTime.wHour+8) % 24; //记着对24取模
sprintf(systemTimeBuf, "%d-%d-%d d:%d:%d", systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute, systemTime.wSecond);
MultiByteToWideChar(CP_ACP,
0, systemTimeBuf, strlen(systemTimeBuf) 1, wsystemTimeBuf, sizeof(wsystemTimeBuf)/sizeof(wsystemTimeBuf[0]));
::MessageBox(NULL, wsystemTimeBuf, L
"末次访问时间", MB_OK);

取得两个 FILETIME 的时间差

如果为SYSTEMTIME,可以通过SystemTimeToFileTime先转到FILETIME

#define NOMINMAX
#include
<algorithm>
#include
<windows.h>
SYSTEMTIME systemtime_difference(SYSTEMTIME
const& st1, SYSTEMTIME const& st2)
{
FILETIME ft1;
SystemTimeToFileTime(
&st1, &ft1);
ULARGE_INTEGER
const u1 = { ft1.dwLowDateTime, ft1.dwHighDateTime };
FILETIME ft2;
SystemTimeToFileTime(
&st2, &ft2);
ULARGE_INTEGER
const u2 = { ft2.dwLowDateTime, ft2.dwHighDateTime };
ULARGE_INTEGER u3;
u3.QuadPart
= std::max(u1.QuadPart, u2.QuadPart) - std::min(u1.QuadPart, u2.QuadPart);
FILETIME
const ft3 = { u3.LowPart, u3.HighPart };
SYSTEMTIME st3;
FileTimeToSystemTime(
&ft3, &st3);
// FILETIME's starting point is 1601-01-01
st3.wYear -= 1601;
st3.wMonth
-= 1;
st3.wDay
-= 1;
st3.wDayOfWeek
= 0; // useless for this purpose, make it always 0 so it's always ignored
return st3;
}

int main()
{
SYSTEMTIME
const st1 = { 2008, 10, 3, 15, 6, 5, 31, 12 };
SYSTEMTIME
const st2 = { 2009, 12, 0, 27, 10, 7, 59, 56 };
SYSTEMTIME st3
= systemtime_difference(st1, st2);
return 0;
}

关于ULARGE_INTEGER的运算

The ULARGE_INTEGER structure is used to specify a 64-bit unsigned integer value.

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.

Remarks

The ULARGE_INTEGER structure is actually a union. If your compiler has built-in support for 64-bit integers, use the QuadPart member to store the 64-bit integer. Otherwise, use the LowPart and HighPart members to store the 64-bit integer.



 
关于种豆 ┊ 联系我们 ┊ 免责声明 ┊ 发帖须知 ┊ 请提意见 ┊ 站点地图
本站为个人爱好兴趣分享网站,不代表本人观点,如有侵权请联系QQ3033380280进行处理
sowsoy.com 版权所有 Copyright©2010-2021 备案号:蜀ICP备2020025376号-3
Email:sowsoy#hotmail.com