timesince 'depth' parameter

97 views
Skip to first unread message

Toby Such

unread,
May 22, 2020, 5:40:03 PM5/22/20
to Django developers (Contributions to Django itself)
Hi all!

I've been a little frustrated with the timesince filter that comes with default Django. By default it always comes with 2 parts e.g. 4 hours and 3 minutes ago but I would much rather have just the 4 hours. In my own projects I've simply created my own version of the timesince filter and accompanying utils function that powers it that just has the code which adds the second part removed. However I wonder why this isn't standard as I can't think of a single website that displays time since as multiple parts.

I'm proposing adding a 'depth' parameter to the timesince function which would allow the developer to set how many parts to render. At depth one it would be "3 weeks". 2 depth: "3 weeks 2 days" (default behaviour), 3 depth: "3 weeks, 2 days, 4 hours ago" etc.

Does anyone have any thoughts related to this?

אורי

unread,
May 22, 2020, 6:21:16 PM5/22/20
to to...@tobysuch.uk, Django developers (Contributions to Django itself)
Hi,

Actually I think it's a good idea. I don't think I ever used this feature with Django, but it's nice to have anyway.

Have you seen this question from 2011?

Also, other related questions from https://www.google.com/search?q=timesince+filter 

אורי

Toby Such

unread,
May 22, 2020, 10:02:02 PM5/22/20
to Django developers (Contributions to Django itself)
Looks like other people seemed to have the same idea as me for a little fix, seems to me like it should be part of core Django. And yes I do admit its a small thing but if it was actually useful some more people might use it?

Tobias Kunze

unread,
May 23, 2020, 5:04:25 AM5/23/20
to django-d...@googlegroups.com
On 20-05-22 19:02:02, Toby Such wrote:
>Looks like other people seemed to have the same idea as me for a little
>fix, seems to me like it should be part of core Django. And yes I do admit
>its a small thing but if it was actually useful some more people might use
>it?

Sounds good to me, especially since it's easy to keep the default behaviour
*and* provide the new flexible behaviour. I'd definitely be more willing to
use `timesince` instead of my cobbled up solutions if it had the behaviour you
suggest.

Tobias
signature.asc

Adam Johnson

unread,
May 23, 2020, 5:42:23 AM5/23/20
to django-d...@googlegroups.com
+1 

I belive I’ve copy-paste-modified timesince in a past project to implement “depth=1”

--
You received this message because you are subscribed to the Google Groups "Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/20200523090408.n4vaffayujbzspn4%40cordelia.localdomain.
--
Adam

Tom Forbes

unread,
May 23, 2020, 6:00:33 AM5/23/20
to django-d...@googlegroups.com
I like this idea. However while `depth=1` makes sense for shorter time periods where the difference between `4 hours` and `4 hours and 3 minutes ago` is not required to be displayed, when you get a value that is over a year ago it loses a lot of granularity. The difference between `1 year` and `1 year and 11 months` is much more important.

In this case you probably want `depth=2` (“1 year, 6 months ago”), but only for values over a year ago. Given that, maybe a simple depth flag isn’t enough? I think what we really want here is a `compact=True` flag.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.

Matthias Kestenholz

unread,
May 23, 2020, 6:13:21 AM5/23/20
to django-d...@googlegroups.com
The same could be said for "1 day, 23 hours" vs "1 day".

The way I implemented it in a former project was to only switch to days after 48 hours, and/or only switch to years after 24 months etc.

Best regards,
Matthias



--
Feinheit AG - Matthias Kestenholz - Direkt +41 79 444 78 96 - Zentrale +41 555 11 11 41

Mark Bailey

unread,
May 23, 2020, 6:52:09 AM5/23/20
to Django developers (Contributions to Django itself)
+1 There are lots of subtleties for how it can behave, and I have written my own when I want specific behaviour.  Other times I want something quick, and I've used timesince, and usually just put up with the fact that it's a bit longer than I want.  Having that control would make it a lot more useful.

Toby Such

unread,
May 23, 2020, 10:41:04 AM5/23/20
to Django developers (Contributions to Django itself)
I see your point but its tricky to know where you draw the line. Where do months stand? That answer will change depending on the project requirements. I wonder if there is a way of changing how the "time_strings" parameter works to control the depth more granularly but this does put a higher overhead on using the function, and certainly couldn't be used with the html filter.

I think adding the depth parameter increases the usefulness without to large of an overhead and that more specific requirements can carry on being implemented on a case by case basis.

Maybe the logic for calculating the time before formatting could be taken out of the function? That way it would be far easier to implement your own custom versions of the function without copying and pasting, and makes the code more readable too.
To unsubscribe from this group and stop receiving emails from it, send an email to django-d...@googlegroups.com.

Claude Paroz

unread,
May 23, 2020, 1:06:17 PM5/23/20
to Django developers (Contributions to Django itself)
Le samedi 23 mai 2020 16:41:04 UTC+2, Toby Such a écrit :
Maybe the logic for calculating the time before formatting could be taken out of the function? That way it would be far easier to implement your own custom versions of the function without copying and pasting, and makes the code more readable too.

I like that, make it easy to customize, in the same spirit as the Truncator class.
Feel free to ping me regarding the strings stuff and translatability, I worked a bit on that in the past.

Claude
Reply all
Reply to author
Forward
0 new messages