diff --git a/lib/business_time/business_days.rb b/lib/business_time/business_days.rb index 644d55c..05fd578 100644 --- a/lib/business_time/business_days.rb +++ b/lib/business_time/business_days.rb @@ -7,8 +7,12 @@ def initialize(days) @days = days end - def after(time = Time.now) - time = Time.zone ? Time.zone.parse(time.strftime('%Y-%m-%d %H:%M:%S %z')) : Time.parse(time.strftime('%Y-%m-%d %H:%M:%S %z')) + def now + BusinessTime::Config.time_zone ? BusinessTime::Config.time_zone.now : Time.now + end + + def after(time = now) + time = BusinessTime::Config.time_zone ? BusinessTime::Config.time_zone.parse(time.strftime('%Y-%m-%d %H:%M:%S %z')) : Time.parse(time.strftime('%Y-%m-%d %H:%M:%S %z')) days = @days while days > 0 || !Time.workday?(time) days -= 1 if Time.workday?(time) @@ -20,8 +24,8 @@ def after(time = Time.now) alias_method :from_now, :after alias_method :since, :after - def before(time = Time.now) - time = Time.zone ? Time.zone.parse(time.rfc822) : Time.parse(time.rfc822) + def before(time = now) + time = BusinessTime::Config.time_zone ? BusinessTime::Config.time_zone.parse(time.rfc822) : Time.parse(time.rfc822) days = @days while days > 0 || !Time.workday?(time) days -= 1 if Time.workday?(time) diff --git a/lib/business_time/business_hours.rb b/lib/business_time/business_hours.rb index 1fc5376..d49b039 100644 --- a/lib/business_time/business_hours.rb +++ b/lib/business_time/business_hours.rb @@ -6,11 +6,11 @@ def initialize(hours) end def ago - Time.zone ? before(Time.zone.now) : before(Time.now) + BusinessTime::Config.time_zone ? before(BusinessTime::Config.time_zone.now) : before(Time.now) end def from_now - Time.zone ? after(Time.zone.now) : after(Time.now) + BusinessTime::Config.time_zone ? after(BusinessTime::Config.time_zone.now) : after(Time.now) end def after(time) diff --git a/lib/business_time/config.rb b/lib/business_time/config.rb index f841816..766eeda 100644 --- a/lib/business_time/config.rb +++ b/lib/business_time/config.rb @@ -7,6 +7,7 @@ module BusinessTime # manually, or with a yaml file and the load method. class Config DEFAULT_CONFIG = { + time_zone: nil, holidays: [], beginning_of_workday: '9:00 am', end_of_workday: '5:00 pm', @@ -37,6 +38,12 @@ def threadsafe_cattr_accessor(name) end end + # You can set this yourself, either by the load method below, or + # by saying + # BusinessTime::Config.time_zone = "UTC" + # someplace in the initializers of your application. + threadsafe_cattr_accessor :time_zone + # You can set this yourself, either by the load method below, or # by saying # BusinessTime::Config.beginning_of_workday = "8:30 am" @@ -73,6 +80,14 @@ def threadsafe_cattr_accessor(name) threadsafe_cattr_accessor :_weekdays # internal class << self + def time_zone + config[:time_zone] || Time.zone + end + + def time_zone=(zone) + config[:time_zone] = Time.find_zone!(zone) + end + def end_of_workday(day=nil) if day wday = work_hours[int_to_wday(day.wday)] @@ -120,7 +135,7 @@ def load(file) config = (data["business_time"] || {}) # load each config variable from the file, if it's present and valid - config_vars = %w(beginning_of_workday end_of_workday work_week work_hours) + config_vars = %w(time_zone beginning_of_workday end_of_workday work_week work_hours) config_vars.each do |var| send("#{var}=", config[var]) if config[var] && respond_to?("#{var}=") end diff --git a/lib/business_time/time_extensions.rb b/lib/business_time/time_extensions.rb index 3f88574..3356144 100644 --- a/lib/business_time/time_extensions.rb +++ b/lib/business_time/time_extensions.rb @@ -104,8 +104,8 @@ def work_hours_total(day) private def change_business_time time, hour, min=0, sec=0 - if Time.zone - time.in_time_zone(Time.zone).change(:hour => hour, :min => min, :sec => sec) + if BusinessTime::Config.time_zone + time.in_time_zone(BusinessTime::Config.time_zone).change(:hour => hour, :min => min, :sec => sec) else time.change(:hour => hour, :min => min, :sec => sec) end diff --git a/test/test_business_hours_time_zone_config.rb b/test/test_business_hours_time_zone_config.rb new file mode 100644 index 0000000..5876247 --- /dev/null +++ b/test/test_business_hours_time_zone_config.rb @@ -0,0 +1,26 @@ +require File.expand_path('../helper', __FILE__) + +describe "business hours" do + describe "with BusinessTime::Config.time_zone set to Eastern and Time.zone set to UTC" do + before do + BusinessTime::Config.time_zone = 'Eastern Time (US & Canada)' + Time.zone = 'UTC' + end + + it "uses BusinessTime::Config.time_zone" do + first = BusinessTime::Config.time_zone.parse("February 3rd, 2014, 3:30 pm") + later = 1.business_hour.after(first) + expected = BusinessTime::Config.time_zone.parse("February 3rd, 2014, 4:30 pm") + assert_equal expected, later + end + + it "converts Time.zone into BusinessTime::Config.time_zone" do + # EST = UTC-5 + # UTC = EST+5 + first = Time.zone.parse("February 3rd, 2014, 8:30 pm") + later = 1.business_hour.after(first) + expected = Time.zone.parse("February 3rd, 2014, 9:30 pm") + assert_equal expected, later + end + end +end diff --git a/test/test_config.rb b/test/test_config.rb index c4ba17a..3e8e565 100644 --- a/test/test_config.rb +++ b/test/test_config.rb @@ -1,6 +1,16 @@ require File.expand_path('../helper', __FILE__) describe "config" do + it "uses Time.zone when time zone is not set" do + Time.zone = "Darwin" + assert_equal "Darwin", BusinessTime::Config.time_zone.name + end + + it "keep track of time zone" do + BusinessTime::Config.time_zone = "Ljubljana" + assert_equal "Ljubljana", BusinessTime::Config.time_zone.name + end + it "keep track of the start of the day" do assert_equal "9:00 am", BusinessTime::Config.beginning_of_workday BusinessTime::Config.beginning_of_workday = "8:30 am" @@ -65,6 +75,7 @@ it "load config from YAML files" do yaml = <<-YAML business_time: + time_zone: UTC beginning_of_workday: 11:00 am end_of_workday: 2:00 pm work_week: @@ -74,6 +85,7 @@ YAML config_file = StringIO.new(yaml.gsub!(/^ /, '')) BusinessTime::Config.load(config_file) + assert_equal "UTC", BusinessTime::Config.time_zone.name assert_equal "11:00 am", BusinessTime::Config.beginning_of_workday assert_equal "2:00 pm", BusinessTime::Config.end_of_workday assert_equal ['mon'], BusinessTime::Config.work_week