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. QDateTime::addSecs does not return new object
Forum Updated to NodeBB v4.3 + New Features

QDateTime::addSecs does not return new object

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 687 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.
  • K Offline
    K Offline
    KonstantinosTsakalis
    wrote on last edited by
    #1

    [Working on Qt 6.5]

    Hello,

    My problem is two-fold.

    I have the following code to update my QML date and time:

    void CGeneralController::updateDateTime()
    {
        QDateTime myCurrentDateTimeUtc = QDateTime::currentDateTimeUtc();
        theCurrentDateTime             = myCurrentDateTimeUtc.addSecs(theTimeDifferenceSeconds);
        theDate = theCurrentDateTime.toString("dd/MM/yyyy");
        emit dateChanged();
        theTime = theCurrentDateTime.toString("hh:mm");
        emit timeChanged();
    }
    

    At this point, addSecs is working as expected because I can see indeed that theTime is correctly updated with the timeDifferenceSeconds which in this case is value 7200.

    Next, theCurrentDateTime is used to set the maximum in a QDateTimeAxis object. Instead of setting the correct time in the axis, the Framework sets a +7200 offset, which would be the case if one uses the local time as the calculation basis. However, this shouldn't be the case, as inside the function updateDateTime I am already adding this offset when needed.

    So,

    Problem 1
    I don't understand why my date time object is handled differently when I set it to the QDateTimeAxis. I have also checked that the timeSpec() in this case is correctly UTC so the offset should remain the same.

    Problem 2
    According to the documentation of addSecs, this function will return a new QDateTime object. However, when I introduce the following line inside my function

    void CGeneralController::updateDateTime()
    {
    QDateTime myCurrentDateTimeUtc = QDateTime::currentDateTimeUtc();
    theCurrentDateTime = myCurrentDateTimeUtc.addSecs(theTimeDifferenceSeconds);
    Q_ASSERT(myCurrentDateTimeUtc != theCurrentDateTime);
    theDate = theCurrentDateTime.toString("dd/MM/yyyy");
    emit dateChanged();
    theTime = theCurrentDateTime.toString("hh:mm");
    emit timeChanged();
    }

    my application does crash, which means that the two objects are the same. This operator actually checks all date, time and time zone, so the described behavior makes no sense to me.

    I have also checked the implementation of addSecs which makes use of the addMSecs here and I can see indeed that a new QDateTime dt(*this) object is contructed and returned. So again this is super strange, why do I get the assertion?

    Last, some time ago I had made a related post regarding date and time used in axis https://forum.qt.io/topic/151744/bug-qdatetimeaxis maybe this helps as well.

    Thank you.

    Christian EhrlicherC 1 Reply Last reply
    0
    • K Offline
      K Offline
      KonstantinosTsakalis
      wrote on last edited by
      #7

      Alright, I have finally gotten a clear reply from the support. Quoting:

      Right now, the Qt Charts and Qt Graphs modules do not support displaying QDateTimeAxis values using a specific timezone:

      • Previously, it always used the local timezone
      • Now, now with the fix from https://codereview.qt-project.org/c/qt/qtgraphs/+/576813/3/src/graphs2d/axis/datetimeaxis/qdatetimeaxis.cpp#b163 , it always uses UTC

      So yes, for now, you'll unfortunately need to manually adjust your data to get the display that you want.
      Please note: The Qt Charts module is deprecated, and the feature will only be implemented in the new Qt Graphs module.

      You can follow the requested feature here.

      To sum up, if you still use QtCharts and QDateTimeAxis, always make sure to manually "correct" the time-zone offset against the LocalTime time spec before you set the new object to the axis.

      1 Reply Last reply
      0
      • K KonstantinosTsakalis

        [Working on Qt 6.5]

        Hello,

        My problem is two-fold.

        I have the following code to update my QML date and time:

        void CGeneralController::updateDateTime()
        {
            QDateTime myCurrentDateTimeUtc = QDateTime::currentDateTimeUtc();
            theCurrentDateTime             = myCurrentDateTimeUtc.addSecs(theTimeDifferenceSeconds);
            theDate = theCurrentDateTime.toString("dd/MM/yyyy");
            emit dateChanged();
            theTime = theCurrentDateTime.toString("hh:mm");
            emit timeChanged();
        }
        

        At this point, addSecs is working as expected because I can see indeed that theTime is correctly updated with the timeDifferenceSeconds which in this case is value 7200.

        Next, theCurrentDateTime is used to set the maximum in a QDateTimeAxis object. Instead of setting the correct time in the axis, the Framework sets a +7200 offset, which would be the case if one uses the local time as the calculation basis. However, this shouldn't be the case, as inside the function updateDateTime I am already adding this offset when needed.

        So,

        Problem 1
        I don't understand why my date time object is handled differently when I set it to the QDateTimeAxis. I have also checked that the timeSpec() in this case is correctly UTC so the offset should remain the same.

        Problem 2
        According to the documentation of addSecs, this function will return a new QDateTime object. However, when I introduce the following line inside my function

        void CGeneralController::updateDateTime()
        {
        QDateTime myCurrentDateTimeUtc = QDateTime::currentDateTimeUtc();
        theCurrentDateTime = myCurrentDateTimeUtc.addSecs(theTimeDifferenceSeconds);
        Q_ASSERT(myCurrentDateTimeUtc != theCurrentDateTime);
        theDate = theCurrentDateTime.toString("dd/MM/yyyy");
        emit dateChanged();
        theTime = theCurrentDateTime.toString("hh:mm");
        emit timeChanged();
        }

        my application does crash, which means that the two objects are the same. This operator actually checks all date, time and time zone, so the described behavior makes no sense to me.

        I have also checked the implementation of addSecs which makes use of the addMSecs here and I can see indeed that a new QDateTime dt(*this) object is contructed and returned. So again this is super strange, why do I get the assertion?

        Last, some time ago I had made a related post regarding date and time used in axis https://forum.qt.io/topic/151744/bug-qdatetimeaxis maybe this helps as well.

        Thank you.

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        This is working as expected for me, no assertion:

        QDateTime myCurrentDateTimeUtc = QDateTime::currentDateTimeUtc();
        auto theCurrentDateTime = myCurrentDateTimeUtc.addSecs(1234);
        Q_ASSERT(myCurrentDateTimeUtc != theCurrentDateTime);
        

        I would say theTimeDifferenceSeconds is 0. Add some debug output to see the values.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • K Offline
          K Offline
          KonstantinosTsakalis
          wrote on last edited by
          #3

          @Christian-Ehrlicher You are very much welcome! Indeed, I had two calls in that function on start-up of the application. The first one that gave the assertion was using theTimeDifferenceSeconds with a value of 0. So that part of my problems is now clear.

          However, the other one still insists. The following code snippet:

          ...
          QDateTime myCurrentDateTime = theGeneralController->getTheCurrentDateTime();
              anAxisX->setMax(myCurrentDateTime);
              QString myDate = myCurrentDateTime.toString("dd/MM/yyyy");
              QString myTime = myCurrentDateTime.toString("hh:mm");
          
              qInfo() << "My date and time is: " + myDate + " "+ myTime;
          ...
          

          prints:
          "My date and time is: 18/07/2024 14:26"

          but the maximum timepoint of my graphs keeps on being set to two hours later.

          f62d5320-9412-4c14-827f-4b2e3c5bfc51-image.png

          I tried setting first the minimum but with no effect.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KonstantinosTsakalis
            wrote on last edited by KonstantinosTsakalis
            #4

            To make it even more straightforward:

            ...
                anAxisX->setMin(QDateTime::currentDateTimeUtc());
                anAxisX->setMax(QDateTime::currentDateTime());
            
                qInfo() << "My current date time utc: " << QDateTime::currentDateTimeUtc().toString("dd/MM/yyyy hh:mm");
                qInfo() << "My current date time: " << QDateTime::currentDateTime().toString("dd/MM/yyyy hh:mm");
            ...
            

            This is what I get with this setting
            119c4043-e208-4097-97a4-5497462671ca-image.png

            What I should get is a two hour difference as I am on CEST (UTC+2) as the log prints:

            My current date time utc:  "18/07/2024 13:11" 
            My current date time:  "18/07/2024 15:11"
            
            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #5

              I don't know anything about QDateTime axis but I would say it is using local time. If this is a bug or expected or can be changed is out of my knowledge - I don't use QML.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • K Offline
                K Offline
                KonstantinosTsakalis
                wrote on last edited by
                #6

                @Christian-Ehrlicher Thank you for your contribution and help.

                Alright I have managed to reach a reasonable explanation. The following code snippet:

                ...
                    QDateTime    myCurrentDateTime = theGeneralController->getTheCurrentDateTime();
                    int          myBeforeOffset    = myCurrentDateTime.offsetFromUtc();
                    Qt::TimeSpec myBeforeTimeSpec  = myCurrentDateTime.timeSpec();
                
                    anAxisX->setMax(myCurrentDateTime);
                
                    QDateTime    myAfterMax      = anAxisX->max();
                    int          myAfterOffset   = myAfterMax.offsetFromUtc();
                    Qt::TimeSpec myAfterTimeSpec = myAfterMax.timeSpec();
                
                    qInfo() << "My before offset: " + QString::number(myBeforeOffset);
                    qInfo() << "My before time spec: " + QString::number(myBeforeTimeSpec);
                    qInfo() << "My after offset: " + QString::number(myAfterOffset);
                    qInfo() << "My after time spec: " + QString::number(myAfterTimeSpec);
                ...
                

                prints

                "My before offset: 0" 
                "My before time spec: 1" // This is Qt::UTC
                "My after offset: 7200" 
                "My after time spec: 0" // This is Qt::LocalTime
                

                So, it seems that the QDateTimeAxis is indeed converting the passed QDateTime object from UTC to LocalTime internally. I don't know if this should be considered a bug, but I have not found any mention of this in the documentation. I have created a ticket with the support team on this, and I will update this thread once I have an answer from them as well.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  KonstantinosTsakalis
                  wrote on last edited by
                  #7

                  Alright, I have finally gotten a clear reply from the support. Quoting:

                  Right now, the Qt Charts and Qt Graphs modules do not support displaying QDateTimeAxis values using a specific timezone:

                  • Previously, it always used the local timezone
                  • Now, now with the fix from https://codereview.qt-project.org/c/qt/qtgraphs/+/576813/3/src/graphs2d/axis/datetimeaxis/qdatetimeaxis.cpp#b163 , it always uses UTC

                  So yes, for now, you'll unfortunately need to manually adjust your data to get the display that you want.
                  Please note: The Qt Charts module is deprecated, and the feature will only be implemented in the new Qt Graphs module.

                  You can follow the requested feature here.

                  To sum up, if you still use QtCharts and QDateTimeAxis, always make sure to manually "correct" the time-zone offset against the LocalTime time spec before you set the new object to the axis.

                  1 Reply Last reply
                  0
                  • K KonstantinosTsakalis has marked this topic as solved on

                  • Login

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