QDateTime::toTime_t () did not return correct result



  • 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?



  • 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!



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

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


Log in to reply
 

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