helper function: compare two SYSTEMTIME, to get time span

来源:互联网 发布:js防水液体怎么用 编辑:程序博客网 时间:2024/05/17 23:44
// testTime.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <windows.h>/// tmNow can get from current time/// tmPrev can get form file already exist/// i want compare two time, get time span. e.g. tmX - tmY = 3 secondLONGLONG SecondTimeSpan(SYSTEMTIME &tmNow, SYSTEMTIME & tmPrev);int _tmain(int argc, _TCHAR* argv[]){    int         iIndex = 0;    DWORD       dwTimeSleep = 0;    LONGLONG    llTimeSpan = 0;    SYSTEMTIME  tmPrev;    SYSTEMTIME  tmNow;    for (iIndex = 0; iIndex < 10; iIndex++)    {        ::GetSystemTime(&tmPrev);        dwTimeSleep = iIndex * 1000;        ::Sleep(dwTimeSleep);        ::GetSystemTime(&tmNow);        llTimeSpan = SecondTimeSpan(tmNow, tmPrev);        _tprintf(L"Sleep(%d Second), time span is %ld Second\r\n", iIndex, llTimeSpan);    }    /** run result    Sleep(0 Second), time span is 0 Second    Sleep(1 Second), time span is 1 Second    Sleep(2 Second), time span is 2 Second    Sleep(3 Second), time span is 3 Second    Sleep(4 Second), time span is 4 Second    Sleep(5 Second), time span is 5 Second    Sleep(6 Second), time span is 6 Second    Sleep(7 Second), time span is 7 Second    Sleep(8 Second), time span is 8 Second    Sleep(9 Second), time span is 9 Second    */    getwchar();return 0;}LONGLONG SecondTimeSpan(SYSTEMTIME &tmNow, SYSTEMTIME & tmPrev){    union FileTimeUnion    {        FILETIME fileTime;        ULARGE_INTEGER ul;    } ;    LONGLONG        llSecond = 0;    FileTimeUnion   ftNow;    FileTimeUnion   ftPrev;    SystemTimeToFileTime(&tmNow, &ftNow.fileTime);    SystemTimeToFileTime(&tmPrev, &ftPrev.fileTime);     llSecond = (ftNow.ul.QuadPart - ftPrev.ul.QuadPart);     llSecond /= 10000; ///< ms counter     llSecond /= 1000; ///< second counter     return llSecond;}

0 0
原创粉丝点击