QFileInfo::birthTime not updated each time?
-
Hello,
I figured out that after creating multiple times a file, sometimes the birth time is older than the real one.
I have the following code, in which I create the same file three times:#include <QCoreApplication> #include <QDateTime> #include <QDebug> #include <QFile> #include <QFileInfo> #include <QThread> void createFile(const QString& fileName) { QFile file { fileName }; file.open(QIODevice::WriteOnly); } int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); const QString fileName { "test.txt" }; qDebug() << "[" << QDateTime::currentDateTime() << "] 1/-" << QFile::exists(fileName); QThread::msleep(500); createFile(fileName); qDebug() << "\tCreated:" << QFileInfo(fileName).birthTime(); qDebug() << "\tRemoved:" << QFile::remove(fileName); qDebug() << "[" << QDateTime::currentDateTime() << "] 2/-" << QFile::exists(fileName); QThread::msleep(500); createFile(fileName); qDebug() << "\tCreated:" << QFileInfo(fileName).birthTime(); qDebug() << "\tRemoved:" << QFile::remove(fileName); qDebug() << "[" << QDateTime::currentDateTime() << "] 3/-" << QFile::exists(fileName); QThread::msleep(500); createFile(fileName); qDebug() << "\tCreated:" << QFileInfo(fileName).birthTime(); qDebug() << "\tRemoved:" << QFile::remove(fileName); return app.exec(); }
And here is the result:
[ QDateTime(2021-07-27 09:32:57.593 Paris, Madrid (heure d’été) Qt::LocalTime) ] 1/- false Created: QDateTime(2021-07-27 09:32:58.100 Paris, Madrid (heure d’été) Qt::LocalTime) Removed: true [ QDateTime(2021-07-27 09:32:58.100 Paris, Madrid (heure d’été) Qt::LocalTime) ] 2/- false Created: QDateTime(2021-07-27 09:32:58.100 Paris, Madrid (heure d’été) Qt::LocalTime) Removed: true [ QDateTime(2021-07-27 09:32:58.613 Paris, Madrid (heure d’été) Qt::LocalTime) ] 3/- false Created: QDateTime(2021-07-27 09:32:58.100 Paris, Madrid (heure d’été) Qt::LocalTime) Removed: true
For the last file, the birth time is exactly the same as the previous one, and less than the currentTime before creation. :o
Before:09:32:58.613
Birth time:09:32:58.100
Do you have any idea?
Thanks a lot!! -
@Maluna34 said in QFileInfo::birthTime not updated each time?:
Do you have any idea?
Did you notice that file always have same creation timestamp?
I am quiet sure, this is because you are always recreating the same file, and the file system layer of your OS is optimizing it, to avoid to always create a new file descriptor.Nothing to do with Qt, it is system relevant.