From 3f9ea8350d6262b56b51519b119ed1d3873e2e63 Mon Sep 17 00:00:00 2001 From: Adrian Colyer Date: Sun, 6 Nov 2016 15:47:50 +0000 Subject: [PATCH] schedule today if at: time not passed --- lib/crono/period.rb | 1 + spec/period_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/crono/period.rb b/lib/crono/period.rb index 3c7a436..cc736f6 100644 --- a/lib/crono/period.rb +++ b/lib/crono/period.rb @@ -48,6 +48,7 @@ module Crono def initial_day return Time.now unless @on day = Time.now.beginning_of_week.advance(days: @on) + day = day.change(time_atts) return day if day.future? @period.from_now.beginning_of_week.advance(days: @on) end diff --git a/spec/period_spec.rb b/spec/period_spec.rb index 5ecf744..f9ec005 100644 --- a/spec/period_spec.rb +++ b/spec/period_spec.rb @@ -49,6 +49,16 @@ describe Crono::Period do expect(@period.next).to be_eql(tuesday) end end + + it 'should return today on the first run if not too late' do + @period = Crono::Period.new(1.week, on: :sunday, at: '22:00') + Timecop.freeze(Time.now.beginning_of_week.advance(days: 6) + .change(hour: 21, min: 0)) do + expect(@period.next).to be_eql( + Time.now.beginning_of_week.advance(days: 6).change(hour: 22, min: 0) + ) + end + end end context 'in daily basis' do