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. QDateTime.toTime_t() output a wrong result
Qt 6.11 is out! See what's new in the release blog

QDateTime.toTime_t() output a wrong result

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 938 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.
  • W Offline
    W Offline
    wuze
    wrote on last edited by
    #1

    Hello,
    I am trying to get a Unix time stamp in my QT application but it outputs a wrong date.

    QDateTime time = QDateTime::fromString("1966-06-21 00:00:00", "yyyy-MM-dd hh:mm:ss");
    qDebug() << time.toString("yyyy-MM-dd hh:mm:ss.zzz");
    uint t = time.toTime_t();
    qDebug() << QDateTime::fromTime_t(t).toString("yyyy-MM-dd hh:mm:ss.zzz");
    

    it outputs:

    "1966-06-21 00:00:00.000"
    "2106-02-07 14:28:15.000"
    

    Why does it output 2106? how do I get the Unix time stamp?

    Christian EhrlicherC C 2 Replies Last reply
    0
    • W wuze

      Hello,
      I am trying to get a Unix time stamp in my QT application but it outputs a wrong date.

      QDateTime time = QDateTime::fromString("1966-06-21 00:00:00", "yyyy-MM-dd hh:mm:ss");
      qDebug() << time.toString("yyyy-MM-dd hh:mm:ss.zzz");
      uint t = time.toTime_t();
      qDebug() << QDateTime::fromTime_t(t).toString("yyyy-MM-dd hh:mm:ss.zzz");
      

      it outputs:

      "1966-06-21 00:00:00.000"
      "2106-02-07 14:28:15.000"
      

      Why does it output 2106? how do I get the Unix time stamp?

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #3

      @wuze said in QDateTime.toTime_t() output a wrong result:

      Why does it output 2106? how do I get the Unix time stamp?

      A UNIX time stamp is a signed number to allow for dates before the epoch start. Usually time_t is defined the same way.

      This long-obsolete function returns an unsigned integer number of seconds after 1 Jan 1970 as @Christian-Ehrlicher points out. How it behaves for dates outside this range is clearly documented. This page also directs you to replacements.

      The function you are looking for is
      QDateTime::toSecsSinceEpoch()

      W 1 Reply Last reply
      6
      • W wuze

        Hello,
        I am trying to get a Unix time stamp in my QT application but it outputs a wrong date.

        QDateTime time = QDateTime::fromString("1966-06-21 00:00:00", "yyyy-MM-dd hh:mm:ss");
        qDebug() << time.toString("yyyy-MM-dd hh:mm:ss.zzz");
        uint t = time.toTime_t();
        qDebug() << QDateTime::fromTime_t(t).toString("yyyy-MM-dd hh:mm:ss.zzz");
        

        it outputs:

        "1966-06-21 00:00:00.000"
        "2106-02-07 14:28:15.000"
        

        Why does it output 2106? how do I get the Unix time stamp?

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @wuze said in QDateTime.toTime_t() output a wrong result:

        how do I get the Unix time stamp?

        You won't since the unix time starts at 01.01.1970

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        3
        • W wuze

          Hello,
          I am trying to get a Unix time stamp in my QT application but it outputs a wrong date.

          QDateTime time = QDateTime::fromString("1966-06-21 00:00:00", "yyyy-MM-dd hh:mm:ss");
          qDebug() << time.toString("yyyy-MM-dd hh:mm:ss.zzz");
          uint t = time.toTime_t();
          qDebug() << QDateTime::fromTime_t(t).toString("yyyy-MM-dd hh:mm:ss.zzz");
          

          it outputs:

          "1966-06-21 00:00:00.000"
          "2106-02-07 14:28:15.000"
          

          Why does it output 2106? how do I get the Unix time stamp?

          C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #3

          @wuze said in QDateTime.toTime_t() output a wrong result:

          Why does it output 2106? how do I get the Unix time stamp?

          A UNIX time stamp is a signed number to allow for dates before the epoch start. Usually time_t is defined the same way.

          This long-obsolete function returns an unsigned integer number of seconds after 1 Jan 1970 as @Christian-Ehrlicher points out. How it behaves for dates outside this range is clearly documented. This page also directs you to replacements.

          The function you are looking for is
          QDateTime::toSecsSinceEpoch()

          W 1 Reply Last reply
          6
          • C ChrisW67

            @wuze said in QDateTime.toTime_t() output a wrong result:

            Why does it output 2106? how do I get the Unix time stamp?

            A UNIX time stamp is a signed number to allow for dates before the epoch start. Usually time_t is defined the same way.

            This long-obsolete function returns an unsigned integer number of seconds after 1 Jan 1970 as @Christian-Ehrlicher points out. How it behaves for dates outside this range is clearly documented. This page also directs you to replacements.

            The function you are looking for is
            QDateTime::toSecsSinceEpoch()

            W Offline
            W Offline
            wuze
            wrote on last edited by
            #4

            @ChrisW67 This did the trick, thank you.

            1 Reply Last reply
            0
            • aha_1980A aha_1980 has marked this topic as solved on

            • Login

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