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. How to split in DD HH MM SS from hours more then 24 hours
Forum Updated to NodeBB v4.3 + New Features

How to split in DD HH MM SS from hours more then 24 hours

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 874 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.
  • blackout69B Offline
    blackout69B Offline
    blackout69
    wrote on last edited by blackout69
    #1

    Hello everybody,
    with a mysql query I extract the sum of the hours of a work process and I want to display them in the format DD HH: MM: SS
    I am using this code which works fine within 24 hours.
    But for all processing beyond 24 QTime it fails

    QString query = QString("SELECT TIME(SUM(SEC_TO_TIME(time_work))) FROM works WHERE id_work = 1 GROUP BY(id_employee) ORDER BY id_work ASC");
      QSqlQuery query_time;
      query_time.prepare(query);
    
      if (query_time.exec())
        {
          while (query_time.next())
            {
              sec_work = sec_work + QTime(0, 0, 0).secsTo(query_time.value(0).toTime());
            }
          int dd = ?                            // my days (DD)
          int hh = sec_work/3600;               // my hours (HH)
          int mm = (sec_work%3600)/60;          // my minuts (MM)
          int ss = (sec_work%3600)%60;          // my seconds (SS)
        }
    

    How can I find the days (DD) if the hours exceed 24 ?
    Thank you in advance

    blackout69

    artwawA JonBJ 2 Replies Last reply
    0
    • blackout69B Offline
      blackout69B Offline
      blackout69
      wrote on last edited by
      #10

      Thanks for the suggestions.
      I solved it like this:

      // my query:
      QString query = QString("SELECT lastname, date, SUM(time_work) FROM works WHERE id_work = 1 GROUP BY(id_employee) ORDER BY id_work ASC");
      
      // my custom delegate:
      void ColCustomTimeDelegato::paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
      {
        int value = index.model()->data(index, Qt::DisplayRole).toInt();
        QStyleOptionViewItem myOption = option;
        myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
        drawDisplay(painter, myOption, myOption.rect, QString("%1:%2:%3").arg(value/3600).arg((value%3600)/60).arg((value%3600)%60));
        drawFocus(painter, myOption, myOption.rect);
      }
      

      It works very well.
      Thanks again.

      1 Reply Last reply
      0
      • blackout69B blackout69

        Hello everybody,
        with a mysql query I extract the sum of the hours of a work process and I want to display them in the format DD HH: MM: SS
        I am using this code which works fine within 24 hours.
        But for all processing beyond 24 QTime it fails

        QString query = QString("SELECT TIME(SUM(SEC_TO_TIME(time_work))) FROM works WHERE id_work = 1 GROUP BY(id_employee) ORDER BY id_work ASC");
          QSqlQuery query_time;
          query_time.prepare(query);
        
          if (query_time.exec())
            {
              while (query_time.next())
                {
                  sec_work = sec_work + QTime(0, 0, 0).secsTo(query_time.value(0).toTime());
                }
              int dd = ?                            // my days (DD)
              int hh = sec_work/3600;               // my hours (HH)
              int mm = (sec_work%3600)/60;          // my minuts (MM)
              int ss = (sec_work%3600)%60;          // my seconds (SS)
            }
        

        How can I find the days (DD) if the hours exceed 24 ?
        Thank you in advance

        blackout69

        artwawA Offline
        artwawA Offline
        artwaw
        wrote on last edited by
        #2

        @blackout69 I am not sure if I understood your problem correctly but 24hrs is 86400 seconds. So, to get full days (Assuming no. of seconds is greater than 86400) would lead to int dd = sec_work/86400;

        For more information please re-read.

        Kind Regards,
        Artur

        1 Reply Last reply
        1
        • blackout69B blackout69

          Hello everybody,
          with a mysql query I extract the sum of the hours of a work process and I want to display them in the format DD HH: MM: SS
          I am using this code which works fine within 24 hours.
          But for all processing beyond 24 QTime it fails

          QString query = QString("SELECT TIME(SUM(SEC_TO_TIME(time_work))) FROM works WHERE id_work = 1 GROUP BY(id_employee) ORDER BY id_work ASC");
            QSqlQuery query_time;
            query_time.prepare(query);
          
            if (query_time.exec())
              {
                while (query_time.next())
                  {
                    sec_work = sec_work + QTime(0, 0, 0).secsTo(query_time.value(0).toTime());
                  }
                int dd = ?                            // my days (DD)
                int hh = sec_work/3600;               // my hours (HH)
                int mm = (sec_work%3600)/60;          // my minuts (MM)
                int ss = (sec_work%3600)%60;          // my seconds (SS)
              }
          

          How can I find the days (DD) if the hours exceed 24 ?
          Thank you in advance

          blackout69

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #3

          @blackout69 said in Hot to split in DD HH MM SS from hours more then 24 hours:

          But for all processing beyond 24 QTime it fails

          QTime represents a time-of-day, 0 to 24 hours. So you will have to deal with your "multiple days" part outside of a QTime.

          For the rest, as @artwaw says.

          1 Reply Last reply
          2
          • blackout69B Offline
            blackout69B Offline
            blackout69
            wrote on last edited by
            #4

            Hi Artur,
            yes of course this is correct. But my query returns a TIME value for example of 35:46:24 in HH: MM: SS and when I pass it to QTime it fails.

            JonBJ 1 Reply Last reply
            0
            • blackout69B blackout69

              Hi Artur,
              yes of course this is correct. But my query returns a TIME value for example of 35:46:24 in HH: MM: SS and when I pass it to QTime it fails.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #5

              @blackout69 said in Hot to split in DD HH MM SS from hours more then 24 hours:

              35:46:24 in HH: MM: SS and when I pass it to QTime it fails.

              As I just wrote above....

              1 Reply Last reply
              0
              • blackout69B Offline
                blackout69B Offline
                blackout69
                wrote on last edited by
                #6

                sure, but my query returns a value of type TIME and I cannot do operations with QTime if the value exceeds 24 hours. Maybe I should turn it all into seconds and move on. But I don't know which method to use to do this.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  Hi,

                  Why not just retrieve the sum in your query rather than making it a TIME type ?

                  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
                  • blackout69B Offline
                    blackout69B Offline
                    blackout69
                    wrote on last edited by
                    #8

                    Hi,
                    for my application, I need to show the processing time on a QTableView.
                    This is the code that shows the data I extract from the query .

                    //My delegate...
                    
                    ColTimeDelegate::ColTimeDelegate(QObject *parent) : QItemDelegate(parent)
                    {
                    
                    }
                    
                    void ColTimeDelegate::paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
                    {
                      QTime text = index.model()->data(index, Qt::DisplayRole).toTime();
                      QStyleOptionViewItem myOption = option;
                      myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
                      drawDisplay(painter, myOption, myOption.rect, text.toString("hh:mm:ss"));
                      drawFocus(painter, myOption, myOption.rect);
                    }
                    
                    //My application code...
                    
                    QString query = QString("SELECT lastname, date, TIME(SUM(SEC_TO_TIME(time_work))) FROM works WHERE id_work = 1 GROUP BY(id_employee) ORDER BY id_work ASC");
                    
                    model = new QSqlQueryModel(this);
                    model->setQuery(query);
                    model->setHeaderData(0, Qt::Horizontal, tr("Employee"));
                    model->setHeaderData(1, Qt::Horizontal, tr("Date"));
                    model->setHeaderData(2, Qt::Horizontal, tr("Time"));   // Fail this field. The time is not shown on this field 
                    
                    // show
                    ui->view->setModel(model);
                    ui->view->resizeColumnsToContents();
                    ui->view->horizontalHeader()->setStretchLastSection(true);
                    ui->view->setItemDelegateForColumn(1, new ColDataDelegate(this));
                    ui->view->setItemDelegateForColumn(2, new ColTimeDelegate(this));
                    

                    blackout69

                    artwawA 1 Reply Last reply
                    0
                    • blackout69B blackout69

                      Hi,
                      for my application, I need to show the processing time on a QTableView.
                      This is the code that shows the data I extract from the query .

                      //My delegate...
                      
                      ColTimeDelegate::ColTimeDelegate(QObject *parent) : QItemDelegate(parent)
                      {
                      
                      }
                      
                      void ColTimeDelegate::paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
                      {
                        QTime text = index.model()->data(index, Qt::DisplayRole).toTime();
                        QStyleOptionViewItem myOption = option;
                        myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
                        drawDisplay(painter, myOption, myOption.rect, text.toString("hh:mm:ss"));
                        drawFocus(painter, myOption, myOption.rect);
                      }
                      
                      //My application code...
                      
                      QString query = QString("SELECT lastname, date, TIME(SUM(SEC_TO_TIME(time_work))) FROM works WHERE id_work = 1 GROUP BY(id_employee) ORDER BY id_work ASC");
                      
                      model = new QSqlQueryModel(this);
                      model->setQuery(query);
                      model->setHeaderData(0, Qt::Horizontal, tr("Employee"));
                      model->setHeaderData(1, Qt::Horizontal, tr("Date"));
                      model->setHeaderData(2, Qt::Horizontal, tr("Time"));   // Fail this field. The time is not shown on this field 
                      
                      // show
                      ui->view->setModel(model);
                      ui->view->resizeColumnsToContents();
                      ui->view->horizontalHeader()->setStretchLastSection(true);
                      ui->view->setItemDelegateForColumn(1, new ColDataDelegate(this));
                      ui->view->setItemDelegateForColumn(2, new ColTimeDelegate(this));
                      

                      blackout69

                      artwawA Offline
                      artwawA Offline
                      artwaw
                      wrote on last edited by
                      #9

                      @blackout69 I'd try to supply data as seconds and do the math in the delegate dropping QTime. The only thing to check is if this will be fast enough.

                      For more information please re-read.

                      Kind Regards,
                      Artur

                      1 Reply Last reply
                      3
                      • blackout69B Offline
                        blackout69B Offline
                        blackout69
                        wrote on last edited by
                        #10

                        Thanks for the suggestions.
                        I solved it like this:

                        // my query:
                        QString query = QString("SELECT lastname, date, SUM(time_work) FROM works WHERE id_work = 1 GROUP BY(id_employee) ORDER BY id_work ASC");
                        
                        // my custom delegate:
                        void ColCustomTimeDelegato::paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
                        {
                          int value = index.model()->data(index, Qt::DisplayRole).toInt();
                          QStyleOptionViewItem myOption = option;
                          myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
                          drawDisplay(painter, myOption, myOption.rect, QString("%1:%2:%3").arg(value/3600).arg((value%3600)/60).arg((value%3600)%60));
                          drawFocus(painter, myOption, myOption.rect);
                        }
                        

                        It works very well.
                        Thanks again.

                        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