C++ 两个 SYSTEMTIME 变量之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8699069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
difference between two SYSTEMTIME variable
提问by kakush
I want to get difference between two SYSTEMTIME variable. I saw someone asked this question here before, but he was told to convert both SYSTEMTIME structures to FILETIME.. Is there another way to get the difference?
我想得到两个 SYSTEMTIME 变量之间的差异。我之前在这里看到有人问过这个问题,但他被告知将两个 SYSTEMTIME 结构都转换为 FILETIME .. 有没有另一种方法来获得差异?
SYSTEMTIME st;
GetSystemTime(&st);
---some code here---
---这里有一些代码---
SYSTEMTIME st2;
GetSystemTime(&st2);
st-st2?
st-st2?
回答by Andre Kirpitch
SYSTEMTIME operator-(const SYSTEMTIME& pSr,const SYSTEMTIME& pSl)
{
SYSTEMTIME t_res;
FILETIME v_ftime;
ULARGE_INTEGER v_ui;
__int64 v_right,v_left,v_res;
SystemTimeToFileTime(&pSr,&v_ftime);
v_ui.LowPart=v_ftime.dwLowDateTime;
v_ui.HighPart=v_ftime.dwHighDateTime;
v_right=v_ui.QuadPart;
SystemTimeToFileTime(&pSl,&v_ftime);
v_ui.LowPart=v_ftime.dwLowDateTime;
v_ui.HighPart=v_ftime.dwHighDateTime;
v_left=v_ui.QuadPart;
v_res=v_right-v_left;
v_ui.QuadPart=v_res;
v_ftime.dwLowDateTime=v_ui.LowPart;
v_ftime.dwHighDateTime=v_ui.HighPart;
FileTimeToSystemTime(&v_ftime,&t_res);
return t_res;
}
回答by bobbymcr
It says pretty clearly on the MSDN documentation:
It is not recommended that you add and subtract values from the SYSTEMTIME structure to obtain relative times. Instead, you should
- Convert the SYSTEMTIME structure to a FILETIME structure.
- Copy the resulting FILETIME structure to a ULARGE_INTEGER structure.
- Use normal 64-bit arithmetic on the ULARGE_INTEGER value.
不建议您从 SYSTEMTIME 结构中添加和减去值来获取相对时间。相反,你应该
- 将 SYSTEMTIME 结构转换为 FILETIME 结构。
- 将生成的 FILETIME 结构复制到 ULARGE_INTEGER 结构。
- 对 ULARGE_INTEGER 值使用普通的 64 位算术。
Why not do exactly that?
为什么不这样做呢?
回答by Jo?o Augusto
ft1 and ft2 are filetime structures
ft1 和 ft2 是文件时间结构
ULARGE_INTEGER ul1;
ul1.LowPart = ft1.dwLowDateTime;
ul1.HighPart = ft1.dwHighDateTime;
ULARGE_INTEGER ul2;
ul2.LowPart = ft2.dwLowDateTime;
ul2.HighPart = ft2.dwHighDateTime;
ul2.QuadPart -= ul1.QuadPart;
Difference in Milliseconds...
以毫秒为单位的差异...
ULARGE_INTEGER uliRetValue;
uliRetValue.QuadPart = 0;
uliRetValue = ul2;
uliRetValue.QuadPart /= 10;
uliRetValue.QuadPart /= 1000; // To Milliseconds