How to work with time intervals in Qt?
-
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.
-
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.
-
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.
@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 likeduration
, 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 librarydatetime.timedelta
. -
a long time ago @Andre was working on that.
I found the thread here on the forum:
https://forum.qt.io/topic/3160/qtimespan-interesthope it helps
-
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.
@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
-
@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 likeduration
, 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 librarydatetime.timedelta
.@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).
-
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.
-
Thank you all for answers and hints.
Well, finally there is 3 ways to work with time intervals in Qt/C++:
- Qt-way: You can use QDateTime
- C++-way: You can use std::chrono
- Funny and ugly way: You can produce your own vehicle to implement functions what do you need
I think this theme can be closed.
-
@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."
-
@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."
@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.
-
@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
-
@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
@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...