How to calculate 'end time' in Python?

Asked

Viewed 3,684 times

0

qpd = int(input('Quantidade produzida: '))  # qpd = Quantidade Produzida
qpg = int(input('Quantidade programada: '))  # qpg = Quantidade Programada
qrs = qpg - qpd  # qrs = Quantidade Restante
qpc = qrs / 550  # qpc = Quantidade por Carro
mrs = int(qpc * 18)  # mrs = Minutos Restantes
if mrs > 60:
    hora = mrs // 60  # hora = Horas até o término da solução
    minuto = mrs % 60  # minuto = Minuto até o término da solução
    print(f'Faltam, aproximadamente, {hora} horas e {minuto} minutos para'
          ' o término da solução.')
else:
    print(f'Faltam, aproximadamente, {mrs} minutos para'
          ' o término da solução.')

I am a beginner in Python and I am creating a code that calculates the time of completion of a process. The code is still very simple because my knowledge is still very limited.

I want my code to calculate what time the process will be completed, but I still don’t know how. For now, I can only calculate how many hours and minutes to the end of the process. I did some research and found the module datetime, but I couldn’t get information that would help me with this problem.

1 answer

3


Hello, welcome!

The library datetime is really a good solution to work with dates and times. For example, in the form below we can set the current date and time:

import datetime as dt

print(dt.datetime.now())
datetime.datetime(2018, 9, 15, 3, 21, 1, 529738)

In the above result we have the function now() which returns the current time in its own structure datetime storing, respectively, the year, month, day, hour, minute, second and microsecond.

Another interesting feature of this library is the ease of operating with schedules using dt.timedelta(). Example:

agora = dt.datetime.now();
daqui_um_segundo = agora + dt.timedelta(seconds = 1);
daqui_um_minuto = agora + dt.timedelta(minutes = 1);
daqui_uma_hora = agora + dt.timedelta(hours = 1);
datetime.datetime(2018, 9, 15, 3, 27, 29, 524590)
datetime.datetime(2018, 9, 15, 3, 27, 30, 524590)
datetime.datetime(2018, 9, 15, 3, 28, 29, 524590)
datetime.datetime(2018, 9, 15, 4, 27, 29, 524590)

For better visualization it is possible to use the function strftime to format the time display:

agora1 = agora.strftime("%Y-%m-%d %H:%M:%S")
agora2 = agora.strftime("%H:%M:%S")
agora3 = agora.strftime("%H:%M")

print(agora1)
print(agora2)
print(agora3)
2018-09-15 03:27:29
03:27:29
03:27

See that function strftime receives a parameter that determines the desired date and time format, where each letter corresponds to some element. %H it’s time to, %Y is year and so on. The other characters outside the identifiers are optional.

Knowing this information it is possible to establish future time based on a time interval. In your case you have the minutes and hours for the process. Only the minutes is enough using the resources cited. See:

qpd = int(input('Quantidade produzida: '))  # qpd = Quantidade Produzida
qpg = int(input('Quantidade programada: '))  # qpg = Quantidade Programada
qrs = qpg - qpd  # qrs = Quantidade Restante
qpc = qrs / 550  # qpc = Quantidade por Carro
mrs = int(qpc * 18)  # mrs = Minutos Restantes
if mrs > 60:
    hora = mrs // 60  # hora = Horas até o término da solução
    minuto = mrs % 60  # minuto = Minuto até o término da solução
    print(f'Faltam, aproximadamente, {hora} horas e {minuto} minutos para'
          ' o término da solução.')
else:
    print(f'Faltam, aproximadamente, {mrs} minutos para'
          ' o término da solução.')

hora_atual = dt.datetime.now()
hora_final = hora_atual + dt.timedelta(minutes = mrs)

hora_atual = hora_atual.strftime("%H:%M")
hora_final = hora_final.strftime("%H:%M")
print(f"A hora atual é {hora_atual}")
print(f"O horário final do processo é {hora_final}")
Quantidade produzida: 45
Quantidade programada: 567
Faltam, aproximadamente, 17 minutos para o término da solução.
A hora atual é 03:45
O horário final do processo é 04:02
  • Lorran, almost a year and a half later I come back to thank you. I’m sorry it took me so long. Your answer helped me A lot by being simple and easy to understand. Thank you for your time and willingness. And again, forgive the delay in thanking you. May God bless you!

  • Eduardo, the important thing was my reply to be useful for you! Any questions just post here!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.