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. How to work with time intervals in Qt?
Forum Updated to NodeBB v4.3 + New Features

How to work with time intervals in Qt?

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 7 Posters 3.8k Views 3 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.
  • sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote on last edited by
    #2

    There is no such class. But parts of the functionality you can get with QDateTime::daysTo() and similar.

    For smaller time spans, you can also check QTimer, QElapsedTimer, QDeadlineTimer.

    (Z(:^

    R 1 Reply Last reply
    4
    • M Offline
      M Offline
      MrShawn
      wrote on last edited by
      #3

      A lot of times I will use QTimer singleshot and connect the timeout signal to a slot to do a task every x ms. If i need it to repeat I start my timer again at the end of the slot's execution.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Hi,

        IIRC, there was at some point discussions about a class named QTimeSpan. You have to search for it.

        Or maybe std::chrono may have what you want.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        R 1 Reply Last reply
        3
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by Eddy
          #5

          a long time ago @Andre was working on that.

          I found the thread here on the forum:
          https://forum.qt.io/topic/3160/qtimespan-interest

          hope it helps

          Qt Certified Specialist
          www.edalsolutions.be

          R 1 Reply Last reply
          0
          • R r3d9u11

            Re: Time Span Calculation

            Hello. Is there any class to work with time intervals in Qt ? Something like a TimeSpan in C#. I can't find any information about it, yet.

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

            @r3d9u11
            TimeSpan in C# has nothing to do with clocks/timers/timeouts/signals, it's just a data type for manipulating time periods, which happens to go down to milliseconds/ticks.

            If you are in C++, given @SGaist's link to std::chrono and its members like duration, it hardly seems worth worrying about/waiting for Qt to implement anything when you have that to use. If you're using Python, the language has library datetime.timedelta.

            R 1 Reply Last reply
            3
            • EddyE Eddy

              a long time ago @Andre was working on that.

              I found the thread here on the forum:
              https://forum.qt.io/topic/3160/qtimespan-interest

              hope it helps

              R Offline
              R Offline
              r3d9u11
              wrote on last edited by
              #7

              @eddy Thank you, I watched this topic, too (so, this topic is just a continuing of that discussion)

              1 Reply Last reply
              0
              • sierdzioS sierdzio

                There is no such class. But parts of the functionality you can get with QDateTime::daysTo() and similar.

                For smaller time spans, you can also check QTimer, QElapsedTimer, QDeadlineTimer.

                R Offline
                R Offline
                r3d9u11
                wrote on last edited by r3d9u11
                #8

                @sierdzio Thak you, however something wrong with QDateTime. It work incorrectly when date is zero.
                For example:

                #include <QCoreApplication>
                #include <QDateTime>
                #include <QDebug>
                
                void print_date(const QDateTime& qdt)
                {
                    qDebug() << qdt.date().toString("yyyy:MM:dd") << "-" << qdt.time().toString("hh:mm:ss");
                }
                
                int main ()
                {
                    QDateTime qdt(QDate(0, 0, 0), QTime(0, 0, 0));
                    print_date(qdt);
                
                    qdt = qdt.addSecs(5);
                    print_date(qdt);
                
                    qdt = qdt.addMSecs(3600003);
                    print_date(qdt);
                
                    return 0;
                }
                

                will output:

                "" - "00:00:00"
                "" - ""
                "" - ""
                Press <RETURN> to close this window...
                

                In that time, if we change date to (1,1,1) it will work fine:

                ...
                QDateTime qdt(QDate(0, 0, 0), QTime(0, 0, 0));
                ...
                

                result:

                "0001:01:01" - "00:00:00"
                "0001:01:01" - "00:00:05"
                "0001:01:01" - "01:00:05"
                Press <RETURN> to close this window...
                

                Maybe I don't understand something ?

                Tested with Qt 5.9.7 and Qt 5.12.4

                1 Reply Last reply
                0
                • JonBJ JonB

                  @r3d9u11
                  TimeSpan in C# has nothing to do with clocks/timers/timeouts/signals, it's just a data type for manipulating time periods, which happens to go down to milliseconds/ticks.

                  If you are in C++, given @SGaist's link to std::chrono and its members like duration, it hardly seems worth worrying about/waiting for Qt to implement anything when you have that to use. If you're using Python, the language has library datetime.timedelta.

                  R Offline
                  R Offline
                  r3d9u11
                  wrote on last edited by r3d9u11
                  #9

                  @jonb Hi, thak you for your notes. I'm on C++ and I don't need any related to timers,timeouts,signals. I just need to manipulate time interval. For example add milliseconds and finally get time interval on hours/minutes, or substract some seconds and finally get resulted time interval.

                  Well I implemented my own class QTimeSpan, it uses alot dividing opetions and remainder of the division but it is easy to use, implemented for the short time and does needed functions :D

                  However, QDateTime can do all these functions, but I faced with some strange but (as I noted below).

                  1 Reply Last reply
                  1
                  • SGaistS SGaist

                    Hi,

                    IIRC, there was at some point discussions about a class named QTimeSpan. You have to search for it.

                    Or maybe std::chrono may have what you want.

                    R Offline
                    R Offline
                    r3d9u11
                    wrote on last edited by
                    #10

                    @sgaist thank you fot hint. I'll read about it, I am a supporter of "standard" functions and methods (and hate to do own vechicles)

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      r3d9u11
                      wrote on last edited by r3d9u11
                      #11

                      Thank you all for answers and hints.

                      Well, finally there is 3 ways to work with time intervals in Qt/C++:

                      1. Qt-way: You can use QDateTime
                      2. C++-way: You can use std::chrono
                      3. Funny and ugly way: You can produce your own vehicle to implement functions what do you need

                      I think this theme can be closed.

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @r3d9u11 said in How to work with time intervals in Qt?:

                        but I was facing with some strange behavior or bugs

                        You simply didn't read the documentation: https://doc.qt.io/qt-5/qdate.html#QDate-2 : "Year 0 is invalid."

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        R 1 Reply Last reply
                        2
                        • Christian EhrlicherC Christian Ehrlicher

                          @r3d9u11 said in How to work with time intervals in Qt?:

                          but I was facing with some strange behavior or bugs

                          You simply didn't read the documentation: https://doc.qt.io/qt-5/qdate.html#QDate-2 : "Year 0 is invalid."

                          R Offline
                          R Offline
                          r3d9u11
                          wrote on last edited by r3d9u11
                          #13

                          @christian-ehrlicher you're right, I didn't do it completely (because mostly I read about QDateTime).
                          It seems like stick in the wheel. I hope there is some really important reasons to exclude zero from valid values.

                          In that case there is only two ways: clean C++ and own vehicle.

                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            @r3d9u11 said in How to work with time intervals in Qt?:

                            I hope there is some really important reasons to exclude zero from valid values.

                            Yes, read the documentation (as I said before): https://doc.qt.io/qt-5/qdate.html#remarks

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            R 1 Reply Last reply
                            2
                            • Christian EhrlicherC Christian Ehrlicher

                              @r3d9u11 said in How to work with time intervals in Qt?:

                              I hope there is some really important reasons to exclude zero from valid values.

                              Yes, read the documentation (as I said before): https://doc.qt.io/qt-5/qdate.html#remarks

                              R Offline
                              R Offline
                              r3d9u11
                              wrote on last edited by r3d9u11
                              #15

                              @christian-ehrlicher Yeah, I read it after was commenting, my bad :D
                              Thanks, again.

                              Finally I did that task with QDateTime and have got results like (example here):

                              "02:30:01.205"
                              Press <RETURN> to close this window...
                              

                              or

                              "146:30:01.205"
                              Press <RETURN> to close this window...
                              
                              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