How do I get milliseconds in a QDateTime with QSqlQuery in Qt C++?

前端 未结 1 1029
旧时难觅i
旧时难觅i 2021-01-21 17:42

I have this sql query:

SELECT LOG_TIME FROM PCY_LOG_EVENTS;

This returns data in the format \"DD-MMM-YY HH.MM.SS.MS\" like this:



        
相关标签:
1条回答
  • 2021-01-21 18:38

    Get the query value as a QString

    QString dateTimeString = query.value(0).toString();
    

    Then use the static fromString function of the QDateTime. You have to specify the format of your string. I assume the days of the month have a leading zero

    QDateTime dateTime = QDateTime::fromString(dateTimeString, "dd-MMM-yy hh.mm.ss.zzz000000 A")
    

    Notice the milliseconds part :zzz000000. Since the max value can be 999 the trailing zeros of your example make no sense. So by using the zzz followed by the zeros you can get the miliseconds stored in your string. The only possible problem is that your month part uses upper case letters while the MMM returns the month abbreviation with just the first letter capitalized. I hope there won't be a problem with it.

    Once you do the conversion you can easily get the milliseconds:

    int ms = dateTime.time().msec();
    

    For more formatting options here

    0 讨论(0)
提交回复
热议问题