[SOLVED]QDateTime problem



  • Hi,

    I got uint value from a QDateTime by calling toTime_t, and then create a new QDateTime with this uint value by calling fromTime_t. But the two QDateTimes look different. Could you please point out where is the problem from? I am using Qt5.1.1.

    The following is my code:
    @
    QDate myD(2010,4,4);
    QTime myT(8,8,59,995);
    QDateTime myDT = QDateTime(myD,myT,Qt::UTC);
    QDateTime newDT = QDateTime::fromTime_t(uint(myDT.toTime_t()));
    newDT.setTimeSpec(Qt::UTC);
    qDebug()<<"myDT="<<myDT.toString("yyyy-MM-dd:hh-mm-ss-zzz");
    qDebug()<<"newDT="<<newDT.toStrimg("yyyy-MM-dd:hh-mm-ss-zzz");
    @

    The output is:
    myDT="2010-04-04:08-08-59-995"
    newDT="2010-04-04:09-08-59-000"

    From the output, the different time of the two QDatetimes is less than 1 hour.

    Thanks in advance,

    Nicho



  • see toTime_t method, it is uint, and it doesn't contain milliseconds, so you've lost milliseconds. 1 hour - is your localtime shift from UTC.



  • try this if you really need to work with time_t:
    @
    QDate myD(2010,4,4);
    QTime myT(8,8,59,995);
    QDateTime myDT(myD,myT, Qt::UTC);
    QDateTime newDT = QDateTime::fromTime_t(myDT.toTime_t());
    newDT = newDT.toUTC();
    @
    but maybe you really need this using qint64 without lost msecs:
    @
    QDate myD(2010,4,4);
    QTime myT(8,8,59,995);
    QDateTime myDT(myD,myT, Qt::UTC);
    QDateTime newDT = QDateTime::fromMSecsSinceEpoch(myDT.toMSecsSinceEpoch());
    newDT = newDT.toUTC();
    @
    Its worked for my system (ubuntu):
    myDT= "2010-04-04:08-08-59-995"
    newDT= "2010-04-04:08-08-59-995"



  • Excellent! It works to me.

    Thanks.

    Nicho



  • You are welcome, don't forget to mark topic as [SOLVED]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.