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. QDateTime::currentMSecsSinceEpoch() resolution is 10 ms, not 1ms
Forum Update on Monday, May 27th 2025

QDateTime::currentMSecsSinceEpoch() resolution is 10 ms, not 1ms

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.2k 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
    MECoder
    wrote on last edited by
    #1

    Hi,
    i have a problem with QDateTime::currentMSecsSinceEpoch(). A simple console application
    shows the problem.

    #include <QCoreApplication>
    #include <QDebug>
    #include <QDateTime>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        for( int i=0; i<1000; ++i )
        {
            qDebug() << Q_FUNC_INFO << QDateTime::currentMSecsSinceEpoch();
        }
        return a.exec();
    }
    

    The timer jumps every 10 ms and it was definitely not the case some weeks ago.
    I'm using QDateTime::currentMSecsSinceEpoch() as timestamp for incoming UDP
    measurement packages over UDP. Some weeks ago the measurement timestamps
    were ok, but now they are jumping for 10 ms.

    Has someone an idea or similar problems?

    Regards
    Stefan

    0_1522217201150_QDateTimeMilliseconds.png

    J.HilkJ aha_1980A 2 Replies Last reply
    0
    • M MECoder

      Hi,
      i have a problem with QDateTime::currentMSecsSinceEpoch(). A simple console application
      shows the problem.

      #include <QCoreApplication>
      #include <QDebug>
      #include <QDateTime>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          for( int i=0; i<1000; ++i )
          {
              qDebug() << Q_FUNC_INFO << QDateTime::currentMSecsSinceEpoch();
          }
          return a.exec();
      }
      

      The timer jumps every 10 ms and it was definitely not the case some weeks ago.
      I'm using QDateTime::currentMSecsSinceEpoch() as timestamp for incoming UDP
      measurement packages over UDP. Some weeks ago the measurement timestamps
      were ok, but now they are jumping for 10 ms.

      Has someone an idea or similar problems?

      Regards
      Stefan

      0_1522217201150_QDateTimeMilliseconds.png

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @MECoder said in QDateTime::currentMSecsSinceEpoch() resolution is 10 ms, not 1ms:

      for( int i=0; i<1000; ++i )
      {
      qDebug() << Q_FUNC_INFO << QDateTime::currentMSecsSinceEpoch();
      }

      can't reproduce that.

      this is my result, it increases each milisecond:
      0_1522217935483_3d4259e5-8b3d-4253-ad8b-0c30d17fd5da-image.png


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • M MECoder

        Hi,
        i have a problem with QDateTime::currentMSecsSinceEpoch(). A simple console application
        shows the problem.

        #include <QCoreApplication>
        #include <QDebug>
        #include <QDateTime>
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
            for( int i=0; i<1000; ++i )
            {
                qDebug() << Q_FUNC_INFO << QDateTime::currentMSecsSinceEpoch();
            }
            return a.exec();
        }
        

        The timer jumps every 10 ms and it was definitely not the case some weeks ago.
        I'm using QDateTime::currentMSecsSinceEpoch() as timestamp for incoming UDP
        measurement packages over UDP. Some weeks ago the measurement timestamps
        were ok, but now they are jumping for 10 ms.

        Has someone an idea or similar problems?

        Regards
        Stefan

        0_1522217201150_QDateTimeMilliseconds.png

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @MECoder

        • Which Qt version are you using?
        • Have you changed this Qt version since your last tests?

        Qt has to stay free or it will die.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MECoder
          wrote on last edited by
          #4

          Hi,
          my post as pure text is always flagged as spam by akismet. So i put everything from preview into a picture below.

          0_1522221664093_Answer.png

          aha_1980A 1 Reply Last reply
          0
          • M MECoder

            Hi,
            my post as pure text is always flagged as spam by akismet. So i put everything from preview into a picture below.

            0_1522221664093_Answer.png

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by aha_1980
            #5

            @MECoder

            What happens if you change QTime to QElapsedTimer?

            From my older experiences with Windows, I can confirm that the API had some minimal time between events. There was a call in mmsystem.dll to speed up this, but I don't remember the API call at the moment.

            Edit: the Windows API call for higher resolution was timeBeginPeriod(). I used it to get shorter sleep times than 10 milliseconds. Be aware of the side effects that may have.

            If possible, use QElaspsedTimer for time interval measurement.

            Qt has to stay free or it will die.

            M 1 Reply Last reply
            2
            • aha_1980A aha_1980

              @MECoder

              What happens if you change QTime to QElapsedTimer?

              From my older experiences with Windows, I can confirm that the API had some minimal time between events. There was a call in mmsystem.dll to speed up this, but I don't remember the API call at the moment.

              Edit: the Windows API call for higher resolution was timeBeginPeriod(). I used it to get shorter sleep times than 10 milliseconds. Be aware of the side effects that may have.

              If possible, use QElaspsedTimer for time interval measurement.

              M Offline
              M Offline
              MECoder
              wrote on last edited by
              #6

              @aha_1980 said in QDateTime::currentMSecsSinceEpoch() resolution is 10 ms, not 1ms:

              What happens if you change QTime to QElapsedTimer?

              Interesting!

              QElapsedTimer elatime;
              qDebug() << Q_FUNC_INFO << QDateTime::currentMSecsSinceEpoch() << time.elapsed() << elatime.elapsed();
              

              i get this result. QElapsedTimer increases in milliseconds while QDateTime and QTime in steps of 10ms.

              int main(int, char**) 1522222320473 40 31
              int main(int, char**) 1522222320473 40 32
              int main(int, char**) 1522222320473 40 33
              int main(int, char**) 1522222320473 40 34
              int main(int, char**) 1522222320473 40 34
              int main(int, char**) 1522222320473 40 35
              int main(int, char**) 1522222320473 40 36
              int main(int, char**) 1522222320473 40 37
              int main(int, char**) 1522222320473 40 38
              int main(int, char**) 1522222320473 40 39
              int main(int, char**) 1522222320483 50 41
              int main(int, char**) 1522222320483 50 43
              int main(int, char**) 1522222320483 50 46
              int main(int, char**) 1522222320483 50 48
              int main(int, char**) 1522222320483 50 49
              int main(int, char**) 1522222320493 60 51
              int main(int, char**) 1522222320493 60 53
              int main(int, char**) 1522222320493 60 54
              int main(int, char**) 1522222320493 60 55
              int main(int, char**) 1522222320493 60 57
              int main(int, char**) 1522222320493 60 59
              
              aha_1980A 1 Reply Last reply
              0
              • M MECoder

                @aha_1980 said in QDateTime::currentMSecsSinceEpoch() resolution is 10 ms, not 1ms:

                What happens if you change QTime to QElapsedTimer?

                Interesting!

                QElapsedTimer elatime;
                qDebug() << Q_FUNC_INFO << QDateTime::currentMSecsSinceEpoch() << time.elapsed() << elatime.elapsed();
                

                i get this result. QElapsedTimer increases in milliseconds while QDateTime and QTime in steps of 10ms.

                int main(int, char**) 1522222320473 40 31
                int main(int, char**) 1522222320473 40 32
                int main(int, char**) 1522222320473 40 33
                int main(int, char**) 1522222320473 40 34
                int main(int, char**) 1522222320473 40 34
                int main(int, char**) 1522222320473 40 35
                int main(int, char**) 1522222320473 40 36
                int main(int, char**) 1522222320473 40 37
                int main(int, char**) 1522222320473 40 38
                int main(int, char**) 1522222320473 40 39
                int main(int, char**) 1522222320483 50 41
                int main(int, char**) 1522222320483 50 43
                int main(int, char**) 1522222320483 50 46
                int main(int, char**) 1522222320483 50 48
                int main(int, char**) 1522222320483 50 49
                int main(int, char**) 1522222320493 60 51
                int main(int, char**) 1522222320493 60 53
                int main(int, char**) 1522222320493 60 54
                int main(int, char**) 1522222320493 60 55
                int main(int, char**) 1522222320493 60 57
                int main(int, char**) 1522222320493 60 59
                
                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @MECoder said in QDateTime::currentMSecsSinceEpoch() resolution is 10 ms, not 1ms:

                QElapsedTimer increases in milliseconds while QDateTime and QTime in steps of 10ms.

                That is because QElapsedTimer don't use the API QTime uses.

                Please see my edit in my last post also.

                Qt has to stay free or it will die.

                M 1 Reply Last reply
                3
                • aha_1980A aha_1980

                  @MECoder said in QDateTime::currentMSecsSinceEpoch() resolution is 10 ms, not 1ms:

                  QElapsedTimer increases in milliseconds while QDateTime and QTime in steps of 10ms.

                  That is because QElapsedTimer don't use the API QTime uses.

                  Please see my edit in my last post also.

                  M Offline
                  M Offline
                  MECoder
                  wrote on last edited by
                  #8

                  @aha_1980 said in QDateTime::currentMSecsSinceEpoch() resolution is 10 ms, not 1ms:

                  That is because QElapsedTimer don't use the API QTime uses.
                  Please see my edit in my last post also.

                  Thx! I have changed my software to a fixed starting timestamp from QDateTime::currentMSecsSinceEpoch()
                  and add the value from QElapsedTimer::elapsed() each event when i receive the UDP package with measurement data.
                  This solved my problem. Great!

                  1 Reply Last reply
                  2

                  • Login

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