[Solved]How to get the difference between current time zone and UTC?



  • Hi all,
    hope u all can help me in solving this problem.
    When I read about the documentation of QTimeDate, I don't really understand on how to find the difference between the both time zone.
    Can anyone help me?
    At least give a clue or some function or maybe some explaination, I don't really need a full source code.

    By the way, this is my code snippet:

    @
    QDateTime now = QDateTime::currentDateTime();
    QString strTime = QString("Date Saved: " + now.toString("dd MMMM yyyy") + "<br/>"
    +"Day Saved: " + now.toString("dddd") + "<br/>"
    +"Time Saved: " + now.toString("hh:mm:ss ap") + "<br/>"
    +"Time At Crime Scene: " + now.toString(Qt::RFC2822Date) + "<br/>"
    +"Time Offset At UTC: " + now.toUTC().toString(Qt::RFC2822Date) + "<br/>"
    +"The Time Zone Abbreviation: " + now.timeZoneAbbreviation() + "<br/><br/>");
    ui->textBrowser->setHtml(strTime);
    @





  • Yeah, I did read QTimeZone Class.
    But, in the end, I still don't find the correct way to find the different between both time zone and display it in the QTextBrowser.



  • This "example":https://qt-project.org/doc/qt-5-snapshot/qdatetime.html#toUTC shows a difference between local and UTC time in seconds.
    Is it what you are looking for ?



  • Yup, this is some sort on how to find the difference between both time.
    But in the end, it will just return 0 because there is no difference between both time.
    Because it is still the same time but only in different places.

    What I want to come out in my QTextBrowser is something like this:
    UTC+08:00



  • Try this one
    @
    QDateTime local(QDateTime::currentDateTime());
    QDateTime UTC(local.toUTC());
    QDateTime dt(UTC.date(), UTC.time(), Qt::LocalTime);
    qDebug() << "Local time is:" << local;
    qDebug() << "UTC time is:" << UTC;
    qDebug() << "No difference between times:" << local.secsTo(UTC);
    qDebug() << "Here is the difference between times:" << local.secsTo(dt);
    qDebug() << "Here is the difference between times:" << dt.secsTo(local);

    @



  • Thanks, andreyc.
    It's really help a lot.
    Thanks for suggesting to use the third sentence. :)


Log in to reply
 

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