Iteration with variable of type 'TIME'

0

I'm doing a project in which I need to make a loop that increases minute by minute of a variable from two times.

I thought of logic in the following way:

 vHoraInicial = '13:30'
 vHoraFinal   = '15:00'

While vHoraInicial <= vHoraFinal:
        print (vHoraInicial)
        vHoraInicial = vHorainicial + 1
  

Print Result:   13:30

     

13:31

     

13:32

     

...

     

15:00

But I do not know the functions that help me manipulate variable 'TIME'. Anyone have any ideas?

Thank you

    
asked by anonymous 08.05.2018 / 17:35

1 answer

1

As it is, you are not working with schedules, but rather with string . For us it ends up being the same thing, but for the computer they are completely different. To work with schedules, you'll need the datetime module.

import datetime

start = datetime.datetime(year=2018, month=5, day=8, hour=13, minute=30)
end = datetime.datetime(year=2018, month=5, day=8, hour=15, minute=0)
interval = datetime.timedelta(minutes=1)

while start <= end:
    print(start)
    start += interval

See working at Repl.it | Ideone | GitHub GIST

Note that by working with schedules, you need to set the date, since if the time exceeds 23:59:59, the day will change. If there is a guarantee that the start and end times will always be on the same day, for practical purposes, you can set the date constant and, instead of displaying the date directly, format it, print(format(start, '%H:%M:%S')) .

    
08.05.2018 / 18:04