QT 5.15.2 QDate::DaysTo 返回 0,尽管所有变量类型都正确

问题描述 投票:0回答:1

我正在寻找文件夹中的文件并检查它们的年龄。

我的这段代码没有按照我所遵循的文档和示例所期望的那样工作:

const QDate today = QDate::currentDate();
Q_FOREACH(auto fileInfo, QDir("folderName").entryInfoList(QDir::Files)) {
    
    // These variables are added to the watch table to see their values
    auto test1 = fileInfo.created().date().daysTo(today);
    auto test2 = fileInfo.baseName();
    auto test3 = fileInfo.suffix();
}

我将 test1、test2、test3 变量添加到监视表中以查看它们具有哪些值。

在我看来,

daysTo(today)
返回
0
,因为
today
是无效格式。这是儒略日。但它是
QDate
类型,并且
QDate::daysTo(QDate d)
想要一个
QDate
类型变量作为参数。这对我来说似乎是正确的?

通过检查基本名称和后缀,也可以找到正确的文件。而且它们不是 0 天...而是一个月左右。

为什么

daysTo
会返回
0
,尽管一切看起来都很好?

我使用的是QT 5.15.2。

更新:我添加了更多测试并将created()函数更改为birthTime()。请参阅我在观察观察表中的变量时发现的结果的评论:

    const QDate today = QDate::currentDate();
    Q_FOREACH(auto fileInfo, QDir("folderName").entryInfoList(QDir::Files)) {

        auto test1 = fileInfo.birthTime().date().daysTo(today);         // Gives "0"
        auto test11 = fileInfo.birthTime();                             // Gives "msecs=280348"
        bool test111 = test31.isValid();                                // Gives "true"
        auto test12 = fileInfo.birthTime().date();                      // Gives "Julian day=2460417"
        auto test13 = fileInfo.fileTime(QFileDevice::FileBirthTime);    // Gives "msecs=280311"
        auto test2 = fileInfo.baseName();                               // Gives "filename"
        auto test3 = fileInfo.suffix();                                 // Gives ".jpg"
c++ qt
1个回答
0
投票

我现在找到了解决方案。代码没有任何问题(除了使用已弃用的 create() 函数),并且 QDate 变量中的 msecs 字段是一个红鲱鱼......

问题在于,当文件复制粘贴到新位置时,它们会重置其出生时间。所以它们算作当天创建的:今天。

因此,即使操作系统将文件识别为一个月前的文件,fileInfo.birthTime() 或 fileInfo.fileTime(QFileDevice::FileBirthTime) 也不会反映这一点。我也尝试了 QFileDevice::FileMetadataChangeTime 但也不起作用。

当我使用 secsTo() 和 QDateTime 而不是 daysTo() 和 QDate 检查过去的秒数而不是天数时,我注意到了这一点。如果我让复制的文件在文件夹中保持原样一天,我会发现它们已经存在 1 天了。

© www.soinside.com 2019 - 2024. All rights reserved.