Petro31 / easy-time-jinja

Easy Time calculations for Home Assistant templates
MIT License
68 stars 25 forks source link

weekday macro returns wrong days and does not accept datetime argument as documented #26

Closed thomluther closed 9 months ago

thomluther commented 9 months ago

The weekday macro seems to be broken and not working according to doc. The guide says it also accepts datetime optionally, but the macro itself does not have code to support datetime. Furthermore the index for weekdays is calculated wrong since 1 is substracted. the datetime.weekday() function usually returns 0-6 (Mon-Sun), but the weekday macro expects 1-7

I fixed this locally with following idx line:

{%- macro weekday(weekday=None, language=default_language) %}
{%- set idx = weekday % 7 if weekday is integer and weekday >= 0 else weekday.weekday() if weekday is datetime else now().weekday() %}
{{- languages.get(language, {}).get('days', [_bad_value]*7)[idx] }}
{%- endmacro %}

Please check what would be the best official fix for weekday in order to support datetime arguments and keep documentation and weekday index in line with the weekday() datetime function

thomluther commented 9 months ago

Ups, just noticed that the latest version has datetime support, but I did not copy it over to the config\custom_templates folder so the latest macros can be used. I guess I have to update the version in config\custom_templates each time manually after updating easy_time version via HACS?

Petro31 commented 9 months ago

@thomluther each time you update via HACS, you have to change your default language. Unfortunately, there's no way around this at the moment.

Petro31 commented 9 months ago

HACS should automatically move it over to your custom_templates folder when you update via the UI.

thomluther commented 9 months ago

Thanks for letting me know. The default language change to be made each time is clear, I was just wondering why update via HACS GUI did not move it over to my custom_templates folder.... Anyway, copied it over now manually and hope it will be done automatically on future update. Thanks for your work on these templates, they are really useful for proper translations. I'm closing this issue again