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>I found it! I actually needed more intervals - the durations asked for were lengthier than I thought...</p>

<pre><code>def prettyList(human_time):
    if len(human_time) &gt; 1:
        return ' '.join([', '.join(human_time[:-1]), "and", human_time[-1]])
    elif len(human_time) == 1:
        return human_time[0]
    else:
        return ""

def format_duration(seconds, granularity = 4):
    intervals = (('years', 29030400), ('months', 2419200), ('weeks', 604800),('days', 86400),('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))
    if not human_time:
        return "now"
    human_time = human_time[:granularity]
    return prettyList(human_time)
</code></pre>

python time control-flow