我想创建一个 Api,它获取 start_time 和 end_time 以及应该拆分开始时间和结束时间的月份数,并返回一个集合,我可以循环遍历该集合并将数据保存到数据库中。例如每周日的 14:05 到 18:30。这就是我所做的一切:
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
我在这种情况下面临的问题是:
dates = schedule.occurrences_between(start_time , start_time + 1.months)
当我从四个月后设置 start_time 时,例如 2017-10-13 16:20。它只给 35 周,不给从四个月后开始的 53 周。