Python / Django time interval

1

I need to create a time grid for a calendar:

I have the following information in the Django template:

Starting time: 08:00

End time: 6:00 p.m.

Interval in minutes 00:30

I would like to implement something like:

for hora in range(hrInicial, hrFinal, intervalo):
      print(hora)

08:00
08:30
09:00
09:30
...

This would be the parameter setting for calendar creation, so I do not have the date, only the time. The idea is to register for each day of the week a specific period. Ex.:

Segunda - 08:00 as 12:00
Terça   - 08:00 as 18:00
Quarta  - 12:00 as 18:00
...

In the template considering the interval of 30 minutes for second, I want to display:

08:00
08:30
09:00
09:30
10:00
10:30
11:00
11:30
12:00

I researched the similar questions, but for Python, I did not find a similar question.

Thanks for the strength.

    
asked by anonymous 06.05.2017 / 18:45

1 answer

4

You can use the module datetime , with types datetime , timedelta and time . The only detail is that you will necessarily need to enter a date, that is, year, month and day, but as we are interested only in the timetable, any valid date will be possible, because in the end it will be disregarded. In this example, I used the date 01/01/2017.

The logic is this: you can create an object of type datetime as follows

>>> from datetime import datetime, timedelta, time
>>> hrInicial = datetime(2017, 1, 1, 8, 0, 0)

Notice that I had to inform the date initially, and then I gave the time, 8, minutes, 0, and seconds, 0. I can check if the time was set correctly by doing:

>>> print(hrInicial.time())
08:00:00

To add a time interval, we use the type timedelta . For an interval of 30 minutes, we do:

>>> intervalo = timedelta(minutes=30)

We can add this interval to the start time with the same sum operator:

>>> novaHora = hrInicial + intervalo
>>> print(novaHora.time())
08:30:00

Note that the time has been incremented as expected. Using this logic, we can create a generic function:

def get_interval (inicio, fim, intervalo):

    """ 
    Retorna a lista de horários entre 'inicio' e 'fim', inclusive, com um intervalo definido por 'intervalo'.

    @param inicio    iterable Lista de três valores no formato (hora, minutos, segundos)
    @param fim       iterable Lista de três valores no formato (hora, minutos, segundos)
    @param intervalo iterable Lista de três valores no formato (hora, minutos, segundos)
    @return generator
    """

    inicio = datetime(2017, 1, 1, *inicio)
    fim = datetime(2017, 1, 1, *fim)

    iHoras, iMinutos, iSegundos = intervalo

    intervalo = timedelta(hours=iHoras, minutes=iMinutos, seconds=iSegundos)

    while inicio <= fim:
        yield inicio.time()
        inicio += intervalo

The parameters will be a list of three values: the first defines the hours, the second the minutes and the third the seconds. This way, we can do:

>>> hrInicial = (8, 0, 0)
>>> hrFinal   = (12, 0, 0)
>>> intervalo = (0, 30, 0)
>>> for hora in get_interval(hrInicial, hrFinal, intervalo):
...     print(hora)
08:00:00
08:30:00
09:00:00
09:30:00
10:00:00
10:30:00
11:00:00
11:30:00
12:00:00
  

See working at Ideone .

    
06.05.2017 / 19:25