Skip to content

Commit

Permalink
add hour and clock_phrase
Browse files Browse the repository at this point in the history
  • Loading branch information
Petro31 committed Nov 20, 2023
1 parent 84a5221 commit e2f8844
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion easy_time.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,21 @@
'October',
'November',
'December',
]
],
'time_of_hour':{
0: '{hour} o\'clock',
1: 'a minute past {hour}',
15: 'quarter past {hour}',
30: 'half past {hour}',
45: 'quarter to {hour}',
59: 'a minute to {hour}',
'past_hour': '{minute} past {hour}',
'to_hour': '{minute} to {hour}',
},
'time_of_day':{
'midnight': 'midnight',
'noon': 'noon',
},
},
'sk':{
'_language': 'Slovak',
Expand Down Expand Up @@ -1568,3 +1582,51 @@
{%- endif %}
{%- endif %}
{%- endmacro %}
{%- macro hour(hour, language=None) %}
{%- if hour is datetime %}
{%- set hour = hour.hour %}
{%- endif %}
{%- set _12 = not (hour % 12) %}
{%- set _24 = not (hour % 24) %}
{%- set _12hr = translate('time','format') == '12-hr' %}
{%- if _12 and _24 and _12hr %}
{{- translate('time_of_day', 'midnight', language=language) }}
{%- elif _12 and _12hr %}
{{- translate('time_of_day', 'noon', language=language) }}
{%- else %}
{{- hour % 12 if _12hr else hour }}
{%- endif %}
{%- endmacro %}
{%- macro clock_phrase(input=None, attr=None, utc=False, language=None) %}
{%- if input is none %}
{%- set dt = now() %}
{%- elif input is datetime %}
{%- set dt = input %}
{%- else %}
{%- set dt = _to_datetime(input, attr, utc) | as_datetime %}
{%- endif %}
{%- set this_hour = dt.hour %}
{%- set this_minute = dt.minute %}
{%- if this_minute > 30 %}
{%- set hour_phrase = hour(this_hour + 1, language=language) %}
{%- set minute_calc = 60 - this_minute %}
{%- set minute_phrase = _phrase('minute', 60 * minute_calc, language, True, True) if this_minute % 5 else minute_calc | string %}
{%- else %}
{%- set hour_phrase = hour(this_hour, language=language) %}
{%- set minute_phrase = _phrase('minute', 60 * this_minute, language, True, True) if this_minute % 5 else this_minute | string %}
{%- endif %}
{%- if this_minute in [0, 1, 15, 30, 45, 59] %}
{%- if hour_phrase in ['noon', 'midnight'] and this_minute == 0 %}
{%- set fmat = '{hour}' %}
{%- else %}
{%- set fmat = translate('time_of_hour', this_minute, language=language) %}
{%- endif %}
{%- elif this_minute > 30 %}
{%- set fmat = translate('time_of_hour', 'to_hour', language=language) %}
{%- else %}
{%- set fmat = translate('time_of_hour', 'past_hour', language=language) %}
{%- endif %}
{{- fmat.format(hour=hour_phrase, minute=minute_phrase) -}}
{%- endmacro %}

0 comments on commit e2f8844

Please sign in to comment.