2

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

Python current time

Compare dates Python

Convert string into datetime

Compare date and datetime

Check if time is between two days

Convert string into datetime

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')
Danny Watson
  • 165
  • 5
  • 24

3 Answers3

2

Like the error says you are trying to compare datetime objects with time objects which isn't possible... luckily the datetime object has a builtin method for converting to a time object, and that is .time(). So you can replace:

if current_time_spain >= start or current_time_spain <= end:

With

if current_time_spain.time() >= start or current_time_spain.time() <= end:

And it should work. Another way of doing it is setting current_time_spain from the beginning as a time object or another option is to set the start and end times to be datetime objects instead of time

Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62
  • Will this adequately cover when the end time is on a different day? (Ex: Start time 10pm 06/05 / End time 3am 06/06) ? – Danny Watson Jun 05 '18 at 11:26
  • 1
    No it will not, but that's because your logic doesn't do that either, is 22:00 higher then 01:00 or lower? It has to be one or the other, it can't be both (or it will give you no results anyway). If you need to cross days like that I suggest you use the 3rd option I mentioned and work only with full `datetime` objects and not just `time` (That way you can compare `now` with "**tomorrow** at 2am") – Ofer Sadan Jun 05 '18 at 11:28
  • I'll add the datetime objects as an iteration down the line, the basic functionality works now thanks to converting to time object – Danny Watson Jun 05 '18 at 11:33
1

You're comparing datetime object with date & time with only time , as mentioned in error

datetime.datetime.now() this returns datetime whereas datetime.time(10, 50) returns time.

If you want to only compare time then why you don't simply compare hours and minutes from now()

Umair Mohammad
  • 4,489
  • 2
  • 20
  • 34
  • The agreed time may change depending on server activity, it may end up going overnight, so it would start at 11pm and finish at 3am the next day – Danny Watson Jun 05 '18 at 11:21
  • Ok then maybe you can make a datetime object with day, month, year from now() and then compare that ? In that case both will be datetime – Umair Mohammad Jun 05 '18 at 11:23
0

You can’t compare a date time within a time but you could continue to use a TimeDelta in order to create a start and end date to compare. With this solution your code could become:

now_time_uk = date time.datetime.now()
current_time_spain = now_time + datetime.timedelta(hours=1)
start_hours = 10
start_minutes = 50
end_hours = 16
end_minutes = 0
time_spain_start = current_time_spain + datetime.timedelta(hours=start_hours, minutes=start_minutes)
time_spain_end = current_time_spain + datetime.timedelta(hours=end_hours, minutes=end_minutes)

if current_time_spain >= time_spain_start or current_time_spain <= spain_time_end:
    print “Start logging”
else:
    print “No need to log”

In this way you can also add a variable for each parameter of the TimeDelta object in order to have different time for the end date even if the day is the next or whatever you want or need. We can imagine to have all variable days, hours, minutes, and seconds initialized to the 0 value, so you need to change only the value you need.

S. Galati
  • 58
  • 10