aboutsummaryrefslogtreecommitdiff
path: root/resumejson_converter/utils/templates.py
blob: a0341031580a87f707676ea480a686bfa5530af3 (plain)
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
def td_format(td_object):
    """
    Return time in `An, Mois, Semaine, Jour, Heure, Minute, Seconde` format.
    Only in french.

    Based on https://stackoverflow.com/a/13756038
    """
    seconds = int(td_object.total_seconds())
    periods = [
        ('an',       60*60*24*365),
        ('mois',        60*60*24*30),
        ('semaine',     60*60*24*7),
        ('jour',        60*60*24),
        ('heure',       60*60),
        ('minute',      60),
        ('seconde',     1)
    ]

    strings = []
    for period_name, period_seconds in periods:
        if seconds > period_seconds:
            period_value, seconds = divmod(seconds, period_seconds)
            has_s = 's' if period_value > 1 and period_name != "mois" else ''
            strings.append("%s %s%s" % (period_value, period_name, has_s))

    return ", ".join(strings)