Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. If Statement with QTime
Qt 6.12 LTS is out! See what's new in the release blog

If Statement with QTime

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 2.1k Views 1 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.
  • V VJ01

    Hi

    i am hoping someone can help me as im not sure if what im doing is correct, i have two times inputted by the user and i also have the current time.
    will post my code blow:

     QDateTime dateTime;
        dateTime = dateTime.currentDateTime();
        QString current_time = dateTime.time().toString("HH:mm");
        QTime Final = QTime::fromString(current_time,"HH:mm");
    
        QString Day_time = "07:00";
        QString Night_time = "14:00";
    
        QTime Final_Day_time = QTime::fromString(Day_time, "HH:mm");
        QTime Final_Night_time = QTime::fromString(Night_time, "HH:mm");
    
        qDebug() << "CURRENT TIME : " << Final;
        qDebug() << "DAY TIME : " << Final_Day_time;
        qDebug() << "NIGHT TIME : " << Final_Night_time;
    
        if(Final > Final_Night_time && Final < Final_Day_time){
            qDebug() << "NIGHT TIME";
        }
    
        if(Final < Final_Night_time && Final > Final_Day_time){
            qDebug() << "DAY TIME";
        }
    

    As you can see I have a Daytime a Night time (inputted by the user) and a current time. What I am trying to achieve is if the current time is between 07:00 and 14:00 do the day time sequence however when the current time is after 14:00, do the night time sequence and not switch back to the daytime sequence until its back to 07:00
    i hope that makes sense and sorry if it doesnt

    Thank you

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

    @VJ01
    Since Final_Day_time is always less than Final_Night_time your if(Final > Final_Night_time && Final < Final_Day_time) ("NIGHT TIME") will always be false. You probably want some 24 hour "wrap around".

    V 1 Reply Last reply
    1
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #3

      in additiona to what was stated, always parethesize your expressions so the intent is both obvious and you don't need to worry if you forget order of operations.

      (z < v && v > a) should be written as ((z < v) && (v > a))
      

      The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

      1 Reply Last reply
      1
      • JonBJ JonB

        @VJ01
        Since Final_Day_time is always less than Final_Night_time your if(Final > Final_Night_time && Final < Final_Day_time) ("NIGHT TIME") will always be false. You probably want some 24 hour "wrap around".

        V Offline
        V Offline
        VJ01
        wrote on last edited by VJ01
        #4

        @JonB

        Hi Thank you for the reply honestly instead of two if ststements i just did one with an else and that seemed to work. i was making it more complicated then it should have been haha

        if(Final <Final_Night_time && Final > Final_Day_time){
                qDebug() << "DAY TIME";
         }else{
               qDebug() << "NIGHT TIME";
        }
        

        Thank you

        Kent-DorfmanK 1 Reply Last reply
        1
        • V VJ01 has marked this topic as solved on
        • V VJ01

          @JonB

          Hi Thank you for the reply honestly instead of two if ststements i just did one with an else and that seemed to work. i was making it more complicated then it should have been haha

          if(Final <Final_Night_time && Final > Final_Day_time){
                  qDebug() << "DAY TIME";
           }else{
                 qDebug() << "NIGHT TIME";
          }
          

          Thank you

          Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #5

          @VJ01 said in If Statement with QTime:

          @JonB

          Hi Thank you for the reply honestly instead of two if ststements i just did one with an else and that seemed to work. i was making it more complicated then it should have been haha

          if(Final <Final_Night_time && Final > Final_Day_time){
                  qDebug() << "DAY TIME";
           }else{
                 qDebug() << "NIGHT TIME";
          }
          

          Thank you

          or in a more sophisticated manner

          qdebug() << (((Final < Final_Night_time) && (Final > Final_Day_time)) ? "DAY" : "NIGHT");
          // proper parenthesis use and ternary if() clause
          

          The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

          JonBJ 1 Reply Last reply
          0
          • Kent-DorfmanK Kent-Dorfman

            @VJ01 said in If Statement with QTime:

            @JonB

            Hi Thank you for the reply honestly instead of two if ststements i just did one with an else and that seemed to work. i was making it more complicated then it should have been haha

            if(Final <Final_Night_time && Final > Final_Day_time){
                    qDebug() << "DAY TIME";
             }else{
                   qDebug() << "NIGHT TIME";
            }
            

            Thank you

            or in a more sophisticated manner

            qdebug() << (((Final < Final_Night_time) && (Final > Final_Day_time)) ? "DAY" : "NIGHT");
            // proper parenthesis use and ternary if() clause
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #6

            @Kent-Dorfman You really like your parentheses, don't you? :)

            jsulmJ 1 Reply Last reply
            0
            • JonBJ JonB

              @Kent-Dorfman You really like your parentheses, don't you? :)

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @JonB Did you ever see Lisp code? :-)

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              JonBJ 1 Reply Last reply
              0
              • jsulmJ jsulm

                @JonB Did you ever see Lisp code? :-)

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

                @jsulm They are compulsory there, I (mostly) only put in the mandatory ones in C++ and omit the rest.

                1 Reply Last reply
                0
                • Kent-DorfmanK Offline
                  Kent-DorfmanK Offline
                  Kent-Dorfman
                  wrote on last edited by Kent-Dorfman
                  #9

                  You parenthesis free folks wouldn't like a MISRA/AUTOSAR audit. LOL!

                  https://www.autosar.org/fileadmin/standards/adaptive/18-03/AUTOSAR_RS_CPP14Guidelines.pdf

                  If/else clauses really stress my OCD when they represent binary code paths...and worse when there are more than three of them...It's like Dude! use a switch/case or a map of function pointers. PLLLEEEEAAASSSEEE!!!

                  The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

                  JonBJ 1 Reply Last reply
                  0
                  • Kent-DorfmanK Kent-Dorfman

                    You parenthesis free folks wouldn't like a MISRA/AUTOSAR audit. LOL!

                    https://www.autosar.org/fileadmin/standards/adaptive/18-03/AUTOSAR_RS_CPP14Guidelines.pdf

                    If/else clauses really stress my OCD when they represent binary code paths...and worse when there are more than three of them...It's like Dude! use a switch/case or a map of function pointers. PLLLEEEEAAASSSEEE!!!

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

                    @Kent-Dorfman
                    That's nearly 500 pages of rules to observer/remember! Very worthy, I can see it being advisable in critical stuff, thankfully I have not had to adhere. TBH if you need to adhere to this many rules it does make me wonder whether you need a different language which enforces more of this stuff?

                    Kent-DorfmanK jsulmJ 2 Replies Last reply
                    0
                    • JonBJ JonB

                      @Kent-Dorfman
                      That's nearly 500 pages of rules to observer/remember! Very worthy, I can see it being advisable in critical stuff, thankfully I have not had to adhere. TBH if you need to adhere to this many rules it does make me wonder whether you need a different language which enforces more of this stuff?

                      Kent-DorfmanK Offline
                      Kent-DorfmanK Offline
                      Kent-Dorfman
                      wrote on last edited by
                      #11

                      @JonB Generally very few institutions take it in its entirety, but at the beginning of a project decide what rules to enforce, which to ignore, and a policy for warning suppression (be prepared to defend why you ignored rule XXX in code review)

                      It's primary domain has been in the automobile automation industry, which as you can guess, needs to be pretty safety critical...but it's finding its way into other industries as well. In space systems we use it as a base, obviously.

                      Any virtually no one tries to adhere without a code scanner program to check for violations.

                      The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

                      1 Reply Last reply
                      2
                      • JonBJ JonB

                        @Kent-Dorfman
                        That's nearly 500 pages of rules to observer/remember! Very worthy, I can see it being advisable in critical stuff, thankfully I have not had to adhere. TBH if you need to adhere to this many rules it does make me wonder whether you need a different language which enforces more of this stuff?

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by jsulm
                        #12

                        @JonB said in If Statement with QTime:

                        it does make me wonder whether you need a different language which enforces more of this stuff

                        Indeed. Instead of reading these 500 pages and try to remember later all these restrictions/rules it would be better to use a programming language which reduces the need to remember such rules. Do you remember the "Port Qt to Rust" thread? :-)

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        JonBJ 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @JonB said in If Statement with QTime:

                          it does make me wonder whether you need a different language which enforces more of this stuff

                          Indeed. Instead of reading these 500 pages and try to remember later all these restrictions/rules it would be better to use a programming language which reduces the need to remember such rules. Do you remember the "Port Qt to Rust" thread? :-)

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

                          @jsulm said in If Statement with QTime:

                          Do you remember the "Port Qt to Rust" thread? :-)

                          That was what I had in mind.... Maybe that would enforce half the C++ rules, without needing to impose an equal number of new ones for Rust?

                          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
                          • Unsolved