2017-06-12 50 views
0

我想創建它獲得START_TIME和END_TIME和月份的數字應該分裂的開始時間和結束時間,並返回一個集合,我可以通過該循環,並保存到數據的API數據庫。例如每週星期日從14:05至18:30進行例如 。 這是我做了一切:splite日期Ice_Cube在軌

高清create_recurring_schedules 數= 0 如果反覆出現 時間表= 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周。

+0

爲什麼要那樣做'Time.now + start_time'? – yzalavin

+0

它是我這樣做了,START_TIME可兩個月後的最瘋狂的事情之一。當我用Time.now獨立它的工作很好,我這樣做是爲了看看發生了什麼 – Sandro

回答

0

你的問題來自s_time=(Time.now+start_time).to_i。看來,start_timeActiveSupport::TimeWithZone一個實例。您嘗試添加TimeActiveSupport::TimeWithZone這是可能不是你想要實現的東西。

schedule.occurrences_between至少預計ActiveSupport::TimeWithZone兩個實例。因此,你可以做這樣的事情:

s_time = Time.zone.now 
e_time = s_time + 1.month 

schedule.occurrences_between(s_time, e_time) 

UPD:其實,你並不需要那麼長的條件伴月:

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

而不是定義和遞增number使用#each_with_index

+0

我這樣做,但我不知道爲什麼,當我設置START_TIME + 1.month我得到44周。我應該是4周而不是44周 – Sandro

+0

你是什麼意思「我得到44周」? – yzalavin

+0

我的意思是關於上述循環,每個日期| ==>它的循環44次,我節省44 – Sandro