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)
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
c++qtlinux desktoplinux
5 Posts 4 Posters 777 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 4 Dec 2023, 10:49 last edited by Pawel_Kleczek 12 Apr 2023, 11:07
    #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?

    J 1 Reply Last reply 4 Dec 2023, 11:06
    0
    • P Pawel_Kleczek
      4 Dec 2023, 10:49

      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?

      J Offline
      J Offline
      JonB
      wrote on 4 Dec 2023, 11:06 last edited by JonB 12 Apr 2023, 11:20
      #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 5 Dec 2023, 06:35
      3
      • J JonB
        4 Dec 2023, 11:06

        @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 5 Dec 2023, 06:35 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.

        K 1 Reply Last reply 5 Dec 2023, 11:15
        2
        • P Pawel_Kleczek has marked this topic as solved on 5 Dec 2023, 07:57
        • C ChrisW67
          5 Dec 2023, 06:35

          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.

          K Offline
          K Offline
          kkoehne
          Moderators
          wrote on 5 Dec 2023, 11:15 last edited by kkoehne 12 May 2023, 11:16
          #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
          • J Offline
            J Offline
            JonB
            wrote on 5 Dec 2023, 12:59 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

            1/5

            4 Dec 2023, 10:49

            • Login

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