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() returns nothing (on Ubuntu)
Forum Updated to NodeBB v4.3 + New Features

QFileInfo::birthTime() returns nothing (on Ubuntu)

Scheduled Pinned Locked Moved Solved General and Desktop
c++qtlinux desktoplinux
5 Posts 4 Posters 999 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.
  • P Offline
    P Offline
    Pawel_Kleczek
    wrote on last edited by Pawel_Kleczek
    #1

    I'm using Qt 5.15.2 on Ubuntu 21.10
    When I call

    QFileInfo fileInfo(PATH_TO_A_FILE);
    LOG() << fileInfo.birthTime().toString("dd/MM/yy HH:mm:ss");
    LOG() << fileInfo.created().toString("dd/MM/yy HH:mm:ss");
    

    then birthTime()'s result is an empty string, whereas created() returns the correct time. However, according to the docs created() is deprecated and birthTime() should be used instead...

    Any ideas why do I observe such behavior?

    JonBJ 1 Reply Last reply
    0
    • P Pawel_Kleczek

      I'm using Qt 5.15.2 on Ubuntu 21.10
      When I call

      QFileInfo fileInfo(PATH_TO_A_FILE);
      LOG() << fileInfo.birthTime().toString("dd/MM/yy HH:mm:ss");
      LOG() << fileInfo.created().toString("dd/MM/yy HH:mm:ss");
      

      then birthTime()'s result is an empty string, whereas created() returns the correct time. However, according to the docs created() is deprecated and birthTime() should be used instead...

      Any ideas why do I observe such behavior?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Pawel_Kleczek
      For code browser for Qt5, https://codebrowser.dev/qt5/qtbase/src/corelib/io/qfileinfo.cpp.html

      QDateTime QFileInfo::created() const
      {
          QDateTime d = fileTime(QFile::FileBirthTime);
          if (d.isValid())
              return d;
          return fileTime(QFile::FileMetadataChangeTime);
      }
      
      QDateTime QFileInfo::birthTime() const
      {
          return fileTime(QFile::FileBirthTime);
      }
      

      So at some level fileTime(QFile::FileBirthTime) is returning invalid QDateTime while for created() the fileTime(QFile::FileMetadataChangeTime) is returning the result you get.

      You would have to follow https://codebrowser.dev/qt5/qtbase/src/corelib/io/qfileinfo.cpp.html#_ZNK9QFileInfo8fileTimeEN11QFileDevice8FileTimeE, QDateTime QFileInfo::fileTime(QFile::FileTime time) const, to find out why.

      I believe Qt caches QFileInfo stuff, that could be an issue somehow?

      Additionally per https://forum.qt.io/topic/133954/either-qfileinfo-caching-does-not-work-or-just-its-methods-are-unreasonable-very-slow/19 I believe at Qt 5.15 birthTime() (created() too?) may be "inefficient", though that is a separate matter, unless it interferes with the result here.

      It may be that Qt/OS does not regard Linux fs as actually storing a "creation" time, hence the call to QFile::FileMetadataChangeTime. Google for e.g. linux file created, read what is said, how it depends on the filing system in use, etc. The c flag on a Linux file is not named "created" time, it is named "Change - the last time meta data of the file was changed (e.g. permissions)", e.g. https://unix.stackexchange.com/a/2465/104736. So you're not going to get a Linux "created/born" time unless you use a native Linux call and appropriate to the file system.

      You may also use QDateTime QFileInfo::metadataChangeTime() const directly.

      C 1 Reply Last reply
      3
      • JonBJ JonB

        @Pawel_Kleczek
        For code browser for Qt5, https://codebrowser.dev/qt5/qtbase/src/corelib/io/qfileinfo.cpp.html

        QDateTime QFileInfo::created() const
        {
            QDateTime d = fileTime(QFile::FileBirthTime);
            if (d.isValid())
                return d;
            return fileTime(QFile::FileMetadataChangeTime);
        }
        
        QDateTime QFileInfo::birthTime() const
        {
            return fileTime(QFile::FileBirthTime);
        }
        

        So at some level fileTime(QFile::FileBirthTime) is returning invalid QDateTime while for created() the fileTime(QFile::FileMetadataChangeTime) is returning the result you get.

        You would have to follow https://codebrowser.dev/qt5/qtbase/src/corelib/io/qfileinfo.cpp.html#_ZNK9QFileInfo8fileTimeEN11QFileDevice8FileTimeE, QDateTime QFileInfo::fileTime(QFile::FileTime time) const, to find out why.

        I believe Qt caches QFileInfo stuff, that could be an issue somehow?

        Additionally per https://forum.qt.io/topic/133954/either-qfileinfo-caching-does-not-work-or-just-its-methods-are-unreasonable-very-slow/19 I believe at Qt 5.15 birthTime() (created() too?) may be "inefficient", though that is a separate matter, unless it interferes with the result here.

        It may be that Qt/OS does not regard Linux fs as actually storing a "creation" time, hence the call to QFile::FileMetadataChangeTime. Google for e.g. linux file created, read what is said, how it depends on the filing system in use, etc. The c flag on a Linux file is not named "created" time, it is named "Change - the last time meta data of the file was changed (e.g. permissions)", e.g. https://unix.stackexchange.com/a/2465/104736. So you're not going to get a Linux "created/born" time unless you use a native Linux call and appropriate to the file system.

        You may also use QDateTime QFileInfo::metadataChangeTime() const directly.

        C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        Storage of actual file creation time will also depend on the file system involved. For example, EXT4 has provision for a create time while EXT2/3 do not.

        Even then, the standard stat() call has no place to return it in struct stat, so you need the statx() call. Not sure what Qt ultimately uses.

        kkoehneK 1 Reply Last reply
        2
        • P Pawel_Kleczek has marked this topic as solved on
        • C ChrisW67

          Storage of actual file creation time will also depend on the file system involved. For example, EXT4 has provision for a create time while EXT2/3 do not.

          Even then, the standard stat() call has no place to return it in struct stat, so you need the statx() call. Not sure what Qt ultimately uses.

          kkoehneK Offline
          kkoehneK Offline
          kkoehne
          Moderators
          wrote on last edited by kkoehne
          #4

          @ChrisW67 said in QFileInfo::birthTime() returns nothing (on Ubuntu):

          Not sure what Qt ultimately uses.

          Qt uses statx (see e.g. https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/io/qfilesystemengine_unix.cpp).

          But yeah, not all file systems support birth time. @Pawel_Kleczek , you should be able to check this yourself on the command line with

          stat PATH_TO_A_FILE
          

          does it print a 'Birth: ' row?

          Director R&D, The Qt Company

          1 Reply Last reply
          0
          • JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            OP's Ubuntu should support stat file for Birth. See e.g. How to Find Out When a File Was Created in Linux for a recent discussion of the issues.

            1 Reply Last reply
            0

            • Login

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