PDA

View Full Version : QDateTime::currentMSecsSinceEpoch ()



fantom
15th May 2012, 16:43
Dear all,

I would like your help concerning the current Date Time acquisition. I am trying to send a message from a client to a DB server. I am structuring the message as follows:


int random = qrand() & 10000;
QString message = QString("%1 J/N " + QDateTime::currentDateTime().toString()).arg(rando m);
QByteArray msg = message.toUtf8();
QByteArray data = "MESSAGE " + QByteArray::number(msg.size()) + " " + msg;

I have no error but the final output of the date time isn't like 0000-00-00 00:00:00 as i expected, but it looks like this the message "256 J/N Di 15. Mai 17:41:39 2012".
Do you have any idea how to change it?

Thank you in advance!

Lesiok
15th May 2012, 16:48
Why do you think it has to be 0000? QDateTime::currentDateTime returns you the current time.
P.S.
I think I understand. You mean the date format. If you want to have this result, regardless of the settings on your computer you must explicitly specify the format of toString.

ChrisW67
15th May 2012, 23:18
If you want the number of milliseconds since the epoch, as your thread title implies, then you should call QDateTime::currentMSecsSinceEpoch() to get a single massive number that has no format. If you want a portable string representation of the current time then I suggest you use:


QString message = QString("%1 J/N %2").arg(random).arg(QDateTime::currentDateTimeUtc(). toString(Qt::ISODate));

Note that I have use UTC to allow for time zone and daylight savings differences between client and server.