#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 created my own utility function:

 {{{
 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 gettext, get_language

 def timesince(d, now):
     delta = relativedelta(now, d)

     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"))

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

 I don't need depth>2, I don't need hours and minutes and by definition my
 function returns "" if both dates are the same. now must be bigger than d
 or else I don't know what will happen... I think you just get ""
 otherwise.

 https://github.com/speedy-net/speedy-
 net/blob/master/speedy/core/base/utils.py

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33879#comment:8>
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701825f2d8ac8-2b7555a6-ec6f-4ffb-8ae1-6911eac1bd95-000000%40eu-central-1.amazonses.com.

Reply via email to