2013-11-03 2 views
1

나는 ice_cube 보석을 사용하여 일정을 만듭니다. 31 일에 시작하는 월간 일정을 작성하는 데는 31 일 미만의 모든 월이 누락됩니다. 그 달의 마지막 날을 예약하고 싶습니다. 30 일에 일정이 시작되면 매월 30 일과 2 월 마지막 날을 원합니다. 도약 년은 그 문제를 더욱 복잡하게 만듭니다.월말 일정을 잡는 월별 일정

29 일, 30 일 또는 31 일에 시작하는 일정을 만드는 좋은 방법은 무엇입니까?

+1

https://github.com/seejohnrun/ice_cube # 월간 월별 –

+0

@ 뮤직 쇼트 #day_of_ the_month는 지나치게 짧은 달을 건너 뜁니다 (doc 참조). 재발이 31 일에 시작되면 나는 5 개월을 그리워합니다. –

+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

일반적인 일정대로 day_of_month를 사용하여 29, 30, 31 일정을 설정할 수 있습니다. 그런 다음 함수를 사용하여 건너 뛴 월을 결정하고 단일 발생 이벤트를 사용하여 월을 설정하십시오. 틀림없이 약간의 해킹이 있지만 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 수정되었습니다 :

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] 
관련 문제