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 determination using QTime
QtWS25 Last Chance

Time determination using QTime

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 643 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.
  • taku-sT Offline
    taku-sT Offline
    taku-s
    wrote on last edited by
    #1

    Hi all.

    I'm making an app that works only during the period.
    Currently, the following code is used to determine if it is within the period.
    However, this method does not work well when the end time (endH) exceeds 0:00.

    What can I do to solve it?

    isIntime(int startH, int startM, endH, endM){
        QTime startHM(startH, startM);
        QTime endHM(endH, endM);
    
        if(QTime::currentTime() >= startHMm && QTime::currentTime() < endHM){
            return true;
        }
        else{
            return false;
        }
    }
    

    Thank you.

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

      Hi,

      Shouldn't you rather use QDateTime ?

      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
      6
      • taku-sT taku-s

        Hi all.

        I'm making an app that works only during the period.
        Currently, the following code is used to determine if it is within the period.
        However, this method does not work well when the end time (endH) exceeds 0:00.

        What can I do to solve it?

        isIntime(int startH, int startM, endH, endM){
            QTime startHM(startH, startM);
            QTime endHM(endH, endM);
        
            if(QTime::currentTime() >= startHMm && QTime::currentTime() < endHM){
                return true;
            }
            else{
                return false;
            }
        }
        

        Thank you.

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #3

        @taku-s I would not use hours and minutes, but take the current unix timestamp QDateTime::currentDateTimeUtc().
        This should solve easely your issue.

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        2
        • taku-sT Offline
          taku-sT Offline
          taku-s
          wrote on last edited by
          #4

          Thank you, everyone.
          I think it was probably solved with the following code.
          Tonight I will see the result at 0: 5.

          isIntime(int startH, int startM,int endH, int endM){
              QTime startHM(startH, startM);
              QTime endHM(endH, endM);
              QDate today = QDate::currentDate();
              QDateTime startDHM(today, startHM);
              if(endHM < startHM){
                  today = today.addDays(1);
              }
              QDateTime eDHM(today, endHM);
              if(QDateTime::currentDateTime() >= startDHM && QDateTime::currentDateTime() < endDHM){
                  return true;
              }
              else{
                  return false;
              }
          }
          
          KroMignonK 1 Reply Last reply
          0
          • taku-sT taku-s

            Thank you, everyone.
            I think it was probably solved with the following code.
            Tonight I will see the result at 0: 5.

            isIntime(int startH, int startM,int endH, int endM){
                QTime startHM(startH, startM);
                QTime endHM(endH, endM);
                QDate today = QDate::currentDate();
                QDateTime startDHM(today, startHM);
                if(endHM < startHM){
                    today = today.addDays(1);
                }
                QDateTime eDHM(today, endHM);
                if(QDateTime::currentDateTime() >= startDHM && QDateTime::currentDateTime() < endDHM){
                    return true;
                }
                else{
                    return false;
                }
            }
            
            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #5

            @taku-s Oh, I apologize, I have read too fast your post and misunderstand your request!

            isIntime(int startH, int startM,int endH, int endM){
                QTime startHM(startH, startM);
                QTime endHM(endH, endM);
                QTime currHM(QTime::currentTime());
            
                if(startHM <= endHM)
                    return ((currHM >= startHM) && (currHM < endHM));
                return ((currHM >= startHM) || (currHM < endHM));
            }
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            1
            • taku-sT Offline
              taku-sT Offline
              taku-s
              wrote on last edited by
              #6

              Certainly, my code doesn't work
              Thank you

              KroMignonK 1 Reply Last reply
              0
              • taku-sT taku-s

                Certainly, my code doesn't work
                Thank you

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #7

                @taku-s Have you tested my proposition? There are conditions to ensure it can work:

                • startH and endH between 0 and 23 (inclusive)
                • startM and endM between 0 and 59 (inclusive)

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                0
                • taku-sT Offline
                  taku-sT Offline
                  taku-s
                  wrote on last edited by
                  #8

                  OK
                  I understard .

                  Thank you.

                  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