[Qt-interest] How calculate time span (days, hours, minutes) QDateTime difference?
Andre Somers
andre at familiesomers.nl
Sat Jun 12 09:18:11 CEST 2010
Op 11-6-2010 19:49, Sean Harmer schreef:
> Hi,
>
> On Friday 11 June 2010 16:58:08 Ed Sutton wrote:
>
>>> http://doc.trolltech.com/4.6/qdatetime.html#secsTo
>>>
>>> gives you seconds.
>>>
>>> The rest is trivial.
>>>
>> Thank you Dan and Jason.
>>
>> QDateTime::secsTo() does the trick with some additional work.
>>
>> I was hoping I had missed the Qt equivalent of the .NET TimeSpan structure.
>>
>> http://msdn.microsoft.com/en-us/library/system.timespan.aspx
>>
> Yes I have been thinking about adding something like this for a while now.
> Maybe I will try to put a merge request together on gitorious for such a
> class.
>
> Does anybody have any other features that they would like for such a class?
>
I think a QTimeSpan class would be very useful. I have written some code
to handle time differences before. Having a class to neatly wrap it all
would be nice. I would be willing to help write it.
I noticed that the operator-() of QDate, QTime and QDateTime have not
been defined yet. That is excellent, because that means that these could
be implemented to return a QTimeSpan. That would add to having it feel
natural.
The class should also be useable without this however. It should be
possible to set the time difference manually in each supported unit, and
both as the total time span as the time span in that unit. To illustrate:
QTimeSpan::setTotalSeconds(qint64) should set the complete value of the
time span based on the passed number of seconds, while
QTimeSpan::setSeconds(int) should set the seconds only in the range of 0
to 59. Any other value would either be ignored or raise an exception.
Say, the current time span is 6 hours, 21 minutes and 17 seconds, then
calling setSeconds(53) should result in a time span representing 6
hours, 21 minutes and 53 seconds, while calling setTotalSeconds(53)
should result in a QTimeSpan representing just 53 seconds and
setTotalSeconds(1297313) would result in a time span representing 6
hours, 21 minutes and 53 seconds again.
Also, I think the class should have methods to create a nice textual
representation of the time span. Such a representation should be
flexible in the units used, and the precision required. For instance,
you should be able to set the maximum unit used (years, weeks, days,
....) and the minimum units used (minutes, seconds, even milliseconds
perhaps?) as well as the maximum number of such units to display at the
same time. This is very useful for display an estimate remaining time
for a long operation, for instance. I have found that I rarely need more
than two units in such cases. If the biggest unit is days, then what do
I care about minutes? For minutes, I find that seconds become only
interesting under ten minutes or so.
For other uses however, a more precise notation may be useful. The code
that I wrote before handles the above, and I would be willing to
contribute that and adapt it.
QTimeSpan should have all the natural operators implemented: addition
and substraction with other QTimeSpans, multiplication and devision with
scalars, and all comparision operators. Also needed are addition and
substraction operators with the Qt time and date related classes.
Careful thought has to be given if there is such a thing as a negative
time span, and what that means. You could say that substracting a later
QDateTime from an earlier one would result in a negative QTimeSpan, but
that would mean that negative means "in the future". Does that feel
natural? And how do you represent that, both in the API (what would
QTimeSpan::seconds() return in such a case?) and textual?
There are more issues to considder of course...
André
More information about the Qt-interest-old
mailing list