From 332f6380503090480b0d5be8544243bec81b29db Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 15 Apr 2009 21:55:17 -0500 Subject: [PATCH] Improving fuzzy date --- src/toolbox.py | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/toolbox.py b/src/toolbox.py index 911555d..c629ed5 100644 --- a/src/toolbox.py +++ b/src/toolbox.py @@ -142,24 +142,32 @@ def to_fuzzy_date(targetDate, todaysDate = datetime.datetime.today()): """ delta = targetDate - todaysDate days = abs(delta.days) - directionBy1 = "Next" if 0 < delta.days else "Last" - directionByN = "Later" if 0 < delta.days else "Earlier" - directionByInf = "from now" if 0 < delta.days else "ago" - if 2*365 < days: + noDifference = datetime.timedelta() + isFuture = noDifference < delta + directionBy1 = "Next" if isFuture else "Last" + directionByN = "Later" if isFuture else "Earlier" + + yearDelta = abs(targetDate.year - todaysDate.year) + if 2 < yearDelta: + directionByInf = "from now" if isFuture else "ago" return "Forever %s" % directionByInf - elif 365 < days: + elif 1 < yearDelta: return "%s year" % directionBy1 - elif 2*30 < days: + + monthDelta = abs(targetDate.month - todaysDate.month) + if 2 < monthDelta: return "%s this year" % directionByN - elif 30 < days: + elif 1 < monthDelta: return "%s month" % directionBy1 - elif 14 < days: + + dayDelta = abs(targetDate.day - todaysDate.day) + if 14 < dayDelta: return "%s this month" % directionByN - elif 7 < days: - return "%s week" % directionBy1 - elif 2 < days: - return "%s this week" % directionByN - elif 1 < days: - return "%s day" % directionByN + elif 1 < dayDelta: + directionInWeek = "This" if isFuture else "Last" + days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] + return "%s %s" % (directionInWeek, days[targetDate.weekday()]) + elif 1 == dayDelta: + return "%s day" % directionBy1 else: return "Today" -- 1.7.9.5