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. Set LastModified date of file
Forum Updated to NodeBB v4.3 + New Features

Set LastModified date of file

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 8 Posters 11.2k Views 5 Watching
  • 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi
    QFileInfo seems only to allow reading it
    http://doc.qt.io/qt-5/qfileinfo.html#lastModified

    so utime could be used on win/linux
    https://msdn.microsoft.com/en-us/library/4wacf567(v=vs.140).aspx
    https://linux.die.net/man/2/utime

    or if only win
    https://msdn.microsoft.com/en-us/library/ms724933(VS.85).aspx

    S 1 Reply Last reply
    2
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #3

      Hi!
      I think the following behaves pretty much like touch (changes the last modified date, creates the file if it didn't exist before):

      bool touch(const QString& filePath)
      {
          QFile file(filePath);
          if (!file.open(QIODevice::ReadWrite)) {
              return false;
          }
          const quint64 size = file.size();
          file.seek(size);
          file.write( QByteArray(1, '0') );
          file.resize(size);
          return true;
      }
      
      G 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi
        QFileInfo seems only to allow reading it
        http://doc.qt.io/qt-5/qfileinfo.html#lastModified

        so utime could be used on win/linux
        https://msdn.microsoft.com/en-us/library/4wacf567(v=vs.140).aspx
        https://linux.die.net/man/2/utime

        or if only win
        https://msdn.microsoft.com/en-us/library/ms724933(VS.85).aspx

        S Offline
        S Offline
        samdol
        wrote on last edited by
        #4

        @mrjj
        I tried as following,

        #include <stdio.h>
        #include <stdlib.h>
        #include <sys/types.h>
        #include <sys/utime.h>
        #include <time.h>

        struct tm tma = {0}, tmm = {0};
        struct _utimbuf ut;
        tmm.tm_year = 102;
        tmm.tm_mon = 1;
        tmm.tm_mday = 15;
        tmm.tm_hour = 10;
        tmm.tm_min = 0;
        tmm.tm_sec = 0;
        ut.modtime = mktime(&tmm);
        
        
        // Show file time before and after
        if( _utime( "D:/kde/crt_utime.c", &ut ) == -1 )
           perror( "_utime failed\n" );
        else
           printf( "File time modified\n" );
        system( "dir D:\kde" );
        

        The result is
        Volume in drive D has no label.
        Volume Serial Number is EA68-9A51

        Directory of D:\kde

        10/08/2016 02:51 PM <DIR> .
        10/08/2016 02:51 PM <DIR> ..
        02/15/2002 10:00 AM 0 crt_utime.c
        1 File(s) 0 bytes
        2 Dir(s) 151,142,400 bytes free

        I can see the correctly chanaged time 10:00 AM. But when I check on my windows explorer, I can see the Date Modified is 9:00 AM. The time checked by system and explorer shows always 1 hour different result. I changed tmm.tm_isdst to positive, negative and zero, but still system and explorer show different time. Is my system infected by virus?

        benlauB 1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #5

          Hi
          I dont think you have a virus :)

          You are right its related to the isdst / daylight savings.

          http://stackoverflow.com/questions/12122084/confusing-behaviour-of-mktime-function-increasing-tm-hour-count-by-one

          http://stackoverflow.com/questions/3660983/c-time-t-problem

          But it seems you tried all values for it and it still have the 1 hour diff so you should try some of the suggestion from the links.
          Some seems to do
          setenv("TZ", "", 1);
          tzset();
          before mktime to correct it.

          S 1 Reply Last reply
          1
          • S samdol

            @mrjj
            I tried as following,

            #include <stdio.h>
            #include <stdlib.h>
            #include <sys/types.h>
            #include <sys/utime.h>
            #include <time.h>

            struct tm tma = {0}, tmm = {0};
            struct _utimbuf ut;
            tmm.tm_year = 102;
            tmm.tm_mon = 1;
            tmm.tm_mday = 15;
            tmm.tm_hour = 10;
            tmm.tm_min = 0;
            tmm.tm_sec = 0;
            ut.modtime = mktime(&tmm);
            
            
            // Show file time before and after
            if( _utime( "D:/kde/crt_utime.c", &ut ) == -1 )
               perror( "_utime failed\n" );
            else
               printf( "File time modified\n" );
            system( "dir D:\kde" );
            

            The result is
            Volume in drive D has no label.
            Volume Serial Number is EA68-9A51

            Directory of D:\kde

            10/08/2016 02:51 PM <DIR> .
            10/08/2016 02:51 PM <DIR> ..
            02/15/2002 10:00 AM 0 crt_utime.c
            1 File(s) 0 bytes
            2 Dir(s) 151,142,400 bytes free

            I can see the correctly chanaged time 10:00 AM. But when I check on my windows explorer, I can see the Date Modified is 9:00 AM. The time checked by system and explorer shows always 1 hour different result. I changed tmm.tm_isdst to positive, negative and zero, but still system and explorer show different time. Is my system infected by virus?

            benlauB Offline
            benlauB Offline
            benlau
            Qt Champions 2016
            wrote on last edited by benlau
            #6

            @samdol In fact, you may just pass a null pointer to the second argument of utime. Then it will be set to current time.

            That is how I implement touch():

            https://github.com/benlau/qtshell/blob/master/qtshell.cpp#L178

            ? J 2 Replies Last reply
            2
            • benlauB benlau

              @samdol In fact, you may just pass a null pointer to the second argument of utime. Then it will be set to current time.

              That is how I implement touch():

              https://github.com/benlau/qtshell/blob/master/qtshell.cpp#L178

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #7

              @benlau QtShell looks nice, thanks for sharing!

              1 Reply Last reply
              1
              • benlauB benlau

                @samdol In fact, you may just pass a null pointer to the second argument of utime. Then it will be set to current time.

                That is how I implement touch():

                https://github.com/benlau/qtshell/blob/master/qtshell.cpp#L178

                J Offline
                J Offline
                Jeronimo
                wrote on last edited by
                #8

                @benlau why you do this change? path.toLocal8Bit().constData()

                benlauB 1 Reply Last reply
                0
                • J Jeronimo

                  @benlau why you do this change? path.toLocal8Bit().constData()

                  benlauB Offline
                  benlauB Offline
                  benlau
                  Qt Champions 2016
                  wrote on last edited by
                  #9

                  @Jeronimo Convert QString to const char*.

                  p.s Just updated the source to use toUtf8() instead of toLocal8Bit().

                  S 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    Hi
                    I dont think you have a virus :)

                    You are right its related to the isdst / daylight savings.

                    http://stackoverflow.com/questions/12122084/confusing-behaviour-of-mktime-function-increasing-tm-hour-count-by-one

                    http://stackoverflow.com/questions/3660983/c-time-t-problem

                    But it seems you tried all values for it and it still have the 1 hour diff so you should try some of the suggestion from the links.
                    Some seems to do
                    setenv("TZ", "", 1);
                    tzset();
                    before mktime to correct it.

                    S Offline
                    S Offline
                    samdol
                    wrote on last edited by
                    #10

                    @mrjj

                    In fact my computer adjust clock for Daylight saving time.
                    So I did,
                    Control Panel --> Time Zone Settings
                    --> Uncheck Automatically adjust clock for Daylight Saving Time.

                    Now the lastmodified time on system and explorer are the same.
                    Thank you.

                    1 Reply Last reply
                    1
                    • benlauB benlau

                      @Jeronimo Convert QString to const char*.

                      p.s Just updated the source to use toUtf8() instead of toLocal8Bit().

                      S Offline
                      S Offline
                      samdol
                      wrote on last edited by
                      #11

                      @benlau
                      Hi Benla,
                      I tested the touch function in qtshell.cpp.

                      bool QtShell::touch(const QString &path)
                      {
                      ...
                      QByteArray bytes = path.toUtf8();

                          if (utime(bytes.constData(), 0) == -1) {
                              qWarning() << "utimes failed:" << path;
                      

                      res = false;
                      ...
                      }

                      It works fine for english filename. But if the filename contains some chinese
                      charactors then it gives an error and I could not modified time stamp of file.
                      I am using Windows 7. Thank you.

                      benlauB S 2 Replies Last reply
                      0
                      • S samdol

                        @benlau
                        Hi Benla,
                        I tested the touch function in qtshell.cpp.

                        bool QtShell::touch(const QString &path)
                        {
                        ...
                        QByteArray bytes = path.toUtf8();

                            if (utime(bytes.constData(), 0) == -1) {
                                qWarning() << "utimes failed:" << path;
                        

                        res = false;
                        ...
                        }

                        It works fine for english filename. But if the filename contains some chinese
                        charactors then it gives an error and I could not modified time stamp of file.
                        I am using Windows 7. Thank you.

                        benlauB Offline
                        benlauB Offline
                        benlau
                        Qt Champions 2016
                        wrote on last edited by
                        #12

                        @samdol Hi, I just setup a Windows 10 to test. Both of VC and Mingw works. Could you send me the failed file name?

                        S 1 Reply Last reply
                        1
                        • benlauB benlau

                          @samdol Hi, I just setup a Windows 10 to test. Both of VC and Mingw works. Could you send me the failed file name?

                          S Offline
                          S Offline
                          samdol
                          wrote on last edited by
                          #13

                          @benlau
                          Here is a zip file containing two files. link text

                          The following is the code, I tested on my Windows7 English version.

                          bool res = true;
                          QFileInfo info(path);
                          
                          if (!info.exists()) {
                          
                              QFile file(path);
                              if (!file.open(QIODevice::WriteOnly)) {
                                  qWarning() << "Failed to create file:" << path;
                                  res = false;
                              }
                              file.close();
                          
                          } else {
                              QByteArray bytes = path.toUtf8();
                          
                              if (utime(bytes.constData(), 0) == -1) {
                                  qWarning() << "utimes failed:" << path;
                                  res = false;
                              }
                          }
                          
                          1 Reply Last reply
                          0
                          • ? A Former User

                            Hi!
                            I think the following behaves pretty much like touch (changes the last modified date, creates the file if it didn't exist before):

                            bool touch(const QString& filePath)
                            {
                                QFile file(filePath);
                                if (!file.open(QIODevice::ReadWrite)) {
                                    return false;
                                }
                                const quint64 size = file.size();
                                file.seek(size);
                                file.write( QByteArray(1, '0') );
                                file.resize(size);
                                return true;
                            }
                            
                            G Offline
                            G Offline
                            Guigas
                            wrote on last edited by
                            #14

                            Thank you for this, very simple, quick and it worked for me (tested only on windows so far)

                            1 Reply Last reply
                            0
                            • danttiD Offline
                              danttiD Offline
                              dantti
                              wrote on last edited by
                              #15

                              Since Qt5.10 there is QFileDevice::setFileTime() https://doc.qt.io/qt-6/qfiledevice.html#setFileTime

                              1 Reply Last reply
                              0
                              • S samdol

                                @benlau
                                Hi Benla,
                                I tested the touch function in qtshell.cpp.

                                bool QtShell::touch(const QString &path)
                                {
                                ...
                                QByteArray bytes = path.toUtf8();

                                    if (utime(bytes.constData(), 0) == -1) {
                                        qWarning() << "utimes failed:" << path;
                                

                                res = false;
                                ...
                                }

                                It works fine for english filename. But if the filename contains some chinese
                                charactors then it gives an error and I could not modified time stamp of file.
                                I am using Windows 7. Thank you.

                                S Online
                                S Online
                                SimonSchroeder
                                wrote on last edited by
                                #16

                                @samdol said in Set LastModified date of file:

                                It works fine for english filename. But if the filename contains some chinese
                                charactors then it gives an error

                                For future reference: By default char* on Windows uses the local 8 bit encoding (which means path.toLocal8Bit().constData() was correct and the local encoding on Linux is already UTF-8). In order to use toUtf8() on Windows you need to call setlocale(LC_ALL, ".UTF8") (preferrably once at the beginning of main). (If you also want to print UTF-8 characters to the console, you'll also need to set the UTF-8 codepage.)

                                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