Fixed #31623 -- Allowed specifying number of adjacent time units in timesince()/timeuntil().#13145
Conversation
|
PR ready to review. |
|
Hi @timpark0807 Thanks for this. 👍 My notes:
>>> d = datetime.datetime(year=2018, month=3, day=22)
>>> timesince(d, depth=4)
'2\xa0years, 3\xa0months, 2\xa0weeks, 2\xa0days'
>>> d = datetime.datetime(year=2018, month=3, day=2)
>>> timesince(d, depth=4)
'2\xa0years, 4\xa0months' |
@smithdc1 Thanks for taking the time to look through this PR. My responses are below:
Screenshot using http://www.mathcats.com/explore/elapsedtime.html As a side note, I was able to implement a timesince function that would be able to skip through missing adjacent units (The example above would return |
The current function only returns a depth of two, so stopping if there isn't an adjacent time depth gives the answer I would expect. (example below) When depth is increased to three or above there is a chance one of the depths is "0" but there are values at more granular time depths. Chopping at the first "0" I find surprising (just my opinion, I'm sure there are others!). For completeness I think the example you posted is what would expect. 👍 >>> d = datetime(year=2018, month=6, day=2)
>>> timesince(d)
'2\xa0years, 1\xa0month'
>>> d = datetime(year=2018, month=6, day=20)
>>> timesince(d)
'2\xa0years' |
I 100% agree with your opinion. However, I decided against changing the behavior within this PR because the original ticket specifically states Let's get the community opinion to see if we should proceed with a modified ticket to not chop at the first "0". |
e7c4571 to
ecdf522
Compare
1ee143e to
1f1116f
Compare
|
@felixxm Thanks for the thorough review. I got a bit carried away with my first PR. I'll keep these comments in mind for future commits. Let me know if you see anything else 👍 |
|
@claudep You mentioned in the ticket and on the mailing list that the most important thing is to make it easily customizable, however I don't see how this PR solves this. I'm fine with limiting this PR to the |
|
I don't remember if I had a specific idea in mind sorry. I guess a refactor can always be done later. |
|
@timpark0807 Thanks 👍 I added check for a |
carltongibson
left a comment
There was a problem hiding this comment.
OK, yes, looks tidy. Thank you @timpark0807, and everyone else for reviews.
…imesince()/timeuntil().
|
can you help me tp develop how old am i script similar to this? |

Overview
Ticket: https://code.djangoproject.com/ticket/31623
The
timesincefunction returns the time between two datetime objects as a formatted string.The previous implementation always returned two units of time. This update improves flexibility by allowing the user to specify the number of units to be returned. To preserve existing functionality, the default depth value is set to 2.
Example
Update includes:
Let me know if you see anything.
Thanks!