5
5
from django .utils .translation import ungettext , ugettext as _
6
6
from .utils import now
7
7
8
- JUST_NOW = _ ('just now' )
9
- SECONDS_AGO = (_ ('{seconds} second ago' ), _ ('{seconds} seconds ago' ))
10
- MINUTES_AGO = (_ ('{minutes} minute ago' ), _ ('{minutes} minutes ago' ))
11
- HOURS_AGO = (_ ('{hours} hour ago' ), _ ('{hours} hours ago' ))
12
- YESTERDAY_AT = _ ('yesterday at {time}' )
13
- OLDER_YEAR = (_ ('year' ), _ ('years' ))
14
- OLDER_MONTH = (_ ('month' ), _ ('months' ))
15
- OLDER_WEEK = (_ ('week' ), _ ('weeks' ))
16
- OLDER_DAY = (_ ('day' ), _ ('days' ))
8
+
9
+ def pluralize_year (n ):
10
+ return ungettext (_ ('{num} year ago' ), _ ('{num} years ago' ), n )
11
+
12
+
13
+ def pluralize_month (n ):
14
+ return ungettext (_ ('{num} month ago' ), _ ('{num} months ago' ), n )
15
+
16
+
17
+ def pluralize_week (n ):
18
+ return ungettext (_ ('{num} week ago' ), _ ('{num} weeks ago' ), n )
19
+
20
+
21
+ def pluralize_day (n ):
22
+ return ungettext (_ ('{num} day ago' ), _ ('{num} days ago' ), n )
23
+
24
+
17
25
OLDER_CHUNKS = (
18
- (365.0 , OLDER_YEAR ),
19
- (30.0 , OLDER_MONTH ),
20
- (7.0 , OLDER_WEEK ),
21
- (1.0 , OLDER_DAY ),
26
+ (365.0 , pluralize_year ),
27
+ (30.0 , pluralize_month ),
28
+ (7.0 , pluralize_week ),
29
+ (1.0 , pluralize_day ),
22
30
)
23
- OLDER_AGO = _ ('{number} {type} ago' )
24
31
25
32
26
33
def _un (singular__plural , n = None ):
27
34
singular , plural = singular__plural
28
35
return ungettext (singular , plural , n )
29
36
30
37
31
- def naturaldate (date ):
38
+ def naturaldate (date , include_seconds = False ):
32
39
"""Convert datetime into a human natural date string."""
33
40
34
41
if not date :
@@ -41,29 +48,38 @@ def naturaldate(date):
41
48
delta_midnight = today - date
42
49
43
50
days = delta .days
44
- hours = round (delta .seconds / 3600 , 0 )
51
+ hours = int ( round (delta .seconds / 3600 , 0 ) )
45
52
minutes = delta .seconds / 60
53
+ seconds = delta .seconds
46
54
47
55
if days < 0 :
48
- return JUST_NOW
56
+ return _ ( 'just now' )
49
57
50
58
if days == 0 :
51
59
if hours == 0 :
52
60
if minutes > 0 :
53
- return _un (MINUTES_AGO , n = minutes ).format (minutes = minutes )
61
+ return ungettext (
62
+ _ ('{minutes} minute ago' ),
63
+ _ ('{minutes} minutes ago' ), minutes
64
+ ).format (minutes )
54
65
else :
55
- return JUST_NOW
66
+ if include_seconds and seconds :
67
+ return ungettext (
68
+ _ ('{seconds} second ago' ),
69
+ _ ('{seconds} seconds ago' ), seconds
70
+ ).format (seconds = seconds )
71
+ return _ ('just now' )
56
72
else :
57
- return _un (HOURS_AGO , n = hours ).format (hours = hours )
73
+ return ungettext (
74
+ _ ('{hours} hour ago' ), _ ('{hours} hours ago' ), hours
75
+ ).format (hours = hours )
58
76
59
77
if delta_midnight .days == 0 :
60
- return YESTERDAY_AT .format (time = date .strftime ('%H:%M' ))
78
+ return _ ( 'yesterday at {time}' ) .format (time = date .strftime ('%H:%M' ))
61
79
62
80
count = 0
63
- for chunk , singular_plural in OLDER_CHUNKS :
81
+ for chunk , pluralizefun in OLDER_CHUNKS :
64
82
if days >= chunk :
65
83
count = round ((delta_midnight .days + 1 ) / chunk , 0 )
66
- type_ = _un (singular_plural , n = count )
67
- break
68
-
69
- return OLDER_AGO .format (number = count , type = type_ )
84
+ fmt = pluralizefun (count )
85
+ return fmt .format (num = count )
0 commit comments