Easy Time Macros for Templates!

v1.0.5.1

New Features

  • Number of Days in Month Counters
  • Weekday dates

New Languages

  • Danish
  • French
1 Like

This is wonderful, really…
As per requests, I have seen quite a few: xth weekday of the month, e.g. for garbage collection

Ah yes, that should be a quick add

And another one: time untill/since a specified datetime

that’s already done. easy_time, big_time, easy_relative_time, etc

Not quite the same request, days remaining until a monthly day, like this:

1 Like

Which I would like to see extended to a datetime, so that one can use more detail then just days

I’m not following what you’re requesting. A difference between datetimes is a timedelta. Is this what you’re asking for? Otherwise there are methods that already handle relative times but they output friendly verbiage.

I can make functions that return timedeltas, but the difference between two datetimes will never be a datetime.

I’m after the days remaining from a monthly anniversary day.

Like how many days until my monthly bill is due on the xth day of each month.

Yeah, I understood what you’re after, I can add that pretty easily. My concern is about vingerha’s request because it doesn’t make sense to me.

I think I’ll have to add vingerha’s request in order to even do yours. So I’d like his clarification first. Then I can reuse functions when outputting your info

1 Like

As an example, I’d like to know the time to (say) 27th of April, 14:00 (my timezone). I’d like to express that not just in days, I also want hours and maybe others want minutes too but that format maybe just a post-macro formatting of the ts in seconds…let’s not make it too complex.
For the overtime, similar, I want to show my (live example) kids that their work is xd, yh due

That would be a timedelta object

Yes, this is already done with easy_time, big_time, easy_relative_time, or big_relative_time.

  'es':{
    '_language': 'Español',
    'and': 'y',
    'in': 'en',
    'ago': 'hace',
    'now': 'ahora',
    'lose': 'perder',
    'gain': 'ganar',
    'time':{
      'format': '24-hr',
      'year': [
        'a',
        'año',
        'años',
      ],
      'week': [
        'sem.',
        'semana',
        'semanas',
      ],
      'day': [
        'd',
        'día',
        'días',
      ],
      'hour': [
        'h',
        'hora',
        'horas',
      ],
      'minute': [
        'm',
        'minuto',
        'minutos',
      ],
      'second': [
        's',
        'segundo',
        'segundos',
      ],
    },
    'delta':{
      'today': 'hoy',
      'tomorrow': 'mañana',
      'yesterday': 'ayer',
    },
    'days':[
      'lunes', 
      'martes', 
      'miércoles', 
      'jueves', 
      'viernes',
      'sábado', 
      'domingo'
    ],
    'months':[
      'enero',
      'febrero',
      'marzo',
      'abril',
      'mayo',
      'junio',
      'julio',
      'agosto' 
      'septiembre', 
      'octubre', 
      'noviembre', 
      'diciembre'
    ]
  }
} %}

1 Like

Are you interested in submissions?

I have this which returns a natural language time.

Feel free to use it or not. I am sure there has to be a better way of doing that if...elif block so do with it what you will.

Also feel free to delete this post if you don’t want to have it start filling up with submissions that you can’t or don’t want to use.

#=======================================================
#=== date_and_time
#===
#===  Receives a datetime object
#===    eg now()
#===  Returns date and time in natural language
#===    13 minutes past 4 on Tuesday the 11th of April.
#===
#===
#===  Optional parameters:
#===    'time_only' to return only the time
#===    'date_only' to return only the date
#=======================================================
{%- macro date_and_time(date_time) %}

  {#-              -#}
  {#- CONVERT TIME -#}
  {#-              -#}
  {%- set hrs = date_time.hour %}
  {%- set min = date_time.minute %}
  {%- if hrs > 12 or (hrs == 12 and min > 30) %}
    {%- set hrs = hrs - 12 %}
  {%- endif %}

  {%- set min_nl = 
    {
      0: hrs ~ " o'clock",
      1: 'a minute past ' ~ hrs,
      5: min ~ ' past ' ~ hrs,
      10: min ~ ' past ' ~ hrs,
      15: 'quarter past ' ~ hrs,
      20: min ~ ' past ' ~ hrs,
      25: min ~ ' past ' ~ hrs,
      30: 'half past ' ~ hrs,
      35: (60 - min) ~ ' to ' ~ (hrs + 1),
      40: (60 - min) ~ ' to ' ~ (hrs + 1),
      45: 'quarter to ' ~ (hrs + 1),
      50: (60 - min) ~ ' to ' ~ (hrs + 1),
      55: (60 - min) ~ ' to ' ~ (hrs + 1),
      59: 'a minute to ' ~ (hrs + 1)
    }
  %}

  {%- if min_nl[min] is defined %}
    {%- set time_nl = min_nl[min] %}
  {%- elif min < 30 %}
    {%- set time_nl = min ~ ' minutes past ' ~ hrs %}
  {%- elif min > 30 %}
    {%- set time_nl = (60 - min) ~ ' minutes to ' ~ (hrs + 1) %}
  {%- else %}
    {%- set time_nl = min %}
  {%- endif %}

  {#-              -#}
  {#- CONVERT DATE -#}
  {#-              -#}
  {%- set day = date_time.strftime("%A") %}
  {%- set date = date_time.day | ordinal %}
  {%- set month = date_time.strftime("%B") %}

  {%- set date_nl = day ~ ' the ' ~ date ~ ' of ' ~ month %}

  {%- if 'time_only' in varargs %}
    {{- time_nl -}}
  {%- elif 'date_only' in varargs %}
    {{- date_nl -}}
  {%- else %}
    {{- time_nl ~ ' on ' ~ date_nl ~ '. ' -}}
  {%- endif %}

{%- endmacro %}


EDIT 1: I couldn’t bear looking at that if...elif any longer. I wrote that years ago so I’ve re-done it. Probably still not optimal but it looks a bit easier on the eye. Also I realised it spat out a load of white space so I’ve changed that too.

EDIT 2: I have now added optional arguments so that it can return just the date or just the time

1 Like

I think what vingerha is referring to is that timedeltas have no format function like datetimes do. Often people then struggle to format a timedelta as e.g. HH:mm, because you either need to compose the parts or make a fake datetime and then use the format function. Not super hard, but less experienced people might miss the tricks.

Indeed it is a more of that and sorry for explanation / comms.

Right now I am using this below script from HACS, it counts down or up from a specific date…in days
I would like to have it count up / down from a datetime having an option showing in xdays, yhours, zmins without needing to find out how to format it.
Noting that I myself have (!) the knowledge to do this but seeing the considerable amount of questions around dates/time, I was thinking to make it ‘easy’ for others too
Could be a case of mixing two macro’s, on for the diff and one for the format.

I hope this helped a bit more

GitHub - point-4ward/ps-date-countdown: A python script for Homeassistant that counts down the days to birthdays, anniversaries etc

Yes a formatter for timedeltas is on my list of things to add.

2 Likes

Good day, thank you for this template.
Apologies if my question is trivial, but im not successfull using this.
Is this example suppose to work in the template editor?

{% from 'easy_time.jinja' import easy_time %}
{{ clock() }}

I get this error: UndefinedError: ‘clock’ is undefined

I’ve saved the jinja template in the config\custom_templates directory and reloaded it.

{% from 'easy_time.jinja' import clock %}

Thank you. Now it’s saying ‘UndefinedError: ‘language’ is undefined’

But it’s defined in the first line per your instructions