1

I have this code where I execute a sql query and get two TIME fields. Now I want to compare these two with other time type field. My current code is below

import datetime

def comp(time1,time2):

        start_time_obj = datetime.datetime.strptime(time1, '%H:%M').time()
        end_time_obj = datetime.datetime.strptime(time2, '%H:%M').time()

        sql_select2 = "select TIME(Start_Time), TIME(End_Time) from table1"
        result2 = cur.execute(sql_select2)

        resultSet2 = cur.fetchall()
        for Start_Time,End_Time in resultSet2:
             if (Start_Time <= start_time_obj <= End_Time) or (Start_Time <= end_time_obj <= End_Time):
                    print("Great!")
                    continue

if __name__ == '__main__':
    comp("08:00","10:00")

When I execute this I get error as TypeError: '<=' not supported between instances of 'datetime.timedelta' and 'datetime.time'. How to remove this?

Note: this is the sample field structure in database - '2017-06-16 08:06:00' for both Start_Time and End_Time. What I want to do is compare the time part in the above field with the time coming as a parameter

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2916886
  • 847
  • 2
  • 16
  • 35
  • 1
    What's a typical value of `Start_Time` and `End_Time` (in Python, not what's in the DB)? They are obviously returning a delta time from some point, you need to first figure out what that point is before you can compare them. Printing out their values should give you a clue. – zwer Jun 07 '17 at 19:40
  • @zwer this is the sample field structure in database - '2017-06-16 08:06:00' for both Start_Time and End_Time. What I want to do is compare the time part in the above field with the time coming as a parameter – user2916886 Jun 07 '17 at 19:40
  • 1
    It's not important what is in the database, it's what your database driver (and we don't know which one) does with the field that matters - it obviously converts it to a `datetime.timedelta` but you need to figure out what's the starting point for getting that delta. It's most likely the epoch, but you won't be sure until you print it out from within Python (or find in the docs of your database driver what it does with `TIME` fields). – zwer Jun 07 '17 at 19:44
  • @zwer what could be the easiest way to compare the time in `DATETIME` value in database and a normal time value(like 08:00) in python code? – user2916886 Jun 07 '17 at 20:32
  • Until you know what type your database driver returns for the DATETIME, TIME or any other SQL field, you cannot know how to compare it. Check your database driver's documentation... – zwer Jun 07 '17 at 21:02

1 Answers1

0

What if you do

 start_time_obj = datetime.datetime.strptime(time1, '%H:%M').date()
 end_time_obj = datetime.datetime.strptime(time2, '%H:%M').date()

using .date() instead of .time()?

lauralindy
  • 23
  • 1
  • 5
  • `TypeError: '<=' not supported between instances of 'datetime.timedelta' and 'datetime.date'` – user2916886 Jun 07 '17 at 19:29
  • What exactly are the time objects being returned by your sql query? If they are the timedelta from some time (say midnight) then you need to take your start_time_obj/end_time_obj and subtract from the same default time. deltatime is the time in minutes/seconds of change between two times. while datetime is an actual time. – lauralindy Jun 07 '17 at 19:34
  • this is the sample field structure in database - `'2017-06-16 08:06:00'` for both `Start_Time` and `End_Time`. What I want to do is compare the time part in the above field with the time coming as a parameter – user2916886 Jun 07 '17 at 19:38
  • Take a look at the link below. Your start_time-obj and end_time_obj are datetime.time objects but whatever is being returned by your sql queries are datetime.timedelta objects. You have to convert the datetime.time to datetime.timedelta by subtracting from some default time. https://stackoverflow.com/questions/35241643/convert-datetime-time-into-datetime-timedelta-in-python-3-4 – lauralindy Jun 07 '17 at 19:44