-1
from twilio.rest import Client
from datetime import datetime

#                                 Twilio Account SID, Auth Token
client = Client("SID", "Token")

while True:
    if (datetime.time(datetime.now())) == '08:00:00.000000':
    client.messages.create(to="+Number", 
                           from_="+Number", 
                           body="Goodmorning")

why doesnt this execute every morning at 8? if i switch it to like a minute from now and let the loop run it doesnt work

philnash
  • 70,667
  • 10
  • 60
  • 88

3 Answers3

2

Your code has two wrong assumptions.

the type of datetime.datetime.time(datetime.datetime.now()) is datetime.time, not str and comparing datetime.time to str will always return False. you should compare datetime.datetime.time(datetime.datetime.now()) with datetime.time(8, 0).

the second assumption is that datetime.now will be executed at the exact time of 08:00:00.000000, this is possible but unlikely.

one way to fix the second assumption is to set a time window during which the if statement can be true.


from twilio.rest import Client
from datetime import datetime, timedelta, time, date

# Twilio Account SID, Auth Token
client = Client("SID", "Token")
# calculate the next time window
time_window_start = datetime.combine(date.today() + timedelta(days=1), time(8, 0))
time_window_end = time_window_start + timedelta(hours=1)

while True:
    # check if we are in the time window
    if time_window_start < datetime.now() and datetime.now() < time_window_end:
        client.messages.create(
            to="+Number", from_="+Number", body="Goodmorning")
        # calculate new time window for tomorrow
        time_window_start = datetime.combine(date.today() + timedelta(days=1), time(8, 0))
        time_window_end = time_window_start + timedelta(hours=1)

this code will wait until it is between 8 and 9, and then create a single message and wait until the next day. i see 3 issues still. the first is that this is busy waiting. you can fix this by regularly sleeping using time.sleep

busy-waiting or spinning is a technique in which a process repeatedly checks to see if a condition is true. ... In most cases spinning is considered an anti-pattern and should be avoided, as processor time that could be used to execute a different task is instead wasted on useless activity. Spinning can be a valid strategy in certain circumstances.

the second issue is that your code might miss the time window. i have set the window duration to 1 hour, so i don't think this will happen often. but there are several ways to mitigate this.

the third is that the window start and end is calculated at two different places. you could put this in a function so you Don't repeat yourself.

steviestickman
  • 1,132
  • 6
  • 17
1

With your code and even with maney's answer it will not work. Your function would have to randomly be called at excatly '08:00:00.000000' for this to work.

Have a look at the answers to this question.

0

The (datetime.time(datetime.now())) returns value like object datetime.time(12, 18, 39, 515846). you can use datetime.time(datetime.now()).isoformat() to get the time value in format 12:19:29.456461. Then you can use this to compare in your if loop.

Ex,

from twilio.rest import Client
from datetime import datetime

#                                 Twilio Account SID, Auth Token
client = Client("SID", "Token")

while True:
    if (datetime.time(datetime.now())).isoformat() == '08:00:00.000000':
    client.messages.create(to="+Number", 
                           from_="+Number", 
                           body="Goodmorning")

maney
  • 74
  • 5