unterstützt verschiedene Sprachen durch Übergabe des Parameters li
, eine Liste mit dem entsprechenden Zeitstempel.
from datetime import datetime
from dateutil import parser
t1 = parser.parse("Tue May 26 15:14:45 2021")
t2 = parser.parse("Tue May 26 15:9:45 2021")
5min
t3 = parser.parse("Tue May 26 11:14:45 2021")
4h
t4 = parser.parse("Tue May 26 11:9:45 2021")
1day
t6 = parser.parse("Tue May 25 11:14:45 2021")
1day4h
t7 = parser.parse("Tue May 25 11:9:45 2021")
1day4h5min
t8 = parser.parse("Tue May 19 11:9:45 2021")
1w
t9 = parser.parse("Tue Apr 26 11:14:45 2021")
1m
t10 = parser.parse("Tue Oct 08 06:00:20 2019")
1y7m, 19m
t11 = parser.parse("Tue Jan 08 00:00:00 2019")
2y4m, 28m
create: date of object creation
now: time now
li: a list of string indicate time (in any language)
lst: suffix (in any language)
long: display length
def howLongAgo(create, now, li, lst, long=2):
dif = create - now
print(dif.days)
sec = dif.days 24 60 60 + dif.seconds
minute = sec // 60
sec %= 60
hour = minute // 60
minute %= 60
day = hour // 24
hour %= 24
week = day // 7
day %= 7
month = (week 7) // 30
week %= 30
year = month // 12
month %= 12
s = []
for ii, tt in enumerate([sec, minute, hour, day, week, month, year]):
ss = li[ii]
if tt != 0:
if tt == 1:
s.append(str(tt) + ss)
else:
s.append(str(tt) + ss + 's')
return ' '.join(list(reversed(s))[:long]) + ' ' + lst
t = howLongAgo(t1, t11, [
'second',
'minute',
'hour',
'day',
'week',
'month',
'year',
], 'ago')
print(t)
2years 4months ago