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 () did not return correct result
Forum Updated to NodeBB v4.3 + New Features

QDateTime::toTime_t () did not return correct result

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 5.5k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    hpng
    wrote on last edited by
    #1

    Hello:

    This is the documentation for:
    uint QDateTime::toTime_t () const
    Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).
    On systems that do not support time zones, this function will behave as if local time were Qt::UTC.

    I ran this code fragment:
    @
    QDateTime UET(QDate( 1970, 1, 1), QTime(0, 0, 3, 0 ));
    unk = UET.toTime_t(); // debug data: unk = 25203
    @

    If I understand this right, I am suppose to get unk = 3 (three ) since it is
    three seconds from 1970, jan, 1st.

    and if you try with exactly 1970, 1, 1, you will not get a ZERO return, but
    unk = 25200s. See below.

    @
    QDateTime UET(QDate( 1970, 1, 1), QTime(0, 0, 1, 0 ));
    unk = UET.toTime_t(); // debug data: unk = 25200
    @
    So what gives?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hpng
      wrote on last edited by
      #2

      The document did not explain this.
      In fact, i think if you follow the description in the documentation, the code implementation for QDateTime::tTime_t () is incorrect. IMHO. Or they have to rewrite this portion of documentation to explain it clearly with calculation example.

      Here is Qt implementation:

      OK, I think I know the answer.
      I am at - 7 (minus 7 )hours from UTC (coordinate universal time).
      So on date 1970, 1, 1, zero hours my local time, Qt calculates the time passed relative to time of 1970, 1, 1 at where UTC time location is.
      So 7 * 60 * 60 = 25200!

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        You'll get the correct result with this QDateTime construction:

        @
        QDateTime testTime(QDate(1970, 1, 1), QTime(0, 0, 0, 0), Qt::UTC);
        @

        http://www.catb.org/~esr/faqs/smart-questions.html

        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