2013-11-03 78 views
1

我使用ice_cube創建計劃。從31日開始的每月計劃會在所有月份中缺少31天。我想安排那幾個月的最後一天。如果我的日程從30日開始,我想每個月的30日和2月的最後一天。閏年進一步使問題複雜化。每月計劃月份結束時間

創建可以處理29日,30日或31日的日程安排的好方法是什麼?

+1

https://github.com/seejohnrun/ice_cube#每月每月 –

+0

@ muistooshort #day_of_ the_month跳過的月份太短(請參閱doc)。因此,如果我的復發從第31次開始,我會在一年中錯過5個月.x –

+0

您是否閱讀過「每個月的第一天和最後一天」示例? –

回答

0

這通過了我的所有規格,但是非常可怕,並且可能會因爲時間超過一年而中斷(我現在不在意)。

class LeasePaymentSchedule 

    def self.monthly(a bunch of args) 
    case start_day 

    when 31 
     schedule = IceCube::Schedule.new(start, scheduler_options) do |s| 
     s.add_recurrence_rule IceCube::Rule.monthly.day_of_month(-1).until(end_time) 
     end 

    when 30,29 
     schedule = IceCube::Schedule.new(start, scheduler_options) do |s| 
     s.add_recurrence_rule IceCube::Rule.monthly.day_of_month(start_day).until(end_time) 
     end 

     schedule.all_occurrences.each do |o| 
     next unless [1,3,6,8,10].include? o.month 
     missed = (o + 1.month).yday 
     # Probably breaks for durations longer than 1 year 
     schedule.add_recurrence_rule IceCube::Rule.yearly.day_of_year(missed).count(1) 
     end 

    else 
     schedule = IceCube::Schedule.new(start, scheduler_options) do |s| 
     s.add_recurrence_rule IceCube::Rule.monthly.day_of_month(start_day).until(end_time) 
     end 
    end 
    schedule 
    end 
end 

這麼多的規格:

Finished in 4.17 seconds 
390 examples, 0 failures 

-

shared_examples_for :a_schedule do 
    it 'returns an IceCube Schedule' do 
    schedule.should be_a IceCube::Schedule 
    end 
    it 'should start on the correct day' do 
    schedule.start_time.should eq expected_start 
    end 
    it 'has the right number of occurrences' do 
    schedule.all_occurrences.size.should eq expected_occurrences 
    end 
end 

describe :monthly do 
    let(:expected_occurrences) { 12 } 
    let(:expected_start) { date.next_month.beginning_of_day } 
    let(:schedule) { LeasePaymentSchedule.monthly } 

    before do 
    Date.stub(:today).and_return(date) 
    end 

    shared_examples_for :on_the_28th do 
    let(:date) { Time.parse "#{year}-#{month}-28" } 
    it_behaves_like :a_schedule 
    end 

    shared_examples_for :on_the_29th do 
    let(:date) { Time.parse "#{year}-#{month}-29" } 
    it_behaves_like :on_the_28th 
    it_behaves_like :a_schedule 
    end 

    shared_examples_for :on_the_30th do 
    let(:date) { Time.parse "#{year}-#{month}-30" } 
    it_behaves_like :on_the_29th 
    it_behaves_like :a_schedule 
    end 

    shared_examples_for :on_the_31st do 
    let(:date) { Time.parse "#{year}-#{month}-31" } 
    it_behaves_like :on_the_30th 
    it_behaves_like :a_schedule 
    end 

    shared_examples_for :the_whole_year do 
    context :february do 
     let(:month) { 2 } 
     it_behaves_like :on_the_28th 
    end 
    [ 4, 7, 9, 11 ].each do |month_num| 
     let(:month) { month_num } 
     it_behaves_like :on_the_30th 
    end 
    [ 1, 3, 5, 6, 8, 10, 12].each do |month_num| 
     let(:month) { month_num } 
     it_behaves_like :on_the_31st 
    end 
    end 

    context :a_leap_year do 
    let(:year) { 2012 } 
    context :february_29th do 
     let(:month) { 2 } 
     it_behaves_like :on_the_29th 
    end 
    it_behaves_like :the_whole_year 
    end 

    context :before_a_leap_year do 
    let(:year) { 2011 } 
    it_behaves_like :the_whole_year 
    end 

    context :nowhere_near_a_leap_year do 
    let(:year) { 2010 } 
    it_behaves_like :the_whole_year 
    end 

end 
0

您可以在本月的最後一天使用day_of_month(-1)

+0

如果我的復發從30日開始,這是不正確的。 –

+0

啊,我現在明白你的問題了。我不確定你可以直接用'ice_cube'做這個。 – micahbf

0

您可以像平時那樣使用29日,30日和31日的day_of_month設置日程安排。然後使用函數確定哪些月份被跳過,並使用單個事件來設置它們。無可否認,這只是一個黑客攻擊,但是可以解決ice_cube的限制。

下面是一個簡單的腳本,用於確定在任何給定年份哪些月份將被跳過。

require 'date' 
def months_less_than_days(days, year = Date.today.year) 
    months = [] 
    (1..11).each do |m| 
     eom = (Date.new(year, m+1, 1) - 1) 
     months << eom if eom.day < days 
    end 
    eom = (Date.new(year+1, 1, 1) - 1) 
    months << eom if eom.day < days 
    months 
end 

puts months_less_than_days(31) # => [2013-02-28, 2013-04-30, 2013-06-30, 2013-09-30, 2013-11-30] 
+0

添加單個事件的語法是什麼?從文檔中不明顯。 –

+0

看看這個答案http://stackoverflow.com/questions/13788926/single-occurrence-event-with-ice-cube-gem-using-start-time-and-end-time –

1

這已固定在最新的冰塊:

IceCube::Schedule.new(Time.parse('Oct 31, 2013 9:00am PDT')) do |s| 
    s.add_recurrence_rule(IceCube::Rule.monthly(1)) 
end.first(5) 

[2013-10-31 09:00:00 -0700, 
2013-11-30 09:00:00 -0800, 
2013-12-31 09:00:00 -0800, 
2014-01-31 09:00:00 -0800, 
2014-02-28 09:00:00 -0800]