I have an MFC application where I need to determine if a file's last write time is older than n hours.
I thought there was a method to get total seconds from either system or file time, but I ended up coding this method which works, but there might be a leaner method.
bool FileOlderThanHours(HANDLE hFile, int hours)
{
FILETIME ftCreate, ftLastAccess, ftLastWrite;
SYSTEMTIME stNow, stFile;
GetSystemTime(&stNow);
if (!GetFileTime(hFile, &ftCreate, &ftLastAccess, &ftLastWrite))
return false;
if (!FileTimeToSystemTime(&ftLastWrite, &stFile))
return false;
COleDateTime oleNow(stNow.wYear, stNow.wMonth, stNow.wDay, stNow.wHour,
stNow.wMinute, stNow.wSecond);
COleDateTime oleFile(stFile.wYear, stFile.wMonth, stFile.wDay, stFile.wHour,
stFile.wMinute, stFile.wSecond);
COleDateTimeSpan timeDiff = oleNow - oleFile;
double totHours = timeDiff.GetTotalHours();
return (totHours > hours);
}
If someone knows a leaner way, please let me know, thanks.
-
\$\begingroup\$ You seem to be missing the return type on the function. \$\endgroup\$JDługosz– JDługosz2021年12月30日 17:11:51 +00:00Commented Dec 30, 2021 at 17:11
-
\$\begingroup\$ my bad--I copied this method from my class and omitted the type along with the ... ClassName:: prefix. \$\endgroup\$netcat– netcat2021年12月30日 18:21:35 +00:00Commented Dec 30, 2021 at 18:21
-
1\$\begingroup\$ Please don't edit or append code in the question after it has been reviewed. For details, see: codereview.meta.stackexchange.com/questions/1763/… \$\endgroup\$user673679– user6736792022年01月04日 19:06:08 +00:00Commented Jan 4, 2022 at 19:06
-
\$\begingroup\$ In addition to what user673679 stated: incorporating feedback from answers goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see What should I do when someone answers my question? as well as what you may and may not do after receiving answers. \$\endgroup\$Sᴀᴍ Onᴇᴌᴀ– Sᴀᴍ Onᴇᴌᴀ ♦2022年01月04日 19:13:30 +00:00Commented Jan 4, 2022 at 19:13
1 Answer 1
In MFC, the CFile::GetStatus
method lets you query the creation, modification or access time of a file.
Code to use it would look something like:
CTime getModificationTIme(const LPCTSTR filename)
/* Returns the last modification time of the requested file. The
* filename must be a null-terminated wide string.
*/
{
CFileStatus inputFileStatus;
if (!CFile::GetStatus( filename, inputFileStatus )) {
// Handle the error, perhaps by throwing an exception. E.g.,
THROW((CException*) new CCustomException(filename));
}
return inputFileStatus.m_mtime;
}
In current versions of Windows, the filename is a null-terminated wide string. Future versions might un-deprecate the char*
version to accept a UTF-8 filename.
Visual Studio 2017 and up support the standard library’s <filesystem>
header, of which you can find an example of usage at cppreference.
One major difference between Windows and other systems is that, on Windows, the <filesystem>
API uses wide strings to create a std::filesystem::path
.
-
\$\begingroup\$ Thanks! I implemented the CTime class into my method. \$\endgroup\$netcat– netcat2021年12月30日 21:57:29 +00:00Commented Dec 30, 2021 at 21:57
-
\$\begingroup\$ @netcat Glad it helped! Anyone who copies this later should be sure to notice, my error-handling code was just a dummy. \$\endgroup\$Davislor– Davislor2021年12月30日 22:18:25 +00:00Commented Dec 30, 2021 at 22:18