enter = datetime.time(hour=1) # Example enter time
exit = datetime.time(hour=2) # Example start time
enter_delta = datetime.timedelta(hours=enter.hour, minutes=enter.minute, seconds=enter.second)
exit_delta = datetime.timedelta(hours=exit.hour, minutes=exit.minute, seconds=exit.second)
difference_delta = exit_delta - enter_delta
import time
from datetime import datetime
def calcTime(enter,exit):
format="%H:%M:%S"
#Parsing the time to str and taking only the hour,minute,second
#(without miliseconds)
enterStr = str(enter).split(".")[0]
exitStr = str(exit).split(".")[0]
#Creating enter and exit time objects from str in the format (H:M:S)
enterTime = datetime.strptime(enterStr, format)
exitTime = datetime.strptime(exitStr, format)
return exitTime - enterTime
enter = datetime.today().time()
#Sleeping for 5 seconds before initializing the exit variable
time.sleep(5)
exit = datetime.today().time()
duration = calcTime(enter,exit)
print(f"Duration is {duration} (Hours:Minutes:Seconds)")
#Output: Duration is 0:00:05 (Hours:Minutes:Seconds)