Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]QDateTime problem

[SOLVED]QDateTime problem

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nicho24
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MrMNight
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MrMNight
        wrote on last edited by
        #3

        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"

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nicho24
          wrote on last edited by
          #4

          Excellent! It works to me.

          Thanks.

          Nicho

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MrMNight
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved