Brian
Brian

Reputation: 5

Changing a variable based on current time of day

I am trying to run a python script every sleep_time, but I want this sleep_time variable to change based on the current time of day. The program is used with analyzing stock data so during the day (8:30am-5:30pm) I want the program to update its data every 60 seconds. When it is not between this time, I only need it to update every 10 minutes. The current code I have is as follows:

sleep_time = 60
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%H:%M')

if (st >= '8:30' and st <='17:30'):
    sleep_time = 60

if(st < '8:30' and st >'17:30'):
    sleep_time = 600


while True: 
#runs this code every sleep_time seconds to constantly update
    ...
    time.sleep(sleep_time)

But I can t get it to work because st is a string and I am not sure how to compare a string like this. Any help would be appreciated

Upvotes: 0

Views: 2425

Answers (2)

CristiFati
CristiFati

Reputation: 41116

On one hand you have datetime.datetime objects, on the other hand you have strings. The 2 are not comparable, so one has to be converted into the other (it's possible both ways), in order to be able to compare them.

The key point is that when comparing, the result should be compatible to what you're comparing to.

Check [Python 3]: strftime() and strptime() Behavior for rules regarding conversion.

You decided to convert everything to string (this is the most straightforward way especially for less experienced ones (it works, and it's perfectly fine), even though I'd suggest the other way around). In Python (actually I'm not aware of a programming language that handles things differently), strings are compared lexicographically ([Wikipedia]: Lexicographical order), or alphabetically.

Let's take a look at the conversion:

>>> datetime.datetime.fromtimestamp(time.time()).strftime('%H:%M')
'04:29'

As seen, numbers (representing hours, minutes) that are less than 10, are left padded with a 0 (0 will be 00), and your strings should follow the same rule.

So, modifying your '8:30' string to '08:30' should do the trick, because for example '11:26' is:

  • Less than '8:30' (as '1' < '8') - which is obviously wrong
  • Greater than '08:30' (as '1' > '0') - which is correct

Upvotes: 0

user10356004
user10356004

Reputation:

Python has a datetime module, which provides a datetime data type.

You can compare between two, even three datetimes.

datetime.now() gives you the timestamp of current local date and time, and datetime.today() also gives you the timestamp of current local date and time. You can replace specific values of a datetime data type with datetime.replace().

from datetime import datetime


while True:

    #You should define sleep_time inside the loop. Otherwise it won't change for the rest of the loop.
    if datetime.today().replace(hour=8, minute=0, second=0, microsecond=0) <= datetime.now() <= datetime.today().replace(hour=15, minute=30, second=0, microsecond=0): 
        sleep_time = 60
    else: 
        sleep_time = 600

    #your code here
    time.sleep(sleep_time)

You would probably wonder what's the difference between datetime.now() and datetime.today() because they produces the same result.

According to the documentation, datetime.now() is more accurate than datetime.today() and can take a tz arg for timezones.

Upvotes: 2

Related Questions