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. Converting a UTC time to local time
QtWS25 Last Chance

Converting a UTC time to local time

Scheduled Pinned Locked Moved Solved General and Desktop
utc time localconvert
6 Posts 2 Posters 6.2k 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.
  • M Offline
    M Offline
    McLion
    wrote on 5 Sept 2017, 13:01 last edited by
    #1

    Hi

    From data received I get a UTC time for an event (it actually comes in as 4-bit coded BCD).
    Is there some easy way to convert this to the local time?

    Thanks

    R 1 Reply Last reply 5 Sept 2017, 13:11
    0
    • M McLion
      5 Sept 2017, 13:01

      Hi

      From data received I get a UTC time for an event (it actually comes in as 4-bit coded BCD).
      Is there some easy way to convert this to the local time?

      Thanks

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 5 Sept 2017, 13:11 last edited by raven-worx 9 May 2017, 13:11
      #2

      @McLion

      int y = ...;
      int mm = ...;
      int d = ...;
      int h = ...;
      int m = ...;
      int s = ...;
      QDate date(y, mm, d);
      QTime time(h, m, s);
      QDateTime localTime = QDateTime(date, time, Qt::UTC).toLocalTime();
      

      Now you just need to fill up the ints (e.g. by bit shifting and masking) of your received data.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • M Offline
        M Offline
        McLion
        wrote on 6 Sept 2017, 12:53 last edited by
        #3

        Thanks - works a treat.
        Here's my finished solution:

        uchar h = BCDtoHex(inData->at(18));
        uchar m = BCDtoHex(inData->at(19));
        uchar s = BCDtoHex(inData->at(20));
        QDate date;
        if(h > 12)  date = QDate::fromJulianDay((inData->at(16) << 8) + inData->at(17) + 2400001);
        else        date = QDate::fromJulianDay((inData->at(16) << 8) + inData->at(17) + 2400000);
        QTime time(h, m, s, 0);
        QDateTime localTime = QDateTime(date, time, Qt::UTC).toLocalTime();
        

        The JulianDate thing needs some testing since my input data is in ModifiedJulianDate.
        If it does not work like above I have an extensive calculation formula on hand. However, the above would be quicker.

        1 Reply Last reply
        2
        • M Offline
          M Offline
          McLion
          wrote on 7 Sept 2017, 16:26 last edited by
          #4

          I have a follow up question:
          Is there an easy way to 'add' times - despite doing it manually and take care of the 'carry-over' at 60 and stuff?

          uchar dh = BCDtoHex(inData->at(21));
          uchar dm = BCDtoHex(inData->at(22));
          uchar ds = BCDtoHex(inData->at(23));
          QTime duration(dh, dm, ds, 0);
          
          // now add this to time from above to get the endtime, i.e. something like:
          QTime end = time + duration; 
          
          QDateTime localEndTime = QDateTime(date, end, Qt::UTC).toLocalTime();
          

          Thanks

          R 1 Reply Last reply 8 Sept 2017, 06:52
          0
          • M McLion
            7 Sept 2017, 16:26

            I have a follow up question:
            Is there an easy way to 'add' times - despite doing it manually and take care of the 'carry-over' at 60 and stuff?

            uchar dh = BCDtoHex(inData->at(21));
            uchar dm = BCDtoHex(inData->at(22));
            uchar ds = BCDtoHex(inData->at(23));
            QTime duration(dh, dm, ds, 0);
            
            // now add this to time from above to get the endtime, i.e. something like:
            QTime end = time + duration; 
            
            QDateTime localEndTime = QDateTime(date, end, Qt::UTC).toLocalTime();
            

            Thanks

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 8 Sept 2017, 06:52 last edited by
            #5

            @McLion
            why don't you simply take a look at the QDateTime docs?! I am sure you will find the method on your own...

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            M 1 Reply Last reply 8 Sept 2017, 08:14
            2
            • R raven-worx
              8 Sept 2017, 06:52

              @McLion
              why don't you simply take a look at the QDateTime docs?! I am sure you will find the method on your own...

              M Offline
              M Offline
              McLion
              wrote on 8 Sept 2017, 08:14 last edited by
              #6

              @raven-worx
              Seems I was not looking at the right place or not well enough.

              QTime zero(0, 0, 0, 0);
              QDateTime EndTimeLocal = StartTimeLocal.addSecs(zero.secsTo(duration));
              

              Thanks anyway :-)

              1 Reply Last reply
              1

              2/6

              5 Sept 2017, 13:11

              topic:navigator.unread, 4
              • Login

              • Login or register to search.
              2 out of 6
              • First post
                2/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved