Adding / appending two QTimes together



  • I have two times, and I was wondering if QTime has the functionality to add or append times together.

    For example:
    I have 2 QStrings in the format 00:00:04 and 00:01:03 where the format is hour:minutes:seconds. If I convert them to a QTime and add them, i want the result to be 00:01:07 then convert it to a QString. I can attempt to do some wicked string processing but I am just curious if I could do this easily with QTime.

    Thanks in advance!


  • Moderators

    You might be interested in the threads with the "tag QTimeSpan":https://qt-project.org/search/tag/qtimespan

    Otherwise it could be possible to convert your strings to QTime convert this secons and the conversion back. Problems may occur when passing the 24 hour mark.



  • koahnig is right, you need something like QTimeSpan for that. Unfortunately, we've not been able to land that in Qt proper yet. Sorry about that! The sources are available though, and with a few tweaks (move the definition of some enums to the class itself) it should pose no big problem to compile into your own application. I do that myself.

    The issue you're facing is that QTime does not represent a length of time, but a point in time. That is an important distinction. Compare it with the distinction between a location (where you are) to a distance (between where you are and the fridge with your nice cool beer). They are different (yet related) concepts. In terms of time: your age is not the same as your birth date.

    QTimeSpan models the concept of a length of time, while QTime, QDate and QDateTime model a point in time. For points in time or space, the concept of adding or substracting them does not make sense, at least not if you expect the result to be another such point. QTimeSpan however defines the concept of substracting two dates or times to yield a time span, and it defines the natural operations like addition, substraction and multiplication, as well as operations like intersection and unions on time spans.



  • I see! Thank you for the detailed description, I will download the sources and give it a try!

    I've seen a lot of posts about QTimeSpan, hope it gets put into the Qt 4.8 soon.

    Thanks again guys.



  • QTimeSpan will not appear in Qt 4.8. There will only be bugfix updates for this series. It is also unlikely there will ever be a 4.9. Also, it will not be in Qt 5.0, as the feature freeze for 5.0 has already passed. So, the first version it might appear in, is in Qt 5.1. There is no planning yet when that version will appear.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.