Eu quero criar uma especie de rotina que intercale intervalo de tempo Exemplo: 8:00 à 12:00 com intervalo de 30 Minutos
Uma amostra do código
class Appointment < ActiveRecord::Base
has_many :schedules
def advance_time(resource)
return Time.new(2016, 07, 29,
resource.strftime("%H").to_i,
resource.strftime("%M").to_i, 0).
advance(minutes: 30)
end
def manage_time
resource[:start] = Time.new(2016, 07, 29, 8,00,0, "+03:00")
resource[:end] = Time.new(2016, 07, 29, 12,00,0, "+03:00")
interval = (( resource[:end] - resource[:start] ) / 3600).round
counter = 0
schedules.each do |schedule|
@last_appointment = advance_time(resource[:start])
schedule.update_attributes(end: @last_appointment)
while counter <= interval do
@x = advance_time(@last_appointment)
@next_appointment = Schedule.create(start: @last_appointment,
end: @x)
@last_appointment = DateTime.parse(@next_appointment.end)
counter += 1
end
@ultimate_appointment = DateTime.parse(@last_appointment.end
@next_appointment = Schedule.new(start: @last_appointment,
end: @x)
end
end
end
O resultado esperado é o seguinte
- 8:00 to 8:30
- 8:30 to 9:00
- 9:00 to 9:30
- ...
- 11:30 to 12:00
Entretanto como existe bug ao trocar de horário.
Tipo de 07:00 - 12:00
Existe alguma forma de fazer esse loop ou alguma sugestão?