#33879: timesince - wrong results for 11 months + several weeks
--------------------------------------+------------------------------------
     Reporter:  אורי                  |                    Owner:  nobody
         Type:  Cleanup/optimization  |                   Status:  new
    Component:  Utilities             |                  Version:  dev
     Severity:  Normal                |               Resolution:
     Keywords:                        |             Triage Stage:  Accepted
    Has patch:  0                     |      Needs documentation:  0
  Needs tests:  0                     |  Patch needs improvement:  0
Easy pickings:  0                     |                    UI/UX:  0
--------------------------------------+------------------------------------

Comment (by אורי):

 Hi,

 I updated my code a little:

 {{{
 from dateutil.relativedelta import relativedelta

 from django.utils.timesince import TIME_STRINGS as timesince_time_strings
 from django.utils.html import avoid_wrapping
 from django.utils.translation import pgettext, get_language

 def timesince(d, now):
     """
     Like Django's timesince but more accurate. Returns results only when
 delta is at least one day (positive). Otherwise returns "". Result is
 either one or two in depth.
     """
     delta = -relativedelta(d, now)

     result = []
     if ((delta.years >= 0) and (delta.months >= 0) and (delta.days >= 0)):

         years = delta.years
         months = delta.months
         weeks = delta.days // 7
         days = delta.days - weeks * 7

         timesince_counts = [(years, "year"), (months, "month")]
         if (years == 0):
             timesince_counts.append((weeks, "week"))
             if (months == 0):
                 timesince_counts.append((days, "day"))

         for (count, name) in timesince_counts:
             if (count > 0):
 result.append(avoid_wrapping(value=timesince_time_strings[name] % {"num":
 count}))

     result = pgettext(context="timesince", message=", ").join(result)
     if (get_language() == "he"):
         result = re.sub(pattern=r'(\ {1}ו{1})(\d{1})', repl=lambda m:
 "-".join(m.groups()), string=result)
     return result
 }}}

 I also think you can consider using `dateutil.relativedelta` if `python-
 dateutil` is installed, and adding it as an optional dependency on your
 docs. And if not, revert to the current algorithm.

 Notice, I used `-relativedelta(d, now)` since it displays more accurate
 results for dates in the past, than `relativedelta(now, d)`. You can see
 [https://github.com/dateutil/dateutil/issues/1228]

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33879#comment:9>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018269cfed8c-dafc2b4e-5985-45fa-918f-28c3b3804601-000000%40eu-central-1.amazonses.com.

Reply via email to