1

I try to read seconds after 1970 to a QDateTime. I.e.: startT = 1.390309552938E9

Therefor I use:

QDateTime time = QDateTime::fromMSecsSinceEpoch(startT*1000); // *1000 because a have seconds while qt expecting milliseconds

now I want to compare this to another time I get with QDateTime::toString:

QDate startDate = QDate::fromString(datestr, "yyyyMMdd");
QTime time = QTime::fromString(timestr, "hhmmss");
QDateTime datetime = QDateTime(date, time, Qt::UTC);

datestr and timestr look for example like this: '20140121' and '130358'

I want to compare if time and datetime are equal (to the seconds, so excepts of milliseconds) therefore I use something like this:

if (time < datetime)

but it doesn't work?! Can someone help me here?

Thank you!

edit:

I do:

QDateTime time = QDateTime::fromMSecsSinceEpoch(startT*1000);

then I give this Information (time) back with:

void Data::setTime(const QDateTime& obTime)
{
    time = QDateTime(obTime);
}

where obTime is my times

later in the code I return my value by:

QDateTime time = newData.getTime();

any getTime is:

QDateTime MetObs::getTime() const
{
    return time;
}

The retrieval of datetime is the same as mentioned above. There is no complication with the two definitions of "time" because it is at another position of the Programm

And than I try to use:

if (time < datetime)

Another Information:

when I print out the values as Strings with:

QString timestring = time.toString(Qt::ISODate);
QString datetimestring = datetime.toString(Qt::ISODate);
cout << timestring.toAscii().data() << endl;
cout << datetimestring.toAscii().data() << endl;

this comes out:

2014-01-21T13:03:59
2014-01-21T13:03:58Z
smaica
  • 723
  • 2
  • 11
  • 26
  • How can you compare `QTime` with `QDateTime`? Maybe it is better to compare like: `if (time < datetime.time())`? – vahancho Apr 16 '14 at 10:03
  • You have two variables called `time`. Is this the case in your actual code? – thuga Apr 16 '14 at 10:09
  • no this is not my actual code so there is no problem with the variable time. datetime is also a QDateTime, consisting of QDate and QTime – smaica Apr 16 '14 at 11:33
  • 1
    By the way, you can use [`QDateTime::fromTime_t`](http://qt-project.org/doc/qt-5/qdatetime.html#fromTime_t-2) instead of `fromMSecsSinceEpoch` if you are using seconds. – thuga Apr 16 '14 at 12:44
  • Okay. I solved my problem but I don't know why: after I read in time with fromTime_t I seperate QDateTime in QDate and QTime with QDate date = time.date(); QTime times = datetimes.time(). Then I put both together again: QDateTIme datetime = QDateTime(date, time, Qt::UTC). Now it works?! And printing out the strings results in samne strings, both witz Z at the end – smaica Apr 16 '14 at 12:54
  • This is fishy in a couple of aspects: 1) You could use chrono. 2) You could get the current datetime without the epoch complication. 3) You could use != instead of <, and so on ... – László Papp Apr 17 '14 at 06:34

1 Answers1

0

Compare like this:

time.toTime_t() == datetime.toTime_t()

The method returns seconds since the epoch.

Note 1: Why did you use < if you want to test for equality?

Note 2: You should not store time values in a double - it may cause loss of precision. uint32_t is good for seconds, and uint64_t is required ofr milliseconds.

laune
  • 31,114
  • 3
  • 29
  • 42
  • I don't use < for equality. This is part of a bigger if-statement I'm sorry to be confusing. I can't chance the compare-statement. This is part of a routine that is used by many inputs and must stay the same. – smaica Apr 16 '14 at 11:35
  • What do you think you are doing? You aren't showing all of the code, and what you are showing is not the real code - and you are asking why "it does not work". – laune Apr 16 '14 at 12:08
  • my code is about 10000++ lines .. but I will try again: – smaica Apr 16 '14 at 12:11
  • Just the relevant code - better a complete example that does not work and the spec what it should do. Should be simple in this case. – laune Apr 16 '14 at 12:28