我打算获取进程运行的时间,但不知道如何计算时间。我知道可以用现在的时间减去进程创建时间,但如果去减?
假如创建时间为:2:30:40.现在时间为3:40:50那运行时间肯定是1:10:10,但是如果现在时间为1:20:30.减了就成负数,取模也不对,也就是说,如果创建秒数为59秒,现在秒数为3秒,我们知道肯定是运行了4秒钟,但直接用3-59显然不对。茫然。。
HANDLE hqq=OpenProcess(PROCESS_ALL_ACCESS,true,lppe.th32ProcessID);
FILETIME CreationTime;
FILETIME ExitTime;
FILETIME KernelTime;
FILETIME UserTime;
GetProcessTimes(hqq,&CreationTime,&ExitTime,&KernelTime,&UserTime); //获取时间
SYSTEMTIME ProcCreatTime,LocalTime;
FileTimeToSystemTime(&CreationTime,&ProcCreatTime);//将FILETIME转换成SYSTEMTIME
GetSystemTime(&LocalTime); //获取系统时间
char TimeBuf[512]={0};
wsprintf(TimeBuf,"%d-%d-%d %02d:%02d:%02d",ProcCreatTime.wYear,ProcCreatTime.wMonth,ProcCreatTime.wDay,ProcCreatTime.wHour+8,ProcCreatTime.wMinute,ProcCreatTime.wSecond);

解决方案 »

  1.   

    时间的加减不是这么来的,我对这个FILETIME 不太熟,但肯定有相关的库函数(MFC的CTime重载了+和-),你去goole搜索一下肯定能找到
      

  2.   

    此方法,我在自己的程序中用过的,绝对可行.楼主不妨一试.SYSTEMTIME LocalTime= {0};
    SYSTEMTIME ProcCreatTime = {0};
    SYSTEMTIME vlueSystemTime = {0};
    FILETIME CreationTime; 
    DOUBLE curTime = 0;
    DOUBLE proTime = 0;
    DOUBLE vlueTime = 0;
    GetProcessTimes(hqq,&CreationTime,&ExitTime,&KernelTime,&UserTime); //获取时间     
    FileTimeToSystemTime(&CreationTime,&ProcCreatTime); //转化为系统时间
    SystemTimeToVariantTime(&systemtime, &proTime); //转化为DOUBLE类的时间,可以直接运算的.GetLocalTime(&LocalTime); // 获取当前时间
    SystemTimeToVariantTime(&systemtime, &curTime );//转化为系统时间
    vlueTime = curTime - proTime; //计算差值vlueTime就是你想得到的时间差值,不过类型是DOUBLE的,要自己转换一下. 转换部分参考MSDN:
    A variant time is stored as an 8-byte real value (double), representing a date between January 1, 1753 and December 31, 2078, inclusive. The value 2.0 represents January 1, 1900; 3.0 represents January 2, 1900, and so on. Adding 1 to the value increments the date by a day. The fractional part of the value represents the time of day. Therefore, 2.5 represents noon on January 1, 1900; 3.25 represents 6:00 A.M. on January 2, 1900, and so on. Negative numbers represent the dates prior to December 30, 1899.
      

  3.   

       呵呵  还要获取当前时区设置  对结果进行运行 
    查查这个API怎么用 就知道了  GetTimeZoneInformation
      

  4.   

    你可以尝试这段代码#include <ctime>std::clock_t start = std::clock();
    /* Stuff to time here */
    std::cout<< ( ( std::clock() - start ) / (double)CLOCKS_PER_SEC ) <<'\n';