def to_fuzzy_date(targetDate, todaysDate = datetime.datetime.today()):
"""
Conert a date/time/datetime object to a fuzzy date
+
+ >>> todaysDate = datetime.date(2009, 4, 16)
+ >>> to_fuzzy_date(datetime.date(1, 4, 6), todaysDate)
+ 'Forever ago'
+ >>> to_fuzzy_date(datetime.date(2008, 4, 13), todaysDate)
+ 'Last year'
+ >>> to_fuzzy_date(datetime.date(2009, 4, 13), todaysDate)
+ 'Last Monday'
+ >>> to_fuzzy_date(datetime.date(2009, 4, 20), todaysDate)
+ 'This Monday'
+ >>> to_fuzzy_date(datetime.date(2010, 4, 13), todaysDate)
+ 'Next year'
+ >>> to_fuzzy_date(datetime.date(2012, 12, 12), todaysDate)
+ 'Forever from now'
"""
delta = targetDate - todaysDate
days = abs(delta.days)
directionByN = "Later" if isFuture else "Earlier"
yearDelta = abs(targetDate.year - todaysDate.year)
- if 2 < yearDelta:
+ if 1 < yearDelta:
directionByInf = "from now" if isFuture else "ago"
return "Forever %s" % directionByInf
- elif 1 < yearDelta:
+ elif 1 == yearDelta:
return "%s year" % directionBy1
monthDelta = abs(targetDate.month - todaysDate.month)
- if 2 < monthDelta:
+ if 1 < monthDelta:
return "%s this year" % directionByN
- elif 1 < monthDelta:
+ elif 1 == monthDelta:
return "%s month" % directionBy1
dayDelta = abs(targetDate.day - todaysDate.day)