文件生成时间耗时检测

    xiaoxiao2022-07-02  129

    原理是根据修改时间减去创建时间,关联部分其他头文件,仅供参考。

    bool GenerateTimeCheck(const _tstring& strFilePath, SYSTEMTIME& sElapsedTime) { if (!CStdFile::IfAccessFile(strFilePath)) { return false; } FILETIME ftCreate, ftAccess, ftWrite; SYSTEMTIME stUTC1, stLocal1, stUTC2, stLocal2, stUTC3, stLocal3; // -------->获取 FileTime HANDLE hSrcFile = CreateFile(strFilePath.c_str(), 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hSrcFile == INVALID_HANDLE_VALUE || !GetFileTime(hSrcFile, &ftCreate, &ftAccess, &ftWrite)) { //error("GetFileTime()"); return false; } //---------> 转换: FileTime --> LocalTime FileTimeToSystemTime(&ftCreate, &stUTC1); //FileTimeToSystemTime(&ftAccess, &stUTC2); FileTimeToSystemTime(&ftWrite, &stUTC3); SystemTimeToTzSpecificLocalTime(NULL, &stUTC1, &stLocal1); //SystemTimeToTzSpecificLocalTime(NULL, &stUTC2, &stLocal2); SystemTimeToTzSpecificLocalTime(NULL, &stUTC3, &stLocal3); //sElapsedTime = stLocal2 - stLocal1; if (stLocal3.wYear != stLocal1.wYear || stLocal3.wMonth != stLocal1.wMonth) { //一般月和年应当相同,不同则认为有问题(比如被修改过) return false; } //stLocal2临时变量 stLocal2 = stLocal3; //手动处理,先全部借位 stLocal2.wDay -= 1; stLocal2.wHour += 24 - 1; stLocal2.wMinute += 60 -1; stLocal2.wSecond += 60 -1; stLocal2.wMilliseconds += 1000; int nDay = (int)stLocal2.wDay - stLocal1.wDay; int nHour = (int)stLocal2.wHour - stLocal1.wHour; int nMin = (int)stLocal2.wMinute - stLocal1.wMinute; int nSecond = (int)stLocal2.wSecond - stLocal1.wSecond; int nMiliseconds = (int)stLocal2.wMilliseconds - stLocal1.wMilliseconds; //还原 if (nMiliseconds >= 1000) { nMiliseconds -= 1000; nSecond += 1; } if (nSecond >= 60) { nSecond -= 60; nMin += 1; } if (nMin >= 60) { nMin -= 60; nHour += 1; } if (nHour >= 24) { nHour-= 24; nDay += 1; } sElapsedTime.wYear = 0; sElapsedTime.wMonth = 0; if (nDay < 0) { return false; } sElapsedTime.wDay = nDay; sElapsedTime.wHour = nHour; sElapsedTime.wMinute = nMin; sElapsedTime.wSecond = nSecond; sElapsedTime.wMilliseconds = nMiliseconds; return true; }

    更多的交流,欢迎留言。

    最新回复(0)