business_time 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -70,6 +70,11 @@ I needed this, but taking into account business hours/days and holidays.
70
70
  friday_afternoon = Time.parse("July 2nd, 2010, 4:50 pm")
71
71
  tuesday_morning = 1.business_hour.after(friday_afternoon)
72
72
 
73
+ # you can also calculate business duration between two dates
74
+ friday = Date.parse("December 24, 2010")
75
+ monday = Date.parse("December 27, 2010")
76
+ friday.business_days_until(monday) #=> 1
77
+
73
78
  == Usage in Rails
74
79
  The code above should work on a rails console without any issue. You will want to add a line something like:
75
80
 
@@ -94,8 +99,12 @@ This code does depend on ActiveSupport, but nothing else within rails. Even the
94
99
 
95
100
  == Timezone support
96
101
  This gem strives to be timezone-agnostic. Due to some complications in the handling of timezones in the built in Time class, and some complexities (bugs?) in the timeWithZone class, this was harder than expected... but here's the idea:
97
- * When you configure the gem with something like 9:00am as the start time, this is agnostic of time zone.
98
- * When you are dealing with a Time or TimeWithZone class, the timezone is preserved and the beginning and end of times for the business day are referenced in that time zone.
102
+
103
+ * When you configure the gem with something like 9:00am as the start time,
104
+ this is agnostic of time zone.
105
+ * When you are dealing with a Time or TimeWithZone class, the timezone is
106
+ preserved and the beginning and end of times for the business day are
107
+ referenced in that time zone.
99
108
 
100
109
  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.
101
110
 
@@ -103,6 +112,7 @@ This can lead to some wierd looking effects if, say, you are in the Eastern time
103
112
  * David Bock http://github.com/bokmann
104
113
  * Enrico Bianco http://github.com/enricob
105
114
  * Arild Shirazi http://github.com/ashirazi
115
+ * Piotr Jakubowski https://github.com/piotrj
106
116
 
107
117
  (Special thanks for Arild on the complexities of dealing with TimeWithZone)
108
118
 
@@ -117,9 +127,32 @@ This can lead to some wierd looking effects if, say, you are in the Eastern time
117
127
  * Send me a pull request. Bonus points for topic branches.
118
128
 
119
129
  == TODO
120
- * I'd like to return ActiveSupport::TimeWithZone just like the equivalent ActiveSupport helpers do.
121
- * if it doesn't pollute the logic too much, I'd like to vary the days counted as 'business days'. Bakers often don't work on Mondays, for instance.
122
130
 
131
+ * if it doesn't pollute the logic too much, I'd like to vary the days counted as 'business days'. Bakers often
132
+ don't work on Mondays, for instance. I'd do it in something like this:
133
+
134
+ BusinessTime::Config.work_week = [:tue, :wed, :thur, :fri, :sat]
135
+
136
+ * Arild has pointed out that there may be some logical inconsistencies
137
+ regaring the beginning_of_workday and end_of workday times not actually
138
+ being considered inside of the workday. I'd like to make sure that they
139
+ work as if the beginning_of_workday is included and the end_of_workday is
140
+ not included, just like the '...' range operator in Ruby.
141
+
142
+ == NOT TODO
143
+
144
+ * I spent way too much time in my previous java-programmer life building frameworks that worshipped complexity,
145
+ always trying to give the developer-user ultimate flexibility at the expense of the 'surface area' of the api.
146
+ Never again - I will sooner limit functionality to 80% so that something stays usable and let people fork.
147
+ * While there have been requests to add 'business minutes' and even 'business seconds' to this gem, I won't
148
+ entertain a pullup request with such things. If you find it useful, great. Most users won't, and they don't
149
+ need the baggage.
150
+ * I'm torn on the inclusion of some kind of business_duration_between two Times, for the following reasons:
151
+ * As a duration, it will be in seconds. This would naturally be business-seconds, which I trimmed out above.
152
+ * The logic is complex and error prone.
153
+ * There is no logical place to put it, as most of this gem mirrors the Timehelpers in ActiveSupport.
154
+ As a result I'm unlikely to add it unless I have an epiphany or someone wows me with an awesome pullup request.
155
+
123
156
  == Copyright
124
157
 
125
158
  Copyright (c) 2010 bokmann. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
@@ -7,4 +7,8 @@ class Date
7
7
  def weekday?
8
8
  [1,2,3,4,5].include? self.wday
9
9
  end
10
- end
10
+
11
+ def business_days_until(to_date)
12
+ (self...to_date).select{ |day| day.workday? }.size
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ require 'helper'
2
+
3
+ class TestCalculatingBusinessDuration < Test::Unit::TestCase
4
+ should "properly calculate business duration over weekends" do
5
+ friday = Date.parse("December 24, 2010")
6
+ monday = Date.parse("December 27, 2010")
7
+ assert_equal friday.business_days_until(monday), 1
8
+ end
9
+
10
+ should "properly calculate business duration without weekends" do
11
+ monday = Date.parse("December 20, 2010")
12
+ wednesday = Date.parse("December 22, 2010")
13
+ assert_equal monday.business_days_until(wednesday), 2
14
+ end
15
+
16
+ should "properly calculate business duration with respect to holidays" do
17
+ free_friday = Date.parse("December 17, 2010")
18
+ wednesday = Date.parse("December 15,2010")
19
+ monday = Date.parse("December 20, 2010")
20
+ BusinessTime::Config.holidays << free_friday
21
+ assert_equal wednesday.business_days_until(monday), 2
22
+ end
23
+
24
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 0
9
- version: 0.3.0
8
+ - 1
9
+ version: 0.3.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - bokmann
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-30 00:00:00 -04:00
17
+ date: 2010-12-22 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -77,6 +77,7 @@ files:
77
77
  - test/test_business_hours.rb
78
78
  - test/test_business_hours_eastern.rb
79
79
  - test/test_business_hours_utc.rb
80
+ - test/test_calculating_business_duration.rb
80
81
  - test/test_config.rb
81
82
  - test/test_date_extensions.rb
82
83
  - test/test_fixnum_extensions.rb
@@ -120,6 +121,7 @@ test_files:
120
121
  - test/test_business_hours.rb
121
122
  - test/test_business_hours_eastern.rb
122
123
  - test/test_business_hours_utc.rb
124
+ - test/test_calculating_business_duration.rb
123
125
  - test/test_config.rb
124
126
  - test/test_date_extensions.rb
125
127
  - test/test_fixnum_extensions.rb