From c28a0bbc8aa068e7bb50f36218a4310d934fcecc Mon Sep 17 00:00:00 2001 From: Dzmitry Plashchynski Date: Thu, 28 May 2015 17:43:02 +0300 Subject: [PATCH] Able to specify minutes for hour-based schedule. Closing #26 --- lib/crono/period.rb | 5 ++++- spec/period_spec.rb | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/crono/period.rb b/lib/crono/period.rb index 661cd88..92b7d9e 100644 --- a/lib/crono/period.rb +++ b/lib/crono/period.rb @@ -49,7 +49,10 @@ module Crono end def parse_at(at) - fail "period should be at least 1 day to use 'at'" if @period < 1.day + if @period < 1.day && (at.is_a? String || at[:hour]) + fail "period should be at least 1 day to use 'at' with specified hour" + end + case at when String time = Time.parse(at) diff --git a/spec/period_spec.rb b/spec/period_spec.rb index 2eb6c36..688238c 100644 --- a/spec/period_spec.rb +++ b/spec/period_spec.rb @@ -52,7 +52,7 @@ describe Crono::Period do expect(@period.next(since: 2.days.ago).to_s).to be_eql(Time.now.to_s) end - it 'should return the time 2 days from now' do + it 'should return time 2 days from now' do @period = Crono::Period.new(2.day) expect(@period.next.to_s).to be_eql(2.days.from_now.to_s) end @@ -80,7 +80,7 @@ describe Crono::Period do it 'should raise error when period is less than 1 day' do expect { Crono::Period.new(5.hours, at: '15:30') - }.to raise_error("period should be at least 1 day to use 'at'") + }.to raise_error("period should be at least 1 day to use 'at' with specified hour") end it 'should return time in relation to last time' do @@ -95,5 +95,21 @@ describe Crono::Period do expect(@period.next.utc.to_s).to be_eql(Time.now.change(at).utc.to_s) end end + + context 'in hourly basis' do + it 'should return next hour minutes if current hour minutes passed' do + Timecop.freeze(Time.now.beginning_of_hour.advance(minutes: 20)) do + @period = Crono::Period.new(1.hour, at: { min: 15 }) + expect(@period.next.utc.to_s).to be_eql 1.hour.from_now.beginning_of_hour.advance(minutes: 15).utc.to_s + end + end + + it 'should return current hour minutes if current hour minutes not passed yet' do + Timecop.freeze(Time.now.beginning_of_hour.advance(minutes: 10)) do + @period = Crono::Period.new(1.hour, at: { min: 15 }) + expect(@period.next.utc.to_s).to be_eql Time.now.beginning_of_hour.advance(minutes: 15).utc.to_s + end + end + end end end