I have the following region of data being read into a QDataStream object :
DE 07 05 19 0E 28 1A
This should translate to the date/time: 25-05-2014 15:40:26
I am trying to use the following to read this into a QDateTime variable:
QFile fileIn(iFile);
if (!fileIn.open(QIODevice::ReadOnly)) return;
QDataStream data(&fileIn);
data.setByteOrder(QDataStream::LittleEndian);
data.setVersion(QDataStream::Qt_5_0);
data.skipRawData(32);
.
.
QDateTime time;
data >> time;
qDebug () << time.date();
Instead I get null/blank in time.
Output is:
QDate("")
The first thing here is how on earth this data is serialised. I've had a look and can't work it out: each byte doesn't nicely convert to each part of date/time, the bytes you are trying to reverse engineer aren't the epoch value of that date converted into hex. You can't expect QDateTime to magically know your raw data format. Report back with data format and I'll try and help.
Related
I receive a response from the Clockify JSON API which contains dates and times in ISO8601 format.
The format of the strings looks fine to me, but when I convert them to a QDateTime with QDateTime::fromString, I get strange and incorrect values:
QString aString = "2021-09-10T15:56:00Z";
QString aDifferentString = "2021-09-10T15:56:00";
QString theString = durationInfo.value("end").toString();
QDateTime aDateTime = QDateTime::fromString(aString, Qt::ISODate);
QDateTime anotherDateTime = QDateTime::fromString(aString, "yyyy-MM-ddTHH:mm:ssZ");
QDateTime aDifferentDateTime = QDateTime::fromString(aDifferentString, Qt::ISODate);
endTime = QDateTime::fromString(theString,Qt::ISODate);
But when I debug, I see the variables have these values after the code has run:
DateTime Thu Jan 31 07:23:38 1974 QDateTime
aDifferentDateTime Sat Nov 27 22:39:02 1971 QDateTime
aDifferentString "2021-09-10T15:56:00" QString
aString "2021-09-10T15:56:00Z" QString
anotherDateTime Sat Nov 27 22:39:02 1971 QDateTime
endTime Thu Jan 31 07:23:38 1974 QDateTime
theString "2021-09-10T15:56:00Z" QString
Am I missing something here, or is the fromString function or the format spec broken? The rest of the logic in my program behaves correspondingly weird because the dates are all wrong.
I'm using Qt Creator 4.13.0
Based on Qt 5.15.0 (MSVC 2019, 64 bit)
Built on Aug 25 2020 10:06:59
From revision fff3b41833
The problem here is/was the display of Qt types in the debugger.
The variables view shows erroneous values for the QDateTime variables (and possibly others). These are not the same as values printed if you pass them to, for example, qDebug().
The lesson to be learned is to double-check if the debugger is telling you the truth.
Why the values displayed in the debugger should be wrong, I'm not sure...
I have a QByteArray i create manually:
QByteArray hexArray(QByteArray::fromHex("495676"));
If this was encoded ASCII it would be "IVv".
If I want to get a single byte of data from that array.
I can do that like this:
qDebug() << messageToBeSent_raw[0];
However, that outputs I, which is correct but I would like to get 49. What I'm looking for is an equivalent of the QByteArray::toHex() just for a single byte. Is there a way to do it?
You can use QString::number.
qDebug() << QString::number(hexArray[0], 16);
I'm having trouble with a seemingly very simple problem: I want to get a QDateTime from a QString containing a timestamp. I got the timestamp from PostgreSQL, but it doesn't matter. Here is the code that does not work:
QString timestamp = "2010-10-09 19:21:46+02:00";
QString format = "YYYY-MM-DD HH:MM:SSTZD";
QDateTime dt = QDateTime::fromString(timestamp, format);
qDebug() << dt.toString(); // outputs empty string
There must be something very obvious I'm missing. Thanks!
There were two mistakes I was making. There is no TZD in the format specifications, so I removed the time zone information since I do not need it in my app by doing:
timeStamp.chop(6);
And then used the following format to get a QDateTime. Note the lowercase format characters:
QDateTime createdAt = QDateTime::fromString(timeStamp, "yyyy-MM-dd HH:mm:ss");
Thanks to everyone above for helping out.
I wish QDateTime overrode the - operator and returned a QTimeSpan representing the difference between two QDateTimes (just like .NET's TimeSpan). Since this doesn't exist in Qt, I decided to implement it.
Unfortunately, QDateTime has no msecsTo-like function. What is the cleanest way to get the difference between two QDateTimes accurate to the millisecond?
I realize that this question is from 2010, and that Qt 4.7 didn't exist back then (it actually came out about a week after this question was originally asked--September 21 2010), but for people who are looking for how to do this now:
As of Qt 4.7, QDateTime has a "msecsTo" method. See Qt 4.8 documentation at http://doc.qt.io/qt-4.8/qdatetime.html#msecsTo.
QDateTime dateTime1 = QDateTime::currentDateTime();
// let's say exactly 5 seconds pass here...
QDateTime dateTime2 = QDateTime::currentDateTime();
qint64 millisecondsDiff = dateTime1.msecsTo(dateTime2);
// millisecondsDiff is equal to 5000
I would probably use a.daysTo(b)*1000*60*60*24 + a.time().msecsTo(b.time()). Note that you need to watch how close you can be, since you're going to overflow your data type rather quickly.
how about this:
QDateTime a = QDateTime::currentDateTime();
QDateTime b = a.addMSecs( 1000 );
qDebug( "%d", a.time().msecsTo( b.time() ) );
Source
All the solutions are good, but if your timestamp is in a particular format then you can find the difference by below, and also you can assign that to QString.
let start is time on some moment in some format
QString start = QDateTime().currentDateTime().toString("hh:mm:ss AP dd/MM/yyyy");
let end is time on some moment after start in some format
QString end = QDateTime().currentDateTime().toString("hh:mm:ss AP dd/MM/yyyy");
So, you can find the difference by providing the format and using msecsto and assign them to QString(if required).
QString timeDiff= QString("%1").arg(QDateTime().fromString(start ,"hh:mm:ss AP dd/MM/yyyy").msecsTo(QDateTime().fromString(end ,"hh:mm:ss AP dd/MM/yyyy")));
I have a Unix timestamp and I need to convert it to human readable date + time. How can it be done in Qt?
int unixTime = 1234567890;
QDateTime timestamp;
timestamp.setTime_t(unixTime);
qDebug() << timestamp.toString(Qt::SystemLocaleShortDate);
That should get you going. Like Matthew said, see QDateTime.setTime_t, as well as QDateTime.toString. The toString has an enumeration with several different options, as well as an overload where you can pass a string allowing however much customization you like.
QDateTime.setTime_t
You can use the static function: "fromTime_t", like:
qDebug() << QDateTime::fromTime_t(your_time_stamp).toString("dd/MM/yyyy hh:mm:ss");
One good example
qint64 qiTimestamp=QDateTime::currentMSecsSinceEpoch();
QDateTime dt;
dt.setTime_t(qiTimestamp/1000);
ui->lineEdit_DateTime->setText(dt.toString("yyyy-MM-dd hh:mm:ss"));
Note that QDateTime::setTime_t() has been deprecated, use QDateTime::setSecsSinceEpoch(). Same for fromTime_t(): use QDateTime::fromSecsSinceEpoch().
As mentioned, instead of using the obsolete _t members here is a working example (Qt 5).
qint64 llTS=1591132400;
QString szFull=QDateTime::fromSecsSinceEpoch(llTS).toString("dddd d MMMM yyyy hh:mm:ss");
szFull: Tuesday 2 June 2020 23:13:20
Source: QDateTime (Qt 5)
Date formatting: QDate
Time formatting: QTime