harungo
harungo

Reputation: 219

Program idle while comparing two dates in while statement

Don't know how to properly describe my problem, but when I compare two datetime objects in while statement the whole program stops working. I have a method work()

import time
import datetime
def work():
    now = None
    intr = 10.0
    d = datetime.datetime.utcnow()
    least_time = datetime.datetime.combine(datetime.datetime.today(), datetime.time(10, 10, 00))
    finish = datetime.datetime.combine(datetime.datetime.today(), datetime.time(10, 10, 20))
    if datetime.datetime.today().weekday() == 0:
        least_time = datetime.datetime.combine(datetime.datetime.today(), datetime.time(11,10,00))
        finish = datetime.datetime.combine(datetime.datetime.today(), datetime.time(11,10,20))
    while d <= finish:
        d = datetime.datetime.utcnow()
        if intr > 1 and d >= least_time:
            intr = 1
            print("Interval set to 1 sec")
        if now == None:
            now = time.time()
        if time.time() - now >= intr:  
            print("Work")
            print("_____")        
            now = None
    print("End")

And, if I call print() or something else before that method:

print("1")
print("2")
print("3")
work()

The program just idle and do nothing.

Upvotes: 2

Views: 38

Answers (1)

Mike M&#252;ller
Mike M&#252;ller

Reputation: 85512

What happens depends on your current time zone. The call to datetime.datetime.utcnow() gives a datetime in UTC, whereas datetime.datetime.today() gives you current datetime for your time zone (which you machine has):

Changing:

d = datetime.datetime.utcnow()

to:

d = datetime.datetime.now()

or to:

d = datetime.datetime.today()

would fix your problem.

Upvotes: 3

Related Questions