I've looked around here for a while trying all different kinds of accepted answers, but they are too irrelevant for my own needs and 90% require static typing of the current date which is something I don't want to do
Context
Quite simple.
Get the current time in Spain.
Have a variable already set up which is an agreed start time.
Have a variable already set up which is an agreed end time.
If the current time in Spain is between the start-end time, proceed with saving logs
Threads checked for solution to my problem
All useless, downvoted, or irrelevant due to static typing of datetime
Check if time is between two days
Compare if datetime.timedelta is between two values
Code so far
now_time_uk = datetime.datetime.now()
current_time_spain = now_time + datetime.timedelta(hours=1)
start = datetime.time(10, 50)
end = datetime.time(16, 00)
if current_time_spain >= start or current_time_spain <= end:
print('Start logging')
else:
print('No need to log')
The code above was extracted from a thread on S.O which was an accepted answer, when I ran this, it gives a TypeError
TypeError: can't compare datetime.datetime to datetime.time
Accepted Answer Code
now_time_uk = datetime.datetime.now()
print(now_time_uk)
current_time_spain = now_time_uk + datetime.timedelta(hours=1)
start = datetime.time(10, 50)
end = datetime.time(17, 00)
print(current_time_spain.time())
if current_time_spain.time() >= start and current_time_spain.time() <= end:
print('Start logging')
else:
print('No need to log')