Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

compare times

Tue May 05, 2015 3:00 pm

I like to compare to time strings to check if the time between is more then 5 minutes.

I use the next code:

Code: Select all

from datetime import datetime
s1 = '21:07'
s2 = '21:16'
delta = datetime.strptime(s2, '%H:%M') - datetime.strptime(s1, '%H:%M')
if (delta.minutes  > 5):
  print ">  5 minuten"
but receive the next errors:

Traceback (most recent call last):
File "thermostaat.py", line 435, in <module>
if (delta.minutes > 5):
AttributeError: 'datetime.timedelta' object has no attribute 'minutes'

What am I doing wrong.?
What is the good way to check?
Please give a good example
Help is apriciated
Last edited by Canedje on Tue May 05, 2015 3:16 pm, edited 1 time in total.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: compare times

Tue May 05, 2015 3:15 pm

I think timedelta objects only store days, seconds and microseconds.

So:

Code: Select all

from datetime import datetime
s1 = '21:07'
s2 = '21:16'
delta = datetime.strptime(s2, '%H:%M') - datetime.strptime(s1, '%H:%M')
if (delta.total_seconds  > 300):
  print ">  5 minuten"
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: compare times

Tue May 05, 2015 3:22 pm

elParaguayo wrote:I think timedelta objects only store days, seconds and microseconds.

So:

Code: Select all

from datetime import datetime
s1 = '21:07'
s2 = '21:16'
delta = datetime.strptime(s2, '%H:%M') - datetime.strptime(s1, '%H:%M')
if (delta.total_seconds  > 300):
  print ">  5 minuten"

Thanks for the swift answer.
No error indeed.
But 300 is not 5 minutes it Always give true now.
How to calculate 5 minutes in microseconds?

EDIT:
I found the solution. The correct code has to be without "total":

Code: Select all

from datetime import datetime
s1 = '21:07'
s2 = '21:13'
delta = datetime.strptime(s2, '%H:%M') - datetime.strptime(s1, '%H:%M')
print delta.seconds
if (delta.seconds  > 300):
  print ">  5 minuten"

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: compare times

Tue May 05, 2015 3:32 pm

Can you give an example where the 5 minutes is not working? Your example code is an interval of 9 minutes.

May be my fault. I think "total_seconds" is a method so would need to be terminated with "()" or you can just use "delta.seconds"

EDIT: Ah - looks like you've found the issue already and it was my fault as I said!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: compare times

Tue May 05, 2015 4:09 pm

elParaguayo wrote:Can you give an example where the 5 minutes is not working? Your example code is an interval of 9 minutes.

May be my fault. I think "total_seconds" is a method so would need to be terminated with "()" or you can just use "delta.seconds"

EDIT: Ah - looks like you've found the issue already and it was my fault as I said!
Thanks anyway.
You gave me the right direction :D

Return to “Python”