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. [Solved]How to get the difference between current time zone and UTC?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 7.3k 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.
  • S Offline
    S Offline
    slumberdin
    wrote on last edited by
    #1

    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);
    @

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rahul Das
      wrote on last edited by
      #2

      What about "QTimeZone":https://qt-project.org/doc/qt-5-snapshot/qtimezone.html Class ?


      Declaration of (Platform) independence.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        slumberdin
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #4

          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 ?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            slumberdin
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andreyc
              wrote on last edited by
              #6

              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);

              @

              1 Reply Last reply
              1
              • S Offline
                S Offline
                slumberdin
                wrote on last edited by
                #7

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

                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