{# set phrases to be used in the relative_time_period macro one list item per language, each time fraction contains a list with the singular, plural and abbriviated phrase combine contains the text to combine the last time fraction, and error the text to display on wrong date input #} {%- set time_period_phrases = [ { 'language': 'en', 'phrases': { 'year': ['year', 'years', 'yr'], 'month': ['month', 'months', 'mth'], 'week': ['week', 'weeks', 'wk'], 'day': ['day', 'days', 'day'], 'hour': ['hour', 'hours', 'hr'], 'minute': ['minute', 'minutes', 'min'], 'second': ['second', 'seconds', 'sec'], 'combine': ' and ', 'error': 'Incorrect date' } }, { 'language': 'fr', 'phrases': { 'year': ['année', 'années', 'an'], 'month': ['mois', 'mois', 'mois'], 'week': ['semaine', 'semaines', 'sem'], 'day': ['jour', 'jours', 'j'], 'hour': ['heure', 'heures', 'h'], 'minute': ['minute', 'minutes', 'min'], 'second': ['seconde', 'secondes', 'sec'], 'combine': ' et ', 'error': 'Date non valide' } }, { 'language': 'it', 'phrases': { 'year': ['anno', 'anni', 'aa'], 'month': ['mese', 'mesi', 'mm'], 'week': ['settimana', 'settimane', 'set'], 'day': ['giorno', 'giorni', 'gg'], 'hour': ['ora', 'ore', 'h'], 'minute': ['minuto', 'minuti', 'min'], 'second': ['secondo', 'secondi', 'sec'], 'combine': ' e ', 'error': 'Data non valida' } }, { 'language': 'nl', 'phrases': { 'year': ['jaar', 'jaar', 'jr'], 'month': ['maand', 'maanden', 'mnd'], 'week': ['week', 'weken', 'wk'], 'day': ['dag', 'dagen', 'dg'], 'hour': ['uur', 'uren', 'u'], 'minute': ['minuut', 'minuten', 'min'], 'second': ['seconde', 'seconden', 'sec'], 'combine': ' en ', 'error': 'Incorrecte datum' } }, { 'language': 'de', 'phrases': { 'year': ['Jahr', 'Jahre', 'J.'], 'month': ['Monat', 'Monate', 'M.'], 'week': ['Woche', 'Wochen', 'Wo.'], 'day': ['Tag', 'Tage', 'Tg.'], 'hour': ['Stunde', 'Stunden', 'Std.'], 'minute': ['Minute', 'Minuten', 'Min.'], 'second': ['Sekunde', 'Sekunden', 'Sek.'], 'combine': ' und ', 'error': 'Falsches Datum' } }, { 'language': 'pt', 'phrases': { 'year': ['ano', 'anos', 'aa'], 'month': ['mês', 'meses', 'mm'], 'week': ['semana', 'semanas', 'sem'], 'day': ['dia', 'dias', 'd'], 'hour': ['hora', 'horas', 'h'], 'minute': ['minuto', 'minutos', 'min'], 'second': ['segundo', 'segundos', 'seg'], 'combine': ' e ', 'error': 'Data Inválida' } } ] %} {# macro to split a timedelta in years, months, weeks, days, hours, minutes, seconds used by the relative time plus macro, set up as a seperate macro so it can be reused #} {%- macro time_split(date, time, compare_date) -%} {# set defaults for variables #} {%- set date = date | as_local -%} {%- set time = time | default(true) | bool(true) -%} {%- set n = compare_date if compare_date is defined else now() -%} {%- set n = n if time else today_at() -%} {%- set a = [n, date] | max -%} {%- set b = [n, date] | min -%} {#- set time periods in seconds #} {%- set m, h, d, w = 60, 3600, 86400, 604800 -%} {#- set numer of years, and set n to value using this number of years #} {%- set yrs = a.year - b.year - (1 if a.replace(year=b.year) < b else 0) -%} {%- set a = a.replace(year=a.year - yrs) -%} {#- set numer of months, and set n to value using this number of months #} {%- set mth = (a.month - b.month - (1 if a.day < b.day else 0) + 12) % 12 -%} {%- set month_new = (((a.month - mth) + 12) % 12) | default(12, true) -%} {%- set day_max = ((a.replace(day=1, month=month_new) + timedelta(days=31)).replace(day=1) - timedelta(days=1)).day -%} {%- set a_temp = a.replace(month=month_new, day=[a.day, day_max]|min) -%} {%- set a = a_temp if a_temp <= a else a_temp.replace(year=a.year-1) -%} {#- set other time period variables #} {%- set s = (a - b).total_seconds() -%} {%- set wks = (s // w) | int -%} {%- set day = ((s - wks * w) // d) | int -%} {%- set hrs = ((s - wks * w - day * d) // h) | int -%} {%- set min = ((s - wks * w - day * d - hrs * h) // m) | int -%} {%- set sec = (s - wks * w - day * d - hrs * h - min * m) | int -%} {# output result #} {{- dict(y=yrs, mo=mth, w=wks, d=day, h=hrs, m=min, s=sec) | to_json -}} {%- endmacro -%} {# macro to output a timedelta in a readable format #} {%- macro relative_time_plus(date, parts, week, time, abbr, language, compare_date, verbose) -%} {#- set defaults for input if not entered #} {%- set date = date | as_datetime if date is string or date is number else date -%} {%- set compare_date = compare_date if compare_date is defined else now() -%} {%- set compare_date = compare_date | as_datetime if compare_date is string or compare_date is number else compare_date -%} {%- set phrases = time_period_phrases -%} {#- select correct phrases bases on language input #} {%- set language = language | default() -%} {%- set languages = phrases | map(attribute='language') | list -%} {%- set language = iif(language in languages, language, languages | first) -%} {%- set phr = phrases | selectattr('language', 'eq', language) | map(attribute='phrases') | list | first -%} {#- check for valid datetime (using as_timestamp) #} {%- if as_timestamp(date, default='error') != 'error' -%} {%- set date = date | as_local -%} {%- set parts = parts | default(1) | int(1) -%} {%- set week = week | default(true) | bool(true) -%} {%- set time = time | default(true) | bool(true) -%} {%- set abbr = abbr | default(false) | bool(false) or verbose | default(false) | bool(false) -%} {%- set language = language | default('first') -%} {%- set date = date if time else today_at().replace(year=date.year, month=date.month, day=date.day) -%} {%- set tp = time_split(date, time, compare_date) | from_json -%} {#- create mapping #} {%- set wk = tp.w if week else 0 -%} {%- set dy = tp.d if week else tp.d + tp.w * 7 -%} {%- set dur = dict( yrs = dict(a=tp.y, d=phr.year[2] if abbr else phr.year[1] if tp.y > 1 else phr.year[0]), mth = dict(a=tp.mo, d=phr.month[2] if abbr else phr.month[1] if tp.mo > 1 else phr.month[0]), wks = dict(a=wk, d=phr.week[2] if abbr else phr.week[1] if wk > 1 else phr.week[0]), day = dict(a=dy, d=phr.day[2] if abbr else phr.day[1] if dy > 1 else phr.day[0]), hrs = dict(a=tp.h, d=phr.hour[2] if abbr else phr.hour[1] if tp.h > 1 else phr.hour[0]), min = dict(a=tp.m, d=phr.minute[2] if abbr else phr.minute[1] if tp.m > 1 else phr.minute[0]), sec = dict(a=tp.s, d=phr.second[2] if abbr else phr.second[1] if tp.s > 1 else phr.second[0]) ) -%} {#- find first non zero time period #} {%- set first = dur.items() | rejectattr('1.a', 'eq', 0) | map(attribute='0') | first -%} {#- set variable to reject weeks if set and find index of first non zero time period #} {%- set week_reject = 'wks' if not week -%} {%- set index = (dur.keys() | reject('eq', week_reject) | list).index(first) -%} {#-select non zero items based on input #} {%- set items = (dur.keys() | reject('eq', week_reject) | list)[index:index + parts] -%} {%- set selection = dur.items() | selectattr('0', 'in', items) | rejectattr('1.a', 'eq', 0) | list -%} {#- create list of texts per selected time period #} {%- set ns = namespace(text = []) -%} {%- for i in selection -%} {%- set ns.text = ns.text + [ i[1].a ~ ' ' ~ i[1].d] -%} {%- endfor -%} {#- join texts in a string, using phr.combine for the last item #} {{- ns.text[:-1] | join(', ') ~ phr.combine ~ ns.text[-1] if ns.text | count > 1 else ns.text | first -}} {%- else -%} {{- phr.error -}} {%- endif -%} {%- endmacro -%}