1

I am using Qt5.6, MSVC2013 32bit on Windows as well as Qt5.4, 64 bit on Ubuntu. I am trying to parse Journal syslog to convert _SOURCE_REALTIME_TIMESTAMP into human readable text. Somehow the millisecond is all 0. Wondering what is correct way to show milliseconds or even microseconds?

bool ok;
QString ss = "1462962462893977";
const qlonglong s = ss.toLongLong(&ok );
if ( !ok ) {
   qDebug() << "Error1";
}
const QDateTime dt = QDateTime::fromTime_t( s/1000000 );
const QString textdate = dt.toString( "yyyy-MM-dd hh:mm:ss.zzz" );

qDebug() <<"==>" << textdate;

Result from PC/UBuntu is ==> "2016-05-11 03:27:42.000" The milliseconds are all 0's which I expect to have numbers.

Thanks!

thsieh
  • 620
  • 8
  • 24

1 Answers1

0

The reason the milliseconds are zero is because you have constructed the QDateTime using fromTime_t() which takes a number of seconds, so you lose your sub-second accuracy. You need to do something like:

const QDateTime dt = QDateTime::fromTime_t( s/1000000 ).addMSecs( (s/1000) % 1000 );

Or use QDateTime::fromMSecsSinceEpoch() which takes the number of milliseconds since the epoch, for example (assuming your timestamp is the number of microseconds since the epoch, which seems to be the case in the question):

const QDateTime dt = QDateTime::fromMSecsSinceEpoch( s/1000 );

I don't think you can work with microseconds using QDateTime, the documentation does not mention them anywhere.

Ulterno
  • 3
  • 1
  • 3
tinman
  • 6,348
  • 1
  • 30
  • 43