Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing dateutil.relativedelta

Tags:

python

I'm trying to do a '>' comparison between two relativedeltas:

if(relativedelta(current_date, last_activity_date) > relativedelta(minutes=15)):

Here is the output from the debugger window in Eclipse:

debug window

One of the relativedeltas is only 15 minutes-- far smaller than the other one. Why does this comparison return false and not true as expected? What would be a better solution?

like image 610
kwikness Avatar asked Jul 28 '12 20:07

kwikness


1 Answers

dateutil.relativedelta doesn't implement __cmp__ sensibly, so instances can't be compared. There's an open bug on the issue; the argument that it doesn't make sense to say whether 29 days or 1 month is greater, and that therefore the whole thing falls back on python's default comparisons seems a bit flimsy to me, but that's just an opinion.

Depending on what you're actually doing, using datetime.timedelta may be a better solution.

like image 75
Wooble Avatar answered Sep 19 '22 10:09

Wooble