gpt4 book ai didi

c++ - 通过 SYSTEMTIME 检查文件的 LastWriteTime 是昨天

转载 作者:行者123 更新时间:2023-11-30 05:46:24 26 4
gpt4 key购买 nike

我想检查很多文件的 LastWriteTimes 是否与系统昨天的日期相同。我的问题是如何以 SYSTEMTIME 格式设置昨天的日期时间。以下是我的代码。

    bool checkLastWriteTime(FILETIME ftLastWriteTime)
{
SYSTEMTIME stUTC, stLocal;
// Convert the last-write time to local time.
FileTimeToSystemTime(&ftLastWriteTime, &stUTC);
SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

// Build a string showing the date and time.
_tprintf(TEXT("%02d/%02d/%d %02d:%02d\n"),
stLocal.wMonth, stLocal.wDay, stLocal.wYear,
stLocal.wHour, stLocal.wMinute);

SYSTEMTIME localTime;
GetLocalTime(&localTime);

//
//How to get the date of yesterday from localTime?
//

if (stLocal.wYear == localTime.wYear && stLocal.wMonth == localTime.wMonth && stLocal.wDay == localTime.wDay)
{
return true;
}
else
{
return false;
}
}

最佳答案

“昨天”是一个范围。结束时间很容易计算,发生在今天早上 12 点:

SYSTEMTIME now;
GetLocalTime(&now);
SYSTEMTIME stYesterdayEnd = { now.wYear, now.wMonth, now.wDayOfWeek, now.wDay };
FILETIME ftYesterdayEnd;
SystemTimeToFileTime(&stYesterdayEnd, &ftYesterdayEnd);

它在那之前 24 小时开始。因此,您需要从 ftYesterdayEnd 中减去 100 纳秒单位。 FILETIME 有点棘手,我们将使用 LARGE_INTEGER 来简化它:

LARGE_INTEGER liYesterdayBeg = { ftYesterdayEnd.dwLowDateTime, ftYesterdayEnd.dwHighDateTime };
ULONGLONG oneday = 24ULL * 60 * 60 * 1000 * 1000 * 10;
liYesterdayBeg.QuadPart -= oneday;
FILETIME ftYesterdayBeg = { liYesterdayBeg.LowPart, liYesterdayBeg.HighPart };

现在您可以编写函数了:

bool wasWrittenYesterday(FILETIME ftLastWriteTime)
{
// As above
//...
return ftLastWriteTime >= ftYesterdayBeg && ftLastWriteTime < ftYesterdayEnd
}

关于c++ - 通过 SYSTEMTIME 检查文件的 LastWriteTime 是昨天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28941474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com