Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFileInfo::birthTime not updated each time?
Forum Updated to NodeBB v4.3 + New Features

QFileInfo::birthTime not updated each time?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 484 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maluna34
    wrote on last edited by
    #1

    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!!

    KroMignonK 1 Reply Last reply
    0
    • M Maluna34

      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!!

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @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.

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved