简体   繁体   中英

compare set time with current time python

I have a @interval check that can run def timecheck(): every 10 second and I need to compare current time/weekday with a set time/weekday.

 @interval(10)
 def timecheck():
 <<< compare current time and weekday with time day ie 09:45  Mon,Tues,Wed,Thur,Fri>>>
 <<<if true then run premkt>>

  def premkt(bot):
  bot.msg('#optiontrader',"((( US MARKET OPEN IN 15 Minutes )))")

You could use the builtin datetime module to do this.

Note that datetime.weekday returns an integer, where Monday is 0 and Sunday is 6.

You might want to add a check somewhere that premkt isn't called multiple times in the same minute.

import datetime
now = datetime.datetime.now()

if now.weekday() < 5 and now.hour == 9 and now.minute == 45:
    # it's 9:45 on a weekday, do stuff
    premkt(bot)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM