Creating a Ruby Commitment Schedule

Asked

Viewed 122 times

2

I want to create a kind of routine that Intercale time interval Example: 8:00 to 12:00 with 30 Minutes interval

A sample of the 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 to 8:30
  • 8:30 to 9:00
  • 9:00 to 9:30
  • ...
  • 11:30 to 12:00

However how there is bug when changing hours.
Type of 07:00 - 12:00
Is there any way to make this loop or any suggestions?

1 answer

1

This code generates the output you want on 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

Browser other questions tagged

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