business_time 0.4.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,5 +1,7 @@
1
1
  = business_time
2
2
 
3
+ {<img src="https://secure.travis-ci.org/bokmann/business_time.png" />}[http://travis-ci.org/bokmann/business_time]
4
+
3
5
  ActiveSupport gives us some great helpers so we can do things like:
4
6
 
5
7
  5.days.ago
@@ -80,6 +82,21 @@ I needed this, but taking into account business hours/days and holidays.
80
82
  friday = Date.parse("December 24, 2010")
81
83
  monday = Date.parse("December 27, 2010")
82
84
  friday.business_days_until(monday) #=> 1
85
+
86
+ # or you can calculate business duration between two Time objects
87
+ ticket_reported = Time.parse("February 3, 2012, 10:40 am")
88
+ ticket_resolved = Time.parse("February 4, 2012, 10:50 am")
89
+ ticket_reported.business_time_until(ticket_resolved) #=> 8.hours + 10.minutes
90
+
91
+ # note that counterintuitively, durations might not be quite what you expect when involving weekends.
92
+ # Consider the following example:
93
+ ticket_reported = Time.parse("February 3, 2012, 10:40 am")
94
+ ticket_resolved = Time.parse("February 4, 2012, 10:40 am")
95
+ ticket_reported.business_time_until(ticket_resolved) # will equal 6 hours and 20 minutes!
96
+
97
+ # why does this happen? Feb 4 2012 is a Saturday. That time will roll over to
98
+ # Monday, Feb 6th 2012, 9:00am. The business time between 10:40am friday and 9am monday is
99
+ # 6 hours and 20 minutes. From a quick inspection of the code, it looks like it should be 8 hours.
83
100
 
84
101
  == Usage in Rails
85
102
  The code above should work on a rails console without any issue. You will want to add a line something like:
@@ -114,6 +131,13 @@ This gem strives to be timezone-agnostic. Due to some complications in the hand
114
131
 
115
132
  This can lead to some wierd looking effects if, say, you are in the Eastern time zone but doing everything in UTC times... Your business day will appear to start and end at 9:00 and 5:00 UTC. If this seems perplexing to you, I can almost guarantee you are in over your head with timezones in other ways too, this is just the first place you encountered it. Timezone relative date handling gets more and more complicated every time you look at it and takes a long time before it starts to seem simple again. I'm hoping Arild and I write some good blog entries on the subject at http://blog.codesherpas.com.
116
133
 
134
+ == Integration with the Holidays gem
135
+
136
+ Chris Wise wrote up a great article[http://murmurinfo.wordpress.com/2012/01/11/handling-holidays-and-business-hours/] on using the business_time gem with the holidays[https://github.com/HealthyWeb/holidays] gem. It boils down to this:
137
+
138
+ Holidays.between(Date.civil(2011, 1, 1), 2.years.from_now, :ca_on, :o bserved).map{|holiday| BusinessTime::Config.holidays << holiday[:date]}
139
+
140
+
117
141
  == Contributors
118
142
  * David Bock http://github.com/bokmann
119
143
  * Enrico Bianco http://github.com/enricob
@@ -149,11 +173,7 @@ This can lead to some wierd looking effects if, say, you are in the Eastern time
149
173
  * While there have been requests to add 'business minutes' and even 'business seconds' to this gem, I won't
150
174
  entertain a pullup request with such things. If you find it useful, great. Most users won't, and they don't
151
175
  need the baggage.
152
- * I'm torn on the inclusion of some kind of business_duration_between two Times, for the following reasons:
153
- * As a duration, it will be in seconds. This would naturally be business-seconds, which I trimmed out above.
154
- * The logic is complex and error prone.
155
- * There is no logical place to put it, as most of this gem mirrors the Timehelpers in ActiveSupport.
156
- As a result I'm unlikely to add it unless I have an epiphany or someone wows me with an awesome pullup request.
176
+
157
177
 
158
178
  == Copyright
159
179
 
data/lib/business_time.rb CHANGED
@@ -1,8 +1,10 @@
1
+ require 'thread'
1
2
  require "active_support"
3
+ require 'active_support/time'
2
4
  require "time"
3
5
  require "business_time/config"
4
6
  require "business_time/business_hours"
5
7
  require "business_time/business_days"
6
8
  require "extensions/date"
7
9
  require "extensions/time"
8
- require "extensions/fixnum"
10
+ require "extensions/fixnum"
@@ -1,5 +1,7 @@
1
- module BusinessTime
1
+ require 'active_support/time'
2
2
 
3
+ module BusinessTime
4
+
3
5
  class BusinessDays
4
6
  def initialize(days)
5
7
  @days = days
@@ -31,6 +31,7 @@ module BusinessTime
31
31
  end
32
32
  after_time
33
33
  end
34
+ alias_method :since, :after
34
35
 
35
36
  def before(time)
36
37
  before_time = Time.roll_forward(time)
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext'
2
+
1
3
  module BusinessTime
2
4
 
3
5
  # controls the behavior of this gem. You can change
@@ -71,8 +73,7 @@ module BusinessTime
71
73
  self.end_of_workday = data["business_time"]["end_of_workday"]
72
74
  self.work_week = data["business_time"]["work_week"]
73
75
  data["business_time"]["holidays"].each do |holiday|
74
- self.holidays <<
75
- Time.zone ? Time.zone.parse(holiday) : Time.parse(holiday)
76
+ self.holidays << (Time.zone ? Time.zone.parse(holiday) : Time.parse(holiday))
76
77
  end
77
78
 
78
79
  end
@@ -1,3 +1,3 @@
1
1
  module BusinessTime
2
- VERSION = "0.4.0"
2
+ VERSION = "0.6.1"
3
3
  end
@@ -63,3 +63,42 @@ class Time
63
63
 
64
64
  end
65
65
  end
66
+
67
+ class Time
68
+
69
+ def business_time_until(to_time)
70
+
71
+ # Make sure that we will calculate time from A to B "clockwise"
72
+ direction = 1
73
+ if self < to_time
74
+ time_a = self
75
+ time_b = to_time
76
+ else
77
+ time_a = to_time
78
+ time_b = self
79
+ direction = -1
80
+ end
81
+
82
+ # Align both times to the closest business hours
83
+ time_a = Time::roll_forward(time_a)
84
+ time_b = Time::roll_forward(time_b)
85
+
86
+ # If same date, then calculate difference straight forward
87
+ if time_a.to_date == time_b.to_date
88
+ result = time_b - time_a
89
+ return result *= direction
90
+ end
91
+
92
+ # Both times are in different dates
93
+ result = Time.parse(time_a.strftime('%Y-%m-%d ') + BusinessTime::Config.end_of_workday) - time_a # First day
94
+ result += time_b - Time.parse(time_b.strftime('%Y-%m-%d ') + BusinessTime::Config.beginning_of_workday) # Last day
95
+
96
+ # All days in between
97
+ duration_of_working_day = Time.parse(BusinessTime::Config.end_of_workday) - Time.parse(BusinessTime::Config.beginning_of_workday)
98
+ result += (time_a.to_date.business_days_until(time_b.to_date) - 1) * duration_of_working_day
99
+
100
+ # Make sure that sign is correct
101
+ result *= direction
102
+ end
103
+
104
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: business_time
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 5
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 4
9
- - 0
10
- version: 0.4.0
8
+ - 6
9
+ - 1
10
+ version: 0.6.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - bokmann
@@ -15,7 +15,8 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-25 00:00:00 Z
18
+ date: 2012-04-12 00:00:00 -04:00
19
+ default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: activesupport
@@ -25,18 +26,50 @@ dependencies:
25
26
  requirements:
26
27
  - - ">="
27
28
  - !ruby/object:Gem::Version
28
- hash: 15
29
+ hash: 3
29
30
  segments:
30
- - 2
31
- - 0
31
+ - 3
32
+ - 1
32
33
  - 0
33
- version: 2.0.0
34
+ version: 3.1.0
34
35
  type: :runtime
35
36
  version_requirements: *id001
36
37
  - !ruby/object:Gem::Dependency
37
- name: rake
38
+ name: tzinfo
38
39
  prerelease: false
39
40
  requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 45
46
+ segments:
47
+ - 0
48
+ - 3
49
+ - 31
50
+ version: 0.3.31
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rake
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 63
62
+ segments:
63
+ - 0
64
+ - 9
65
+ - 2
66
+ version: 0.9.2
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: shoulda
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
40
73
  none: false
41
74
  requirements:
42
75
  - - ">="
@@ -46,11 +79,11 @@ dependencies:
46
79
  - 0
47
80
  version: "0"
48
81
  type: :development
49
- version_requirements: *id002
82
+ version_requirements: *id004
50
83
  - !ruby/object:Gem::Dependency
51
- name: shoulda
84
+ name: rdoc
52
85
  prerelease: false
53
- requirement: &id003 !ruby/object:Gem::Requirement
86
+ requirement: &id005 !ruby/object:Gem::Requirement
54
87
  none: false
55
88
  requirements:
56
89
  - - ">="
@@ -60,7 +93,7 @@ dependencies:
60
93
  - 0
61
94
  version: "0"
62
95
  type: :development
63
- version_requirements: *id003
96
+ version_requirements: *id005
64
97
  description: Have you ever wanted to do things like "6.business_days.from_now" and have weekends and holidays taken into account? Now you can.
65
98
  email: dbock@codesherpas.com
66
99
  executables: []
@@ -84,6 +117,7 @@ files:
84
117
  - rails_generators/business_time_config/business_time_config_generator.rb
85
118
  - rails_generators/business_time_config/templates/business_time.rb
86
119
  - rails_generators/business_time_config/templates/business_time.yml
120
+ has_rdoc: true
87
121
  homepage: http://github.com/bokmann/business_time
88
122
  licenses: []
89
123
 
@@ -113,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
147
  requirements: []
114
148
 
115
149
  rubyforge_project:
116
- rubygems_version: 1.8.10
150
+ rubygems_version: 1.6.2
117
151
  signing_key:
118
152
  specification_version: 3
119
153
  summary: Support for doing time math in business hours and days