diff --git a/Gemfile.lock b/Gemfile.lock index dfac2c8..32e4bdf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,21 +8,21 @@ PATH GEM remote: https://rubygems.org/ specs: - activesupport (3.2.16) - i18n (~> 0.6, >= 0.6.4) - multi_json (~> 1.0) - atomic (1.1.14) + activesupport (4.1.0) + i18n (~> 0.6, >= 0.6.9) + json (~> 1.7, >= 1.7.7) + minitest (~> 5.1) + thread_safe (~> 0.1) + tzinfo (~> 1.1) i18n (0.6.9) json (1.8.1) minitest (5.2.2) minitest-rg (5.0.0) minitest (~> 5.0) - multi_json (1.8.4) rake (10.1.1) rdoc (4.1.1) json (~> 1.4) - thread_safe (0.1.3) - atomic + thread_safe (0.3.3) tzinfo (1.1.0) thread_safe (~> 0.1) diff --git a/lib/business_time.rb b/lib/business_time.rb index 9d3ce2c..f734318 100644 --- a/lib/business_time.rb +++ b/lib/business_time.rb @@ -2,6 +2,7 @@ require 'active_support' require 'active_support/time' require 'time' +require 'yaml' require 'business_time/config' require 'business_time/business_hours' diff --git a/lib/business_time/config.rb b/lib/business_time/config.rb index cc2ea18..2d156b4 100644 --- a/lib/business_time/config.rb +++ b/lib/business_time/config.rb @@ -76,7 +76,7 @@ class << self def end_of_workday(day=nil) if day wday = work_hours[int_to_wday(day.wday)] - wday ? (wday.last =~ /0{1,2}\:0{1,2}/ ? "23:59:59" : wday.last) : config[:end_of_workday] + wday ? (wday.last =~ /^0{1,2}\:0{1,2}$/ ? "23:59:59" : wday.last) : config[:end_of_workday] else config[:end_of_workday] end diff --git a/test/test_calculating_business_duration.rb b/test/test_calculating_business_duration.rb index 35adb97..2901b54 100644 --- a/test/test_calculating_business_duration.rb +++ b/test/test_calculating_business_duration.rb @@ -78,4 +78,20 @@ } assert_equal 68.hours, friday.business_time_until(monday) end + + it 'properly calculate overnight business time' do + BusinessTime::Config.work_hours = { + mon: ["08:00","20:00"], + tue: ["08:00","20:00"], + } + BusinessTime::Config.holidays = [] + + created_at = Time.local(2014, 05, 12, 20, 50) #yesterday night 20:50 + published_at = Time.local(2014, 05, 13, 8, 10) #today morning 08:10 + assert created_at.monday? + assert published_at.tuesday? + + assert_equal 10.minutes, created_at.business_time_until(published_at) + end + end diff --git a/test/test_config.rb b/test/test_config.rb index 0cb7e5e..c4ba17a 100644 --- a/test/test_config.rb +++ b/test/test_config.rb @@ -50,6 +50,18 @@ assert_equal([1,2,4,5], BusinessTime::Config.weekdays.sort) end + it 'end of workday not freakout' do + BusinessTime::Config.work_hours = { + :mon=>["9:00","20:00"], + :tue=>["9:00","17:00"], + :thu=>["9:00","17:00"], + :fri=>["9:00","17:00"] + } + + monday = Time.local(2014, 05, 12, 20, 50) + assert_equal "20:00", BusinessTime::Config.end_of_workday(monday) + end + it "load config from YAML files" do yaml = <<-YAML business_time: