[Qt-interest] How calculate time span (days, hours, minutes) QDateTime difference?

Sean Harmer sean.harmer at maps-technology.com
Sat Jun 12 01:50:20 CEST 2010


Hi,

On Friday 11 June 2010 21:52:32 Dan White wrote:
> OK, how about this:
> 
> #include <QtCore/QCoreApplication>
> #include <QTime>
> #include <QDateTime>
> #include <QDebug>
> 
> int main(int argc, char *argv[])
> {
>      QCoreApplication a(argc, argv);
> 
>      // DateTime departure = new DateTime(2010, 6, 12, 18, 32, 0);
>      // DateTime arrival = new DateTime(2010, 6, 13, 22, 47, 0);
>      // TimeSpan travelTime = arrival - departure;
>      // Console.WriteLine("{0} - {1} = {2}", arrival, departure,
> travelTime);
>      // The example displays the following output:
>      //       6/13/2010 10:47:00 PM - 6/12/2010 6:32:00 PM = 1.04:15:00
> 
>      QDateTime departure = QDateTime::fromString ( "2010, 06, 12, 18,
> 32, 00", "yyyy, MM, dd, HH, mm, ss" ) ;
>      QDateTime arrival   = QDateTime::fromString ( "2010, 06, 13, 22,
> 47, 00", "yyyy, MM, dd, HH, mm, ss" ) ;
> 
>      int days = departure.daysTo ( arrival ) ;
> 
>      QTime when = QTime ( 0, 0, 0, 0 ) ;
> 
>      when = when.addSecs ( departure.addDays(days).secsTo( arrival ) ) ;
> 
>      qDebug()
>      << arrival.toString( "M/d/yyyy h:mm:ss AP" )
>      << " - "
>      << departure.toString( "M/d/yyyy h:mm:ss AP" )
>      << " = "
>      << days << when.toString( ".HH:mm:ss" ) ;
> }
> 
> When I run it, I get:
> 
> "6/13/2010 10:47:00 PM"  -  "6/12/2010 6:32:00 PM"  =  1 ".04:15:00"
> 
> Now, can anyone tell me how to get rid of them quote marks ?

Yes as Scott mentioned, don't use qDebug() like that. Prepare your output 
string using QString::arg() or QStringBuilder to construct your final string 
and send the whole thing to qDebug() at once. Or just don't use qDebug() ;-)

> Why make another class ? 

Because a time interval is a separate concept from a QTime or QDateTime which 
are points in time relative to a defined origin or epoch (well QTime can be 
interpreted as absolute or relative). A time interval has no origin. You could 
look at it the other way around and say why do we have QDateTime and QTime as 
these could be easily defined from a QTimeSpan and an epoch.

One reason is convenience. Nobody ever said that the calculation of a time 
interval was particularly tricky but it is nice syntactic sugar to have and 
for some use cases having such a class that represents this concept would be 
very useful and makes dealing with time intervals more natural.

Of course all of this assumes all times are taken in the same inertial frame 
of reference. You can get really funky if you introduce different inertial 
frames of reference or sidereal time etc into the picture! That is way beyond 
the scope of Qt though ;-)

> Seems like a waste.

Of what? My time (pun intended)? It's my time to waste and if it is of use to 
others as well as myself then why not push it as a merge request? Nobody would 
force you to use the class if you would rather do it manually.

Sean



More information about the Qt-interest-old mailing list