1

I need some help with my code. I want to check if the variable start_time is less than, equal or greater than the current_time to compare for the time.

Here is the code:

start_date = str(stop_date[0])
stop_date = str(stop_date[1])
get_current_time = datetime.datetime.now().strftime('%H:%M')
get_start_time = time.strptime(start_date, '%Y%m%d%H%M%S')
start_time = time.strftime('%H:%M', get_start_time)
get_stop_time = time.strptime(stop_date, '%Y%m%d%H%M%S')
stop_time = time.strftime('%H:%M', get_stop_time)
current_time = str(get_current_time)

if start_time <> current_time <> stop_time:
   print "program is half way"

Here is the output for the start_time:

19:00
19:00
19:00
19:00
19:00
19:00
19:00

Here is the output for the current_time:

00:10:36 T:5304  NOTICE: 00:09

Here is the output for the stop_time:

00:09:33 T:6824  NOTICE: 19:30
00:09:33 T:6824  NOTICE: 20:00
00:09:33 T:6824  NOTICE: 19:30
00:09:33 T:6824  NOTICE: 20:00
00:09:33 T:6824  NOTICE: 20:00
00:09:33 T:6824  NOTICE: 19:30
00:09:33 T:6824  NOTICE: 20:00

When I try this:

if start_time <=> current_time < stop_time:
   print "program has finished"

It will give me an error: invalid syntax

It won't let me to have the = in the statement, I can only write on the statement with less than or greater than but not with the =.

How I can include the equal = with less than and greater than for the start_time to compare the time with current_time?

EDIT: Opps, sorry my mistake. I pasted the wrong code so here is what I am trying to do:

if start_time <=> current_time < stop_time:
    print "program is half way"
13
  • 1
    Well, there you go. docs.python.org/2/library/stdtypes.html#comparisons Commented Jan 7, 2016 at 1:21
  • 1
    Um.. wait: you want to check if it's less than or greater than or equal to? Won't this be always true no matter what? Commented Jan 7, 2016 at 1:22
  • @wil93 yeah that is what i am trying to do. So what I should use then? Commented Jan 7, 2016 at 1:23
  • 1
    What? Are you sure that's what you want to do? The result is always true, as I said, so you would not need to even check it. Commented Jan 7, 2016 at 1:24
  • 1
    @astrosyam yes that is correct Commented Jan 7, 2016 at 1:34

1 Answer 1

1

From what I understand, you want to check that both of these hold:

  1. current_time is either less than, greater than, or equal to start_time.
  2. current_time is less than stop_time.

Now, the first one can be written like this:

if current_time < start_time or current_time > start_time or current_time == start_time:
   print "program has finished"

However, this is always true because start_time and current_time will either be equal (making current_time == start_time true) or different (making current_time < start_time or current_time > start_time true).

The second one can be written like this:

if current_time < stop_time:
   print "program has finished"

So, since the first one is not needed, you just need:

if current_time < stop_time:
   print "program has finished"
Sign up to request clarification or add additional context in comments.

8 Comments

ooops sorry i pasted the wrong code in my question. Can you please change your code as I want to check start_time with current_time to see if it is less than, equal or greater than and also check with stop_time to see if it is less than current_time? please see in my update question
The only difference is that now you want stop_time to be less than current_time (not the other way around), am I correct?
In the updated question you still say that current_time should be less than stop_time, so the present answer should be still correct.
yes that is correct. I tried to use the code on the update answer but it say the program are half way through which it is not. Any idea?
@David you really didn't understand the central part of wils93's answer: if start_time < current_time or current_time > start_time or current_time == start_time <-- that check is nonsensical because it will always be true.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.