Python - execute code at certain time

Soldato
Joined
22 Nov 2007
Posts
4,130
Hi All

I'm having trouble getting the following code to execute, it works fine with the set_time variable but with time_to_set, nothing happens. What am i doing wrong please?

Code:
import datetime
dt=datetime
#set_time=str(dt.time(14,19))[0:5]
time_to_set=datetime.datetime(year=2022, month=12, day=1, hour=14, minute=19, second=0, microsecond=0, tzinfo=None, fold=0)
timenow=dt.datetime.now().time()
time=False
while not time:
 timenow=str(dt.datetime.now().time())[0:5]
# print(timenow)
 if timenow==time_to_set:
    print("yeeehaaa")
    time=True
    break
else:
    print("naaaaa")
 
Associate
Joined
11 Dec 2016
Posts
2,042
Location
Oxford
need to convert `time_to_set` to a string
time_to_set = str(time_to_set)[0:5]

timenow is a str and time_to_set is a datetime object, so comparison is always false

also add a sleep(1) somewhere in the while loop, else it would just be spinning madly at full cpu
 
Last edited:
Soldato
OP
Joined
22 Nov 2007
Posts
4,130
need to convert `time_to_set` to a string
time_to_set = str(time_to_set)[0:5]

timenow is a str and time_to_set is a datetime object, so comparison is always false

also add a sleep(1) somewhere in the while loop, else it would just be spinning madly at full cpu
delete
 
Last edited:
Associate
Joined
11 Dec 2016
Posts
2,042
Location
Oxford
yes, and the [0:5] in the end
that takes a slice of 5 characters from start of string and so that the string you are comparing looks like "15:14"
 
Soldato
OP
Joined
22 Nov 2007
Posts
4,130
need to convert `time_to_set` to a string
time_to_set = str(time_to_set)[0:5]

timenow is a str and time_to_set is a datetime object, so comparison is always false

also add a sleep(1) somewhere in the while loop, else it would just be spinning madly at full cpu

Like this?

Python:
import time
import datetime
dt=datetime
time_to_set=str(datetime.datetime(year=2022, month=12, day=1, hour=15, minute=21, second=0, microsecond=0, tzinfo=None, fold=0))
time1=str(time_to_set)[0:5]
#set_tstr(time_to_set)[0:5]ime=str(dt.time(14,19))[0:5]
time=False
while not time:
 timenow=str(dt.datetime.now().time())[0:5]

# print(timenow)
 if timenow==time1:
    time.sleep(10)
    print("yeeehaaa")
    time=True
    break
else:
    print("naaaaa")
 
Associate
Joined
11 Dec 2016
Posts
2,042
Location
Oxford
this is how I would do it, note equality comparison is replaced with greater than
Python:
from datetime import datetime
from time import sleep

time_to_set=datetime(year=2022, month=12, day=1, hour=15, minute=31, second=0, microsecond=0, tzinfo=None, fold=0)

while True:
    timenow=datetime.now()
    if timenow>time_to_set:
        print("yeeehaaa")
        break
    else:
        print("naaaaa")
    sleep(1)
but keeping the string version and equality comparison, the trick is to compare the hour and minute part only

Python:
from datetime import datetime
from time import sleep

time_to_set=str(datetime(year=2022, month=12, day=1, hour=15, minute=35, second=0, microsecond=0, tzinfo=None, fold=0).time())[0:5]

while True:
    timenow=str(datetime.now().time())[0:5]
    if timenow==time_to_set:
        print("yeeehaaa")
        break
    else:
        print("naaaaa")
    sleep(1)
 
Soldato
OP
Joined
22 Nov 2007
Posts
4,130
this is how I would do it, note equality comparison is replaced with greater than
Python:
from datetime import datetime
from time import sleep

time_to_set=datetime(year=2022, month=12, day=1, hour=15, minute=31, second=0, microsecond=0, tzinfo=None, fold=0)

while True:
    timenow=datetime.now()
    if timenow>time_to_set:
        print("yeeehaaa")
        break
    else:
        print("naaaaa")
    sleep(1)
but keeping the string version and equality comparison, the trick is to compare the hour and minute part only

Python:
from datetime import datetime
from time import sleep

time_to_set=str(datetime(year=2022, month=12, day=1, hour=15, minute=35, second=0, microsecond=0, tzinfo=None, fold=0).time())[0:5]

while True:
    timenow=str(datetime.now().time())[0:5]
    if timenow==time_to_set:
        print("yeeehaaa")
        break
    else:
        print("naaaaa")
    sleep(1)
Thank you very much. I see where i was going wrong now comparing string to date object.
 
Soldato
OP
Joined
22 Nov 2007
Posts
4,130
this is how I would do it, note equality comparison is replaced with greater than
Python:
from datetime import datetime
from time import sleep

time_to_set=datetime(year=2022, month=12, day=1, hour=15, minute=31, second=0, microsecond=0, tzinfo=None, fold=0)

while True:
    timenow=datetime.now()
    if timenow>time_to_set:
        print("yeeehaaa")
        break
    else:
        print("naaaaa")
    sleep(1)
but keeping the string version and equality comparison, the trick is to compare the hour and minute part only

Python:
from datetime import datetime
from time import sleep

time_to_set=str(datetime(year=2022, month=12, day=1, hour=15, minute=35, second=0, microsecond=0, tzinfo=None, fold=0).time())[0:5]

while True:
    timenow=str(datetime.now().time())[0:5]
    if timenow==time_to_set:
        print("yeeehaaa")
        break
    else:
        print("naaaaa")
    sleep(1)

Hey, i've been playing around with both methods. I don't understand why you can't use == on the first method?

time_to_set returns
datetime.datetime(2022, 12, 1, 15, 31)

and datetime.now() returns

datetime.datetime(2022, 12, 4, 12, 44, 56, 806293)
In [ ]:

is it because datetime.now() incudes milliseconds so they will never be equal ? But using > will check to see if the hour/minute in the time.now() is greater than the time_to_set hour/minute?
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
is it because datetime.now() incudes milliseconds so they will never be equal ?

Yes. The datetime object includes properties for milliseconds and that can't just be ignored when comparing 2 dates. You may not explicitly set the values for milliseconds when constructing the date but they still exist so will get zero values.

To be clear: you can comparing dates without milliseconds by doing manual checks like the 'greater than' method already posted or something like (date1.year == date2.year AND date1.month == date2.month AND date1.day == date2.day) etc. You can't use date1 == date2 and expect them to be the same if they have different millisecond values.
 
Last edited:
Soldato
OP
Joined
22 Nov 2007
Posts
4,130
Yes. The datetime object includes properties for milliseconds and that can't just be ignored when comparing 2 dates. You may not explicitly set the values for milliseconds when constructing the date but they still exist so will get zero values.

To be clear: you can comparing dates without milliseconds by doing manual checks like the 'greater than' method already posted or something like (date1.year == date2.year AND date1.month == date2.month AND date1.day == date2.day) etc. You can't use date1 == date2 and expect them to be the same if they have different millisecond values.
Got it thank you!
 
Back
Top Bottom