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. time zone other than UTC and local in QDateTimeEdit
Forum Updated to NodeBB v4.3 + New Features

time zone other than UTC and local in QDateTimeEdit

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 2.7k Views 2 Watching
  • 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.
  • K Offline
    K Offline
    kamorrissey
    wrote on last edited by
    #1

    I have a QDateTimeEdit in a form that I need to show times from time zones other than UTC and local. It's showing the time from an embedded device that might not be in the same time zone as the running form. Is there a way to do this?

    K 1 Reply Last reply
    0
    • K kamorrissey

      I have a QDateTimeEdit in a form that I need to show times from time zones other than UTC and local. It's showing the time from an embedded device that might not be in the same time zone as the running form. Is there a way to do this?

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @kamorrissey

      You can basically set any date and time you want with setDateTime
      or with the constructor

      Vote the answer(s) that helped you to solve your issue(s)

      K 1 Reply Last reply
      1
      • K koahnig

        @kamorrissey

        You can basically set any date and time you want with setDateTime
        or with the constructor

        K Offline
        K Offline
        kamorrissey
        wrote on last edited by
        #3

        @koahnig I know that, but I can only seem to get it to display in UTC or machine's local time zone. I need a way to have the functionality of QDateTimeEdit plus have it display the time in a specified time zone.

        K 1 Reply Last reply
        0
        • K kamorrissey

          @koahnig I know that, but I can only seem to get it to display in UTC or machine's local time zone. I need a way to have the functionality of QDateTimeEdit plus have it display the time in a specified time zone.

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @kamorrissey

          You need to show code snippet.

          QDateTime allows you to retrieve through its static members currentDateTime the current local date and time and there is also the UTC variant. When you are using those static members for initialization of QDateTimeEdit you are certainly getting local respectively UTC.

          However, you would need to initialize an instance of QDateTime with the value you like to use (e.g. using setDate or setTime.

          Vote the answer(s) that helped you to solve your issue(s)

          K 1 Reply Last reply
          0
          • K koahnig

            @kamorrissey

            You need to show code snippet.

            QDateTime allows you to retrieve through its static members currentDateTime the current local date and time and there is also the UTC variant. When you are using those static members for initialization of QDateTimeEdit you are certainly getting local respectively UTC.

            However, you would need to initialize an instance of QDateTime with the value you like to use (e.g. using setDate or setTime.

            K Offline
            K Offline
            kamorrissey
            wrote on last edited by
            #5

            @koahnig What I need is the editability of QDateTimeEdit, but have control over the time zone the widget displays in.

            K 1 Reply Last reply
            0
            • K kamorrissey

              @koahnig What I need is the editability of QDateTimeEdit, but have control over the time zone the widget displays in.

              K Offline
              K Offline
              kamorrissey
              wrote on last edited by
              #6

              What I discovered is that I needed to use QDateTime::toTimeZone(const QTineZone&) to convert the time and use QDateTimeEdit::setTimeSpec(Qt::TimeZone) on the widget.

              A 1 Reply Last reply
              3
              • K kamorrissey

                What I discovered is that I needed to use QDateTime::toTimeZone(const QTineZone&) to convert the time and use QDateTimeEdit::setTimeSpec(Qt::TimeZone) on the widget.

                A Offline
                A Offline
                agarvey
                wrote on last edited by
                #7

                To me, it looks like even though the solution from @kamorrissey got it to display with the correct time zone, the time that is returned is not correct.

                dte is a QDateTimeEdit*, timezone is a valid QByteArray and I'm using 5.15.2.

                auto time = QDateTime::currentDateTime();
                
                dte->setDateTime(time.toTimeZone(QTimeZone(timezone)));
                dte->setTimeSpec(Qt::TimeZone);
                
                auto test = dte->dateTime().toSecsSinceEpoch() - time.toSecsSinceEpoch();
                auto test2 = time.toTimeZone(QTimeZone(timezone)).toSecsSinceEpoch() - time.toSecsSinceEpoch();
                

                I'm seeing that test returns -3600 (the difference between my local time and the timezone) and test2 returns 0 (as expected). I believe both should return 0.

                SGaistS C 2 Replies Last reply
                0
                • A agarvey

                  To me, it looks like even though the solution from @kamorrissey got it to display with the correct time zone, the time that is returned is not correct.

                  dte is a QDateTimeEdit*, timezone is a valid QByteArray and I'm using 5.15.2.

                  auto time = QDateTime::currentDateTime();
                  
                  dte->setDateTime(time.toTimeZone(QTimeZone(timezone)));
                  dte->setTimeSpec(Qt::TimeZone);
                  
                  auto test = dte->dateTime().toSecsSinceEpoch() - time.toSecsSinceEpoch();
                  auto test2 = time.toTimeZone(QTimeZone(timezone)).toSecsSinceEpoch() - time.toSecsSinceEpoch();
                  

                  I'm seeing that test returns -3600 (the difference between my local time and the timezone) and test2 returns 0 (as expected). I believe both should return 0.

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @agarvey hi and welcome to devnet,

                  You need to provide more information about your timezone.

                  Showing all the different values you are using and getting would also help.

                  There's also the daylight saving time that might be at play at some point.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • A agarvey

                    To me, it looks like even though the solution from @kamorrissey got it to display with the correct time zone, the time that is returned is not correct.

                    dte is a QDateTimeEdit*, timezone is a valid QByteArray and I'm using 5.15.2.

                    auto time = QDateTime::currentDateTime();
                    
                    dte->setDateTime(time.toTimeZone(QTimeZone(timezone)));
                    dte->setTimeSpec(Qt::TimeZone);
                    
                    auto test = dte->dateTime().toSecsSinceEpoch() - time.toSecsSinceEpoch();
                    auto test2 = time.toTimeZone(QTimeZone(timezone)).toSecsSinceEpoch() - time.toSecsSinceEpoch();
                    

                    I'm seeing that test returns -3600 (the difference between my local time and the timezone) and test2 returns 0 (as expected). I believe both should return 0.

                    C Offline
                    C Offline
                    ChrisW67
                    wrote on last edited by ChrisW67
                    #9

                    @agarvey said in time zone other than UTC and local in QDateTimeEdit:

                    timezone is a valid QByteArray

                    On my Ubuntu system timezone is (also) an external long int declared in time.h. Without declaring a QByteArray called timezone this statement:

                    dte->setDateTime(time.toTimeZone(QTimeZone(timezone)));
                    

                    compiles happily here and becomes (in my UTC+10 TZ):

                    dte->setDateTime(time.toTimeZone(QTimeZone(-36000)));
                    

                    which takes the time given to the date edit on a wild excursion.
                    Make sure of what you are actually getting.

                    1 Reply Last reply
                    1

                    • Login

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