Skip to content

Commit 7251d9b

Browse files
craigloftusjmerdich
authored andcommitted
Split duration rendering into human_duration_string utility function
1 parent bc70c39 commit 7251d9b

2 files changed

Lines changed: 35 additions & 29 deletions

File tree

natural_duration/fields.py

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
from django.utils.dateparse import parse_duration
99
from django.utils.duration import duration_string
1010

11+
from .utils import human_duration_string
12+
13+
1114
MICRO = timedelta(microseconds=1)
1215
MILLIS = timedelta(milliseconds=1)
1316
SECOND = timedelta(seconds=1)
@@ -145,32 +148,5 @@ def prepare_value(self, value):
145148
return duration_string(value)
146149
elif value == timedelta():
147150
return "a moment"
148-
builder = []
149-
# a list of tuples like ngettext (but with no subs for single)
150-
151-
us = abs(value.microseconds)
152-
seconds = abs(value.seconds)
153-
days = abs(value.days)
154-
builder.append(('a year', '%d years', days // 365))
155-
builder.append(('a month', '%d months', days % 365 // 30))
156-
builder.append(('a week', '%d weeks', days % 365 % 30 // 7))
157-
builder.append(('a day', '%d days', days % 365 % 30 % 7))
158-
builder.append(('an hour', '%d hours', seconds // 60 // 60))
159-
builder.append(('a minute', '%d minutes', seconds // 60 % 60))
160-
builder.append(('a second', '%d seconds', seconds % 60))
161-
builder.append(('a millisecond', '%d milliseconds', us // 1000))
162-
builder.append(('a microsecond', '%d microseconds', us % 1000))
163-
164-
legit = []
165-
for tup in builder:
166-
if tup[2] == 0:
167-
continue
168-
elif tup[2] == 1:
169-
legit.append(tup[0])
170-
else:
171-
legit.append(tup[1] % tup[2])
172-
if len(legit) == 1:
173-
return legit[0]
174-
elif len(legit) == 2:
175-
return legit[0] + " and " + legit[1]
176-
return ", ".join(legit[:-1]) + ", and " + legit[-1]
151+
else:
152+
return human_duration_string(value)

natural_duration/utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def human_duration_string(value):
2+
builder = []
3+
# a list of tuples like ngettext (but with no subs for single)
4+
5+
us = abs(value.microseconds)
6+
seconds = abs(value.seconds)
7+
days = abs(value.days)
8+
builder.append(('a year', '%d years', days // 365))
9+
builder.append(('a month', '%d months', days % 365 // 30))
10+
builder.append(('a week', '%d weeks', days % 365 % 30 // 7))
11+
builder.append(('a day', '%d days', days % 365 % 30 % 7))
12+
builder.append(('an hour', '%d hours', seconds // 60 // 60))
13+
builder.append(('a minute', '%d minutes', seconds // 60 % 60))
14+
builder.append(('a second', '%d seconds', seconds % 60))
15+
builder.append(('a millisecond', '%d milliseconds', us // 1000))
16+
builder.append(('a microsecond', '%d microseconds', us % 1000))
17+
18+
legit = []
19+
for tup in builder:
20+
if tup[2] == 0:
21+
continue
22+
elif tup[2] == 1:
23+
legit.append(tup[0])
24+
else:
25+
legit.append(tup[1] % tup[2])
26+
if len(legit) == 1:
27+
return legit[0]
28+
elif len(legit) == 2:
29+
return legit[0] + " and " + legit[1]
30+
return ", ".join(legit[:-1]) + ", and " + legit[-1]

0 commit comments

Comments
 (0)