-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.py
67 lines (51 loc) · 2.05 KB
/
date.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def time_ago(self):
now = datetime.datetime.now()
delta = now - self.publish # where publish is the time the comment is published on
format_delta = delta.total_seconds() # calculating total seconds since the comment was posted
# converting seconds to years, months, days, hours and minutes :
years = format_delta // (60 * 60 * 24 * 30 * 12)
months = format_delta // (60 * 60 * 24 * 30)
days = format_delta // (60 * 60 * 24)
hours = format_delta // (60 * 60)
minutes = format_delta // 60
seconds = format_delta
if seconds > 60:
while months > 11.99:
months -= 12
while days > 29.99:
days -= 30
while hours > 23.99:
hours -= 24
while minutes > 59.99:
minutes -= 60
while seconds > 59.99:
seconds -= 60
date = ''
if years > 0:
if years == 1:
date += int(years) + ' year'
else:
date += int(years) + ' years'
if months > 0:
if months == 1:
date += ' , ' + str(int(months)) + ' month'
else:
date += ' , ' + str(int(months)) + ' months'
if days > 0:
if days == 1:
date += ' , ' + str(int(days)) + ' day'
else:
date += ' , ' + str(int(days)) + ' days'
if hours > 0:
if hours == 1:
date += ' , ' + str(int(hours)) + ' hour'
else:
date += ' , ' + str(int(hours)) + ' hours'
if minutes > 0:
date += ' , ' + str(int(minutes)) + ' minutes'
date += ' ago'
if years == 0:
date = date[3:]
return date
else:
return 'moments ago' # so that if the comment is posted less than a minute ago it will return 'moments ago'