kwikness
kwikness

Reputation: 1445

Comparing dateutil.relativedelta

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?

Upvotes: 7

Views: 4043

Answers (1)

Wooble
Wooble

Reputation: 89917

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.

Upvotes: 11

Related Questions