Search code examples
pythonpython-3.xtimescheduler

Unable to solve TypeError: unorderable types: time.struct_time() > float()


I want a function to run at specific date and time. I am using scheduler.enterabs(). It is giving me following error:

START: 1561263892.1316419
Traceback (most recent call last):
  File "timer.py", line 78, in <module>
    scheduler.run()
  File "/usr/lib/python3.5/sched.py", line 137, in run
    if time > now:
TypeError: unorderable types: time.struct_time() > float()
import sched,time
scheduler = sched.scheduler(time.time, time.sleep)
def print_event():
    print('inside print_event')
    print ('EVENT:', time.time())

print ('START:', time.time())
scheduler.enterabs(time.strptime("2019-06-23 09:55:00", "%Y-%m-%d %I:%M:%S"),1,print_event)

scheduler.run()

I want to run print_event() on specific date and time. Please help me to solve this issue. Thanks in advance :)


Solution

  • The timefunc given to sched.scheduler is time.time which works with simple float numbers as time only.

    This means the time given to scheduler.enterabs must be a float, too. strptime returns a struct_time tuple instead.

    Solution: Use time.mktime to convert the tuple to a float time.