Datetime comparison behavior in python
Question: Hi I am writing a program that is dependent on time and observing some curious
behavior with datetime objects I cannot quite figure out. The code I am
working with / am having trouble with is...

    
    
    now = datetime.now()
    time_changed_state = some datettime object previously initialized
    time_delay = some integer
    time_arrival = time_changed_state + timedelta(minutes=time_delay)
    
    if now < time_arrival:
       do something
    elif now >= time_arrival:
       do something different
    

I have been working with test cases in order ot make sure the code behaves the
way I would like it to but it doesn't seem to.

I discovered the odd behavior when the time_delay = 0, and I know for a fact
that now would be >= time_arrival since time_changed_state was a datetime
object initilizated before this function call and now was initialized within
the function. However, the "do something" code is being executed rather than
the "do something different code".

Thanks so much!


Answer: I've edited your code, is this what you are expecting?

    
    
    from datetime import datetime
    from datetime import timedelta
    
    now = datetime.now()
    time_changed_state = now - timedelta(hours=2)
    time_delay = 0
    time_arrival = time_changed_state + timedelta(minutes=time_delay)
    
    if now < time_arrival:
       print 'something'
    elif now >= time_arrival:
       print 'something different'
    

