Created
January 8, 2022 15:13
-
-
Save blzzua/292fcdd0c214e2ed766253db46d47789 to your computer and use it in GitHub Desktop.
estimate_timedelta.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def estimate_timedelta(td): | |
"""returns estimate timedelta in format 'YY year MM mon DD day'. consider plural form or absence y/m/d""" | |
if td.days > 0: | |
y, rem = divmod(td.days, 365) | |
m, d = divmod(rem, 31) | |
dict_ = {'year': y, 'mon': m, 'day':d } | |
return " ".join([f"{dict_[k]} {k}" + ("s" if dict_[k] > 1 else "") for k in dict_ if dict_[k] > 0 ]) | |
else: | |
return str(td) | |
# estimate_timedelta(timedelta(days=2*365, seconds=100000)) | |
# '2 years 1 day' | |
# estimate_timedelta(timedelta(days=23000)) | |
# '63 years 5 days' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment