Readable Time Format (w/ Good Grammar!)
<p>I've been reading posts upon posts of the methods for converting an input number of seconds, which should be output as a formal string with given durations (hours, minutes, seconds). But I want to know how to format it so that it accounts for singularization/pluralization, when I know, for example, <code>62</code> seconds should read as <code>"1 minute and 2 seconds"</code> as opposed to <code>120</code> seconds which is simply <code>"2 minutes"</code>.</p>

<p>One other criteria is that it should return <code>"now"</code> if the seconds is <code>0</code>. </p>

<p>Here's my code so far: </p>

<pre><code>def format_duration(seconds, granularity = 2):
    intervals = (('hours', 3600), ('minutes', 60), ('seconds', 1))
    human_time = []
    for name, count in intervals: 
        value = seconds // count
        if value: 
            seconds -= value * count
            if value == 1:
                name = name.rstrip('s')
            human_time.append("{} {}".format(value, name))
        else:
            return "now"
    return ','.join(human_time[:granularity])
</code></pre>

<p>Please help! Thanks! </p>

<p>MJ</p>

<p>You can try to code for each variation:</p>

<pre><code>def timestamp(ctime):
    sec = int(ctime)
    if sec == 0:
        return "Now"
    m, s = divmod(sec, 60)
    h, m = divmod(m, 60)
    if h == 1: hr_t = 'Hour'
    else: hr_t = 'Hours'
    if m == 1: mn_t = 'Minute'
    else: mn_t = 'Minutes'
    if s == 1: sc_t = 'Second'
    else: sc_t = 'Seconds'
    time_stamp = ""
    if h &gt; 0 and m ==0 and s ==0:
        time_stamp = "%02d %s " % (h, hr_t)
    elif h &gt; 0:
        time_stamp = "%02d %s, " % (h, hr_t)
    if m &gt; 0 and s !=0:
        time_stamp = time_stamp +"%02d %s and %02d %s" % (m, mn_t, s, sc_t)
    elif m &gt; 0 and s == 0:
        time_stamp = time_stamp +"%02d %s" % (m, mn_t)
    elif m == 0  and s != 0:
        time_stamp = time_stamp +"%02d %s" % (s, sc_t)
    return time_stamp
print (timestamp(11024))
print (timestamp(0))
print (timestamp(31))
print (timestamp(102))
print (timestamp(61))
print (timestamp(60))
print (timestamp(3600))
print (timestamp(3632))
03 Hours, 03 Minutes and 44 Seconds
Now
31 Seconds
01 Minute and 42 Seconds
01 Minute and 01 Second
01 Minute
01 Hour 
01 Hour, 32 Seconds
</code></pre>

<p>Or you can use the <code>relativedelta</code> option in <code>dateutil</code> and then pick the bones out of it.</p>

<pre><code>from dateutil.relativedelta import relativedelta
attrs = ['years', 'months', 'days', 'hours', 'minutes', 'seconds']
human_readable = lambda delta: ['%d %s ' % (getattr(delta, attr), getattr(delta, attr) != 1 and attr   or attr[:-1]) for attr in attrs if getattr(delta, attr) or attr == attrs[-1]]
readable=''
for i in human_readable(relativedelta(seconds=1113600)):
    readable += i
print readable
print human_readable(relativedelta(seconds=13600))
print human_readable(relativedelta(seconds=36))
print human_readable(relativedelta(seconds=60))
print human_readable(relativedelta(seconds=3600))
12 days 21 hours 20 minutes 0 seconds 
['3 hours ', '46 minutes ', '40 seconds ']
['36 seconds ']
['1 minute ', '0 seconds ']
['1 hour ', '0 seconds ']
</code></pre>

<p>For more examples of the second example see: <a href="http://code.activestate.com/recipes/578113-human-readable-format-for-a-given-time-delta/" rel="nofollow">http://code.activestate.com/recipes/578113-human-readable-format-for-a-given-time-delta/</a>
which is where I stole almost all the second set of code from</p>

python time control-flow