Search code examples
ruby-on-railsrubyrubygemsruby-on-rails-5.1ice-cube

splite date with Ice_Cube in rails


I want to create an Api that it get start_time and end_time and the number of month that should split start time and end time and return a collection that i can loop through that and save the data in to the database. for example every week from 14:05 to 18:30 on Sundays. this is what everything that i did:

def create_recurring_schedules number=0 if recurring schedule = Schedule.new(start_time, end_time: end_time)

    schedule.add_recurrence_rule Rule.weekly

     if month == 12

        dates = schedule.occurrences_between(start_time,  start_time + 1.year) 
     elsif month == 3
        logger.info "################### The 3 month is #{self.s_month} #############################################"
        dates = schedule.occurrences_between(start_time, start_time + 3.month)
     elsif month == 6
        logger.info "################### The 6 month is #{month} ###########################################"
        dates = schedule.occurrences_between(start_time, start_time + 6.month)
     elsif month == 1

     dates = schedule.occurrences_between(start_time , start_time + 1.months)
   end
    dates.each do |date|
      number +=1
      logger.info "################### Start number #{number} #############################################"
      ClinicSchedule.create(user_id: user_id, clinic_id: clinic_id, month: month,
                                  start_time: date.start_time.strftime("%Y-%m-%d %H:%M"),
                                  end_time: date.end_time.strftime("%Y-%m-%d %H:%M"),
                                  date: date.strftime("%Y-%m-%d"), recurring: true,
                                  recurrence_id: recurrence_id)
      logger.info "################### Recurrence ID: "
      logger.info recurrence_id
      logger.info "################### End Recurrence ID ###########################################################"
    end
  end
end

the problem that i faced with that is on this case:

dates = schedule.occurrences_between(start_time , start_time + 1.months)

when i set start_time from four month later for example 2017-10-13 16:20. it only give 35 weeks and don't give 53 week that start from four month later.


Solution

  • Your problem arises from s_time=(Time.now+start_time).to_i. It seems that start_time is an instance of ActiveSupport::TimeWithZone. You are trying to add Time and ActiveSupport::TimeWithZone that is probably not the thing you want to achieve.

    schedule.occurrences_between at least expects two instances of ActiveSupport::TimeWithZone. Therefore, you could do something like this:

    s_time = Time.zone.now
    e_time = s_time + 1.month
    
    schedule.occurrences_between(s_time, e_time)
    

    UPD: Actually, you do not need that long conditional with months:

    schedule.occurrences_between(start_time, start_time + month.months)
    

    And instead of defining and incrementing number use #each_with_index