Karabah
Karabah

Reputation: 249

How to compare current time with time from database?

There is a field time in my MySQL database with the value: 08:05:00.

I need to compare this value with the current time. I attempted to do this:

now = datetime.now(pytz.timezone('Europe/Kiev'))
current_minute = now.minute
current_hour = now.hour

parse_time = split('08:05:00')

if int(current_hour) == parse_time[0] and (int(parse_time[1]) == int(current_minute)):
    print "Current hour and minute are the same with value in db"

I am not sure in this. Is it correct? I mean this expression:

int(current_hour) == parse_time[0]

Upvotes: 0

Views: 318

Answers (1)

Barmar
Barmar

Reputation: 782064

You're using int() on the wrong variables. current_minute and current_hour are already integers, while parse_time contains strings, which need to be converted to integers. So it should be:

if current_hour = int(parse_time[0]) and current_minute = int(parse_time[1]):

However, when you're doing your database query, you could use

SELECT HOUR(time) AS hour, MINUTE(time) AS minute, ...

to return these as integers, then you don't need to split it or convert them in Python.

Upvotes: 1

Related Questions