usr bin python -coding utf-8- from datetime import datetime timedelta

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/python
#-*-coding:utf-8-*-
from datetime import datetime, timedelta
def human(num, words = []):
last_element = num % 10
if num < 11 or num > 20:
if last_element == 1:
return "%s %s" % (num, words[0])
elif last_element > 1 and last_element < 5:
return "%s %s" % (num, words[1])
else:
return "%s %s" % (num, words[2])
else:
return "%s %s" % (num, words[2])
def humanize(time):
delta = datetime.now() - time
today = datetime.now().replace(hour=0, minute=0, second=0)
yesterday = today - timedelta(days=1)
if delta.days == 0:
if delta.seconds < 60:
return human(delta.seconds, [u'секунда', u'секунды', u'секунд']) + u' назад'
elif delta.seconds < 3600:
minutes = int(delta.seconds / 60)
return human(minutes, [u'минута', u'минуты', u'минут']) + u' назад'
if time > today:
return u'сегодня, %s' % time.strftime('%H:%M')
elif time > yesterday:
return u'вчера, %s' % time.strftime('%H:%M')
else:
return time.strftime('%d %b, %Y %H:%M')
print humanize(datetime(2008, 10, 7, 13, 35))