If Statement with QTime
-
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 doesntThank you
-
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 doesntThank you
-
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)) -
@VJ01
SinceFinal_Day_timeis always less thanFinal_Night_timeyourif(Final > Final_Night_time && Final < Final_Day_time)("NIGHT TIME") will always be false. You probably want some 24 hour "wrap around".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
-
V VJ01 has marked this topic as solved on
-
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
@VJ01 said in If Statement with QTime:
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 -
@VJ01 said in If Statement with QTime:
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@Kent-Dorfman You really like your parentheses, don't you? :)
-
@Kent-Dorfman You really like your parentheses, don't you? :)
-
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!!!
-
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!!!
@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-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?@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.
-
@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?@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? :-)
-
@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? :-)