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
The timeago package will format a datetime object as a "X time ago" statement.
It might also be useful to be able to convert a timedelta, or time in seconds, to a natural language time string.
Cribbing StackOverflow, here's a simple converter from seconds to a natural time, but it might make more sense to base something around a timedelta object?
#https://stackoverflow.com/a/24542445/454773intervals= (
('weeks', 604800), # 60 * 60 * 24 * 7
('days', 86400), # 60 * 60 * 24
('hours', 3600), # 60 * 60
('minutes', 60),
('seconds', 1),
)
defdisplay_time(seconds, granularity=3, sep=',', andword='and'):
"""Take a time in seconds and return a sensible natural language interpretation of it."""defnl_join(l):
iflen(l)>2:
return', '.join(f'{l[:-1]}{andword}{str(l[-1])}')
eliflen(l)==2:
returnf' {andword} '.join(l)
returnl[0]
result= []
forname, countinintervals:
value=seconds//countifvalue:
seconds-=value*countifvalue==1:
name=name.rstrip('s')
result.append("{} {}".format(value, name))
returnnl_join(result[:granularity])
I'd like to see a feature like the following:
I would expect one to use the result to produce nice output for a time difference like:
It would be nice if it accepted a time formatter such that it could alternately emit:
Perhaps that inflection is too trivial for a library like inflect. Or perhaps there are nuances I haven't yet considered.
The text was updated successfully, but these errors were encountered: