Creating a schedule of appointments in Ruby

2

I want to create a sort of routine that interleaves time interval Example: 8:00 a.m. to 12:00 a.m. 30 minute interval

A sample code

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

The expected result is as follows

  
  • 8:00 a.m. to 8:30 p.m.
  •   
  • 8:30 to 9:00
  •   
  • 9:00 a.m. to 9:30 p.m.
  •   
  • ...
  •   
  • 11:30 to 12:00
  •   

However, there is bug when you change the time.
  Type from 07:00 - 12:00
  Is there any way to do this loop or any suggestion?

    
asked by anonymous 03.08.2016 / 21:40

1 answer

1

This code generates the output you want in the console:

start = Time.new(2016, 07, 29, 8,00,0,  "+03:00")
end_time = Time.new(2016, 07, 29, 12,00,0, "+03:00")

while(start < end_time)
   p "#{start} - #{start + 30.minutes}"
   start = start + 30.minutes
end
    
04.08.2016 / 16:11