Check if time is passed – Python

In this tutorial, you will learn how to check if the time is passed in Python.

First, we will get the future time in UTC. In order to get this, first we will get the current time in UTC.

Then we will add 24 hours in it.

After that, we will get time_struct object from it. This will be used to get the Unix timestamp.

Then we will the Unix timestamp using the time_struct object.

Same can be done for getting the current time in UTC but without adding 24 hours in it.

delta is the difference between future Unix timestamp and current one.

Then we will convert the seconds into minutes by dividing it to 60 since each minute has 60 seconds.

And again dividing that to 60 since each hour has 60 minutes. This will return the number of hours since the time is passed.

Finally, we will check if the delta is less than or equal to zero. If it is, it means that the time is passed. If not, it means that the time is not passed yet.

from datetime import datetime, timezone, timedelta
import time

future_time = datetime.now(timezone.utc)		# current datetime in UTC
future_time = future_time + timedelta(hours=24)	# current time + 24 hours
future_time = future_time.timetuple()			# instance of time.struct_time
future_time = time.mktime(future_time)			# Unix timestamp

current_time = datetime.now(timezone.utc)		# current datetime in UTC
current_time = current_time.timetuple()			# instance of time.struct_time
current_time = time.mktime(current_time)		# Unix timestamp

delta = future_time - current_time				# difference between future datetime and current datetime (in seconds)
delta = delta / 60 / 60							# convert seconds into hours

if delta <= 0:								   # check if the time is passed
	print("Time passed")
else:
	print("Time not passed")

You can check the real-world example of this in the URL shortener app we created in Python.