time.py 676 B

123456789101112131415161718
  1. import datetime
  2. import threading
  3. def set_time(func, args, time):
  4. now_time = datetime.datetime.now()
  5. # 获取明天时间
  6. next_time = now_time + datetime.timedelta(days=+1)
  7. next_year = next_time.date().year
  8. next_month = next_time.date().month
  9. next_day = next_time.date().day
  10. # 获取明天七点的时间
  11. next_time = datetime.datetime.strptime(str(next_year) + "-" + str(next_month) + "-" + str(next_day) + time,
  12. "%Y-%m-%d %H:%M:%S")
  13. timer_start_time = (next_time - now_time).total_seconds()
  14. print(timer_start_time)
  15. timer = threading.Timer(timer_start_time, func, args)
  16. timer.start()