Qt World Summit: Register Today!

Is there any function is avalible in QT/c++ that we pass selected date and time as a string/ time_t structure a we get time in seconds since epoch ?

  • i want to pass date and time as string or i will fill the date and time in time_t structure
    defined in <time.h> as follows:

           struct tm {
               int tm_sec;    /* Seconds (0-60) */
               int tm_min;    /* Minutes (0-59) */
               int tm_hour;   /* Hours (0-23) */
               int tm_mday;   /* Day of the month (1-31) */
               int tm_mon;    /* Month (0-11) */
               int tm_year;   /* Year - 1900 */
               int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
               int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
               int tm_isdst;  /* Daylight saving time */

    after that i want time of that date and time in seconds since epoch .

    if i used this function so i get current time in seconds since epoch

    qint64 QDateTime::toSecsSinceEpoch() const
    Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00.000, Coordinated Universal Time (Qt::UTC).

  • @TM9412

    qint64 QDateTime::secsTo(const QDateTime &other) const
    Returns the number of seconds from this datetime to the other datetime. If the other datetime is earlier than this datetime, the value returned is negative.

    Before performing the comparison, the two datetimes are converted to Qt::UTC to ensure that the result is correct if daylight-saving (DST) applies to one of the two datetimes but not the other.

    Returns 0 if either datetime is invalid.


    QDateTime now = QDateTime::currentDateTime();
    QDateTime xmas(QDate(now.date().year(), 12, 25), QTime(0, 0));
    qDebug("There are %d seconds to Christmas", now.secsTo(xmas));

  • QDateTime class has all you need.

    Use "fromString" to retrieve a QDateTime and toSecsSinceEpoch to get the secs from epoch

Log in to reply