You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Missing option to display years and months when years > 1 #108
>>> for year inrange(5):
... humanize.naturaltime(timedelta(days=(year+0.6) *365), months=True)
...
'7 months ago'
'1 year, 7 months ago'
'2 years ago'
'3 years ago'
'4 years ago'
>>> for year inrange(5):
... humanize.naturaltime(timedelta(days=(year+0.6) *365), months=False)
...
'219 days ago'
'1 year, 219 days ago'
'2 years ago'
'3 years ago'
'4 years ago'
So months=True doesn't control whether to show a more granular time, but what units to use.
Hi @hugovk! Thank you for the explanation. My use case is that we need to display the amortization of an investment. Currently it says "4.6 years" and I thought I could use humanize to turn it into something like "4 years, 7 months" because that's more human friendly. There's a way to do this using a hack:
In [12]: def naturaldelta(delta):
...: if delta < timedelta(days=2*365):
...: return humanize.naturaldelta(delta)
...: return f"{humanize.naturaldelta(delta)},{humanize.naturaldelta(timedelta(days=delta.days%365))}"
...:
In [13]: naturaldelta(timedelta(days=4.6*365))
Out[13]: '4 years,7 months'
In [14]: naturaldelta(timedelta(days=1.6*365))
Out[14]: '1 year, 7 months'
In [15]: naturaldelta(timedelta(days=0.6*365))
Out[15]: '7 months'
The problem with this is that it might break translations.
May be >= 10m, we can drop the seconds part without option.
Similarly for hours, output should be allowed to get configured between 3 hours vs 3h43m and then again, after may be >=10h, we drop the minutes part altogether.
This is a default output format in kubernetes client to show the age. It always shows 2 units (unless we are in seconds)
❯ kubectl get deployments -ANAMESPACE NAME READY UP-TO-DATE AVAILABLE AGEkube-system coredns 2/2 2 2 6d14hlocal-path-storage local-path-provisioner 1/1 1 1 6d14h
❯ kubectl get podNAME READY STATUS RESTARTS AGEtemp 1/1 Running 0 18s
❯ kubectl get podNAME READY STATUS RESTARTS AGEtemp 1/1 Running 0 105s
❯ kubectl get podNAME READY STATUS RESTARTS AGEtemp 1/1 Running 0 2m4s
My use case is that we need to display the amortization of an investment.
You already know that you would like to have the precision down to the months. Maybe you could use precisedelta for your use case?
>>>humanize.precisedelta(timedelta(days=4.6*365), minimum_unit="months")
'4 years and 7.18 months'>>>humanize.precisedelta(timedelta(days=4.6*365), minimum_unit="months", format="%d")
'4 years and 7 months'>>>humanize.precisedelta(timedelta(days=4*365), minimum_unit="months", format="%d")
'4 years'
Using minimum_units="months" and format="%d" will truncate any decimals for the months.
ℹ️ Using format="%.0f" would instead round to the nearest month.
>>>humanize.precisedelta(timedelta(days=4.65*365), minimum_unit="months")
'4 years and 7.77 months'>>>humanize.precisedelta(timedelta(days=4.65*365), minimum_unit="months", format="%d")
'4 years and 7 months'>>>humanize.precisedelta(timedelta(days=4.65*365), minimum_unit="months", format="%.0f")
'4 years and 8 months'
Activity
hugovk commentedon Apr 13, 2023
What it's doing is rounding to years, when years >= 2:
And similarly with
months=True, except the granularity is in days not months:And likewise for
naturaltime:So
months=Truedoesn't control whether to show a more granular time, but what units to use.adonig commentedon Apr 13, 2023
Hi @hugovk! Thank you for the explanation. My use case is that we need to display the amortization of an investment. Currently it says "4.6 years" and I thought I could use humanize to turn it into something like "4 years, 7 months" because that's more human friendly. There's a way to do this using a hack:
The problem with this is that it might break translations.
hugovk commentedon Apr 13, 2023
I guess one idea is to add a new option to make the cutoff configurable, and (I expect) use it to replace the 1 in
elif years == 1.That would also require a lot more handling in that block to deal with singular and plural years, especially for the translations.
dharapvj commentedon Mar 5, 2024
Would like to chime in here. I would like to see naturaldelta behavior to be..
should allow for either
3 minutesOR3m36s3 hoursvs3h43mand then again, after may be >=10h, we drop the minutes part altogether.This is a default output format in kubernetes client to show the age. It always shows 2 units (unless we are in seconds)
Logic for above calculation is here.
dangillet commentedon Nov 2, 2025
You already know that you would like to have the precision down to the
months. Maybe you could useprecisedeltafor your use case?Using
minimum_units="months"andformat="%d"will truncate any decimals for the months.ℹ️ Using
format="%.0f"would instead round to the nearest month.hugovk commentedon Sep 16, 2026
Let's close, use
precisedelta(minimum_unit="months")instead.