Improving fuzzy date
authorEd Page <epage@Dulcinea.(none)>
Thu, 16 Apr 2009 02:55:17 +0000 (21:55 -0500)
committerEd Page <epage@Dulcinea.(none)>
Thu, 16 Apr 2009 02:55:17 +0000 (21:55 -0500)
src/toolbox.py

index 911555d..c629ed5 100644 (file)
@@ -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"