business_time 0.9.3 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 5fe356eb4f4e620e3f3db2aa9242df1c63213500
4
- data.tar.gz: d1f662a4f576662e3f6d140970d5b2191016fda8
2
+ SHA256:
3
+ metadata.gz: 1c76c5a8e2c840f078e70de2e996a33bc11106501cccca459f1e77ce236bf587
4
+ data.tar.gz: 8d41d5e765ff55f6158049fabd41039c6acb121e2c2be834d579f60a0fa6a3ed
5
5
  SHA512:
6
- metadata.gz: 86434d31fbf26aa3372510db883266643431a449fa2980cee43e9d34198c140a560d0606d41911aa0a47e030b373eeba0442f38dad07c36a5b5113a811c97213
7
- data.tar.gz: 5c1ae7866418a21538030ba2129698eaafb2016b5d715cd2c13e31e1739b8732ff625af6f87e48e98bbe697ea06005d74c35bfab7c8610b306f7ba7c9d0121b5
6
+ metadata.gz: 46bececd4f088d2a50a061f83510e0e998f427e4fe7ef49d304a8949b7f42f8a1265689f2f83c972829d3a175813dc3a79956b117aa8719a4203b5afd86ab9c6
7
+ data.tar.gz: 42316dc95c99be2600588ad5a2d765a97b79abf9369c0d41235f375c3e7b6ee3ae63318da62a0a20b0a989499ae9527263da3b7184a65e21c9d2e5bc6b0db2e1
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009-2016 bokmann
1
+ Copyright (c) 2009-2022 bokmann
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -37,6 +37,9 @@ module BusinessTime
37
37
  end
38
38
 
39
39
  def calculate_after(time, days)
40
+ if (time.is_a?(Time) || time.is_a?(DateTime)) && !time.workday?
41
+ time = Time.beginning_of_workday(time)
42
+ end
40
43
  while days > 0 || !time.workday?
41
44
  days -= 1 if time.workday?
42
45
  time += 1.day
@@ -50,6 +53,9 @@ module BusinessTime
50
53
  end
51
54
 
52
55
  def calculate_before(time, days)
56
+ if (time.is_a?(Time) || time.is_a?(DateTime)) && !time.workday?
57
+ time = Time.beginning_of_workday(time)
58
+ end
53
59
  while days > 0 || !time.workday?
54
60
  days -= 1 if time.workday?
55
61
  time -= 1.day
@@ -7,7 +7,7 @@ module BusinessTime
7
7
  # manually, or with a yaml file and the load method.
8
8
  class Config
9
9
  DEFAULT_CONFIG = {
10
- holidays: SortedSet.new,
10
+ holidays: Set.new,
11
11
  beginning_of_workday: ParsedTime.parse('9:00 am'),
12
12
  end_of_workday: ParsedTime.parse('5:00 pm'),
13
13
  work_week: %w(mon tue wed thu fri),
@@ -80,23 +80,7 @@ module BusinessTime
80
80
  end
81
81
  end
82
82
 
83
- # You can set this yourself, either by the load method below, or
84
- # by saying
85
- # BusinessTime::Config.beginning_of_workday = "8:30 am"
86
- # someplace in the initializers of your application.
87
- threadsafe_cattr_reader :beginning_of_workday
88
-
89
- # You can set this yourself, either by the load method below, or
90
- # by saying
91
- # BusinessTime::Config.end_of_workday = "5:30 pm"
92
- # someplace in the initializers of your application.
93
- threadsafe_cattr_reader :end_of_workday
94
-
95
- # You can set this yourself, either by the load method below, or
96
- # by saying
97
- # BusinessTime::Config.work_week = [:sun, :mon, :tue, :wed, :thu]
98
- # someplace in the initializers of your application.
99
- threadsafe_cattr_accessor :work_week
83
+ threadsafe_cattr_reader :work_week
100
84
 
101
85
  # You can set this yourself, either by the load method below, or
102
86
  # by saying
@@ -118,6 +102,10 @@ module BusinessTime
118
102
  threadsafe_cattr_accessor :fiscal_month_offset
119
103
 
120
104
  class << self
105
+ # You can set this yourself, either by the load method below, or
106
+ # by saying
107
+ # BusinessTime::Config.end_of_workday = "5:30 pm"
108
+ # someplace in the initializers of your application.
121
109
  def end_of_workday(day=nil)
122
110
  if day
123
111
  wday = work_hours[int_to_wday(day.wday)]
@@ -127,6 +115,10 @@ module BusinessTime
127
115
  end
128
116
  end
129
117
 
118
+ # You can set this yourself, either by the load method below, or
119
+ # by saying
120
+ # BusinessTime::Config.beginning_of_workday = "8:30 am"
121
+ # someplace in the initializers of your application.
130
122
  def beginning_of_workday(day=nil)
131
123
  if day
132
124
  wday = work_hours[int_to_wday(day.wday)]
@@ -136,6 +128,10 @@ module BusinessTime
136
128
  end
137
129
  end
138
130
 
131
+ # You can set this yourself, either by the load method below, or
132
+ # by saying
133
+ # BusinessTime::Config.work_week = [:sun, :mon, :tue, :wed, :thu]
134
+ # someplace in the initializers of your application.
139
135
  def work_week=(days)
140
136
  config[:work_week] = days
141
137
  self._weekdays = nil
@@ -148,7 +144,7 @@ module BusinessTime
148
144
  wday_to_int(day_name)
149
145
  end.compact
150
146
 
151
- self._weekdays = SortedSet.new(days)
147
+ self._weekdays = days.sort.to_set
152
148
  end
153
149
 
154
150
  # loads the config data from a yaml file written as:
@@ -190,13 +186,18 @@ module BusinessTime
190
186
 
191
187
  private
192
188
 
189
+ DAY_NAMES = [
190
+ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
191
+ ]
192
+ private_constant :DAY_NAMES
193
+
193
194
  def wday_to_int day_name
194
- lowercase_day_names = ::Time::RFC2822_DAY_NAME.map(&:downcase)
195
+ lowercase_day_names = DAY_NAMES.map(&:downcase)
195
196
  lowercase_day_names.find_index(day_name.to_s.downcase)
196
197
  end
197
198
 
198
199
  def int_to_wday num
199
- ::Time::RFC2822_DAY_NAME.map(&:downcase).map(&:to_sym)[num]
200
+ DAY_NAMES.map(&:downcase).map(&:to_sym)[num]
200
201
  end
201
202
 
202
203
  def reset
@@ -1,3 +1,3 @@
1
1
  module BusinessTime
2
- VERSION = "0.9.3"
2
+ VERSION = "0.12.0"
3
3
  end
data/lib/business_time.rb CHANGED
@@ -1,9 +1,10 @@
1
- require 'thread'
2
- require 'active_support'
3
- require 'active_support/time'
1
+ require 'set'
4
2
  require 'time'
5
3
  require 'yaml'
6
4
 
5
+ require 'active_support'
6
+ require 'active_support/time'
7
+
7
8
  require 'business_time/parsed_time'
8
9
  require 'business_time/version'
9
10
  require 'business_time/config'
@@ -1,21 +1,21 @@
1
1
  module BusinessTime
2
2
  module Generators
3
3
  class ConfigGenerator < Rails::Generators::Base # :nodoc:
4
-
4
+
5
5
  def self.gem_root
6
- File.expand_path("../../../..", __FILE__)
6
+ File.expand_path("../../..", __dir__)
7
7
  end
8
8
 
9
9
  def self.source_root
10
10
  # Use the templates from the 2.3.x generator
11
11
  File.join(gem_root, 'rails_generators', 'business_time_config', 'templates')
12
12
  end
13
-
13
+
14
14
  def generate
15
15
  template 'business_time.rb', File.join('config', 'initializers', 'business_time.rb')
16
16
  template 'business_time.yml', File.join('config', 'business_time.yml')
17
17
  end
18
-
18
+
19
19
  end
20
20
  end
21
- end
21
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: business_time
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - bokmann
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-04 00:00:00.000000000 Z
11
+ date: 2022-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -102,7 +102,6 @@ extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
104
  - LICENSE
105
- - README.rdoc
106
105
  - lib/business_time.rb
107
106
  - lib/business_time/business_days.rb
108
107
  - lib/business_time/business_hours.rb
@@ -122,7 +121,7 @@ homepage: https://github.com/bokmann/business_time
122
121
  licenses:
123
122
  - MIT
124
123
  metadata: {}
125
- post_install_message:
124
+ post_install_message:
126
125
  rdoc_options: []
127
126
  require_paths:
128
127
  - lib
@@ -137,9 +136,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
136
  - !ruby/object:Gem::Version
138
137
  version: '0'
139
138
  requirements: []
140
- rubyforge_project:
141
- rubygems_version: 2.6.13
142
- signing_key:
139
+ rubygems_version: 3.3.12
140
+ signing_key:
143
141
  specification_version: 4
144
142
  summary: Support for doing time math in business hours and days
145
143
  test_files: []
data/README.rdoc DELETED
@@ -1,223 +0,0 @@
1
- = business_time
2
-
3
- {<img src="https://secure.travis-ci.org/bokmann/business_time.png" />}[http://travis-ci.org/bokmann/business_time]
4
-
5
- ActiveSupport gives us some great helpers so we can do things like:
6
-
7
- 5.days.ago
8
-
9
- and
10
-
11
- 8.hours.from_now
12
-
13
- as well as helpers to do that from any provided date or time.
14
-
15
- I needed this, but taking into account business hours/days and holidays.
16
-
17
- == Usage
18
- === install the gem
19
-
20
- gem install business_time
21
-
22
- === open up your console
23
-
24
- # if in irb, add these lines:
25
-
26
- require 'business_time'
27
-
28
- # try these examples, using the current time:
29
-
30
- 1.business_hour.from_now
31
- 4.business_hours.from_now
32
- 8.business_hours.from_now
33
-
34
- 1.business_hour.ago
35
- 4.business_hours.ago
36
- 8.business_hours.ago
37
-
38
- 1.business_day.from_now
39
- 4.business_days.from_now
40
- 8.business_days.from_now
41
-
42
- 1.business_day.ago
43
- 4.business_days.ago
44
- 8.business_days.ago
45
-
46
- Date.today.workday?
47
- Date.parse("2015-12-09").workday?
48
- Date.parse("2015-12-12").workday?
49
-
50
- And we can do it from any Date or Time object.
51
- my_birthday = Date.parse("August 4th, 1969")
52
- 8.business_days.after(my_birthday)
53
- 8.business_days.before(my_birthday)
54
-
55
- my_birthday = Time.parse("August 4th, 1969, 8:32 am")
56
- 8.business_days.after(my_birthday)
57
- 8.business_days.before(my_birthday)
58
-
59
-
60
- We can adjust the start and end time of our business hours
61
- BusinessTime::Config.beginning_of_workday = "8:30 am"
62
- BusinessTime::Config.end_of_workday = "5:30 pm"
63
-
64
- and we can add holidays that don't count as business days
65
- July 5 in 2010 is a monday that the U.S. takes off because our independence day falls on that Sunday.
66
- three_day_weekend = Date.parse("July 5th, 2010")
67
- BusinessTime::Config.holidays << three_day_weekend
68
- friday_afternoon = Time.parse("July 2nd, 2010, 4:50 pm")
69
- tuesday_morning = 1.business_hour.after(friday_afternoon)
70
-
71
- plus, we can change the work week:
72
- # July 9th in 2010 is a Friday.
73
- BusinessTime::Config.work_week = [:sun, :mon, :tue, :wed, :thu]
74
- thursday_afternoon = Time.parse("July 8th, 2010, 4:50 pm")
75
- sunday_morning = 1.business_hour.after(thursday_afternoon)
76
-
77
- As alternative we also can change the business hours for each work day:
78
- BusinessTime::Config.work_hours = {
79
- :mon=>["9:00","17:00"],
80
- :fri=>["9:00","17:00"],
81
- :sat=>["10:00","15:00"]
82
- }
83
- friday = Time.parse("December 24, 2010 15:00")
84
- monday = Time.parse("December 27, 2010 11:00")
85
- working_hours = friday.business_time_until(monday) # 9.hours
86
-
87
- You can also calculate business duration between two dates
88
- friday = Date.parse("December 24, 2010")
89
- monday = Date.parse("December 27, 2010")
90
- friday.business_days_until(monday) #=> 1
91
-
92
- Or you can calculate business duration between two Time objects
93
- ticket_reported = Time.parse("February 3, 2012, 10:40 am")
94
- ticket_resolved = Time.parse("February 4, 2012, 10:50 am")
95
- ticket_reported.business_time_until(ticket_resolved) #=> 8.hours + 10.minutes
96
-
97
- You can also determine if a given time is within business hours
98
- Time.parse("February 3, 2012, 10:00 am").during_business_hours?
99
-
100
- Note that counterintuitively, durations might not be quite what you expect when involving weekends.
101
- Consider the following example:
102
- ticket_reported = Time.parse("February 3, 2012, 10:40 am")
103
- ticket_resolved = Time.parse("February 4, 2012, 10:40 am")
104
- ticket_reported.business_time_until(ticket_resolved) # will equal 6 hours and 20 minutes!
105
-
106
- Why does this happen? Feb 4 2012 is a Saturday. That time will roll over to
107
- Monday, Feb 6th 2012, 9:00am. The business time between 10:40am friday and 9am monday is
108
- 6 hours and 20 minutes. From a quick inspection of the code, it looks like it should be 8 hours.
109
-
110
- Or you can calculate business dates between two dates
111
- monday = Date.parse("December 20, 2010")
112
- wednesday = Date.parse("December 22, 2010")
113
- monday.business_dates_until(wednesday) #=> [Mon, 20 Dec 2010, Tue, 21 Dec 2010]
114
-
115
- You can get the first workday after a time or return itself if it is a workday
116
- saturday = Time.parse("Sat Aug 9, 18:00:00, 2014")
117
- monday = Time.parse("Mon Aug 11, 18:00:00, 2014")
118
- Time.first_business_day(saturday) #=> "Mon Aug 11, 18:00:00, 2014"
119
- Time.first_business_day(monday) #=> "Mon Aug 11, 18:00:00, 2014"
120
-
121
- # similar to Time#first_business_day Time#previous_business_day only cares about
122
- # workdays:
123
- saturday = Time.parse("Sat Aug 9, 18:00:00, 2014")
124
- monday = Time.parse("Mon Aug 11, 18:00:00, 2014")
125
- Time.previous_business_day(saturday) #=> "Fri Aug 8, 18:00:00, 2014"
126
- Time.previous_business_day(monday) #=> "Mon Aug 11, 18:00:00, 2014"
127
- == Rails generator
128
-
129
- rails generate business_time:config
130
-
131
- The generator will add a ./config/business_time.yml and a ./config/initializers/business_time.rb
132
- file that will cause the start of business day, the end of business day, and your holidays to be loaded from the yaml file.
133
-
134
- You might want to programatically load your holidays from a database table,
135
- but you will want to pay attention to how the initializer works -
136
- you will want to make sure that the initializer sets stuff up appropriately so
137
- rails instances on mongrels or passenger will have the appropriate data as they come up and down.
138
-
139
- == Timezone support
140
- This gem strives to be timezone-agnostic.
141
- Due to some complications in the handling of timezones in the built in Time class,
142
- and some complexities (bugs?) in the timeWithZone class, this was harder than expected... but here's the idea:
143
-
144
- * When you configure the gem with something like 9:00am as the start time,
145
- this is agnostic of time zone.
146
- * When you are dealing with a Time or TimeWithZone class, the timezone is
147
- preserved and the beginning and end of times for the business day are
148
- referenced in that time zone.
149
-
150
- This can lead to some weird looking effects if, say, you are in the Eastern time zone but doing everything in UTC times...
151
- Your business day will appear to start and end at 9:00 and 5:00 UTC.
152
- If this seems perplexing to you, I can almost guarantee you are in over your head with timezones in other ways too,
153
- this is just the first place you encountered it.
154
- 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.
155
-
156
- == Integration with the Holidays gem
157
-
158
- Chris Wise wrote up a great article[http://murmurinfo.wordpress.com/2012/01/11/handling-holidays-and-business-hours/]
159
- on using the business_time gem with the holidays[https://github.com/alexdunae/holidays] gem. It boils down to this:
160
-
161
- Holidays.between(Date.civil(2013, 1, 1), 2.years.from_now, :ca_on, :observed).map do |holiday|
162
- BusinessTime::Config.holidays << holiday[:date]
163
- # Implement long weekends if they apply to the region, eg:
164
- # BusinessTime::Config.holidays << holiday[:date].next_week if !holiday[:date].weekday?
165
- end
166
-
167
- == Contributors
168
- * David Bock http://github.com/bokmann
169
- * Enrico Bianco http://github.com/enricob
170
- * Arild Shirazi http://github.com/ashirazi
171
- * Piotr Jakubowski http://github.com/piotrj
172
- * Glenn Vanderburg http://github.com/glv
173
- * Michael Grosser http://github.com/grosser
174
- * Michael Curtis http://github.com/mcurtis
175
- * Brian Ewins http://github.com/bazzargh
176
-
177
- (Special thanks for Arild on the complexities of dealing with TimeWithZone)
178
-
179
- == Note on Patches/Pull Requests
180
-
181
- * Fork the project.
182
- * Make your feature addition or bug fix.
183
- * Add tests for it. This is important so I don't break it in a
184
- future version unintentionally.
185
- * Commit, do not mess with rakefile, version, or history.
186
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
187
- * Send me a pull request. Bonus points for topic branches.
188
-
189
- == TODO
190
-
191
- * Arild has pointed out that there may be some logical inconsistencies
192
- regarding the beginning_of_workday and end_of workday times not actually
193
- being considered inside of the workday. I'd like to make sure that they
194
- work as if the beginning_of_workday is included and the end_of_workday is
195
- not included, just like the '...' range operator in Ruby.
196
-
197
- == NOT TODO
198
-
199
- * I spent way too much time in my previous java-programmer life building frameworks that worshipped complexity,
200
- always trying to give the developer-user ultimate flexibility at the expense of the 'surface area' of the api.
201
- Never again - I will sooner limit functionality to 80% so that something stays usable and let people fork.
202
- * While there have been requests to add 'business minutes' and even 'business seconds' to this gem, I won't
203
- entertain a pull request with such things. If you find it useful, great. Most users won't, and they don't
204
- need the baggage.
205
-
206
-
207
- == A note on stability and change
208
-
209
- Sometimes people ask me why this gem doesn't release more often. My opinions on that are best discussed in person in a friendly discussion, but I'll attempt some of that here.
210
-
211
- First, a big part of the reason is that the projects I do use this gem on are happy with it's current functionality. It is 'suitable for the purpose' for which I released it, and as such, maintenance I do on this gem is a gift to the community.
212
-
213
- Second, out of the ~1.3 million downloads (according to rubygems.org), the number of real 'issues' with this gem have been minimal. Most of the issues that are opened are really people with slightly different requirements than I have regarding whether 'off hours' work counts as the previous or the next business day, a disagreement on the semantics of days vs. hours, etc. I take care to try to explain these choices in the open issues, but to my mind, they aren't true issues if it's just a difference of opinion. Even so, I'll gladly accept pull requests that resolve this difference of opinion as a configuration option... just don't expect me to do your job for you. I've already given you 90% of what you need.
214
-
215
- Third, a business time gem is, well, relevant to businesses. Many businesses don't move quickly. My government clients move even more slowly. Stability is favored in these environments.
216
-
217
- Fourth, new features can wait. To the person that adds them they can be mission critical, but with modern packaging processes, they can use their version without waiting for their changes to be included in the upstream version. Their changes don't break your code.
218
-
219
- I'm proud of the work in this gem; the stability is a big part of that. This gem has lived longer than many others that have attempted to do the same thing. I expect it to be here chugging away when Ruby has become the next COBOL.
220
-
221
- == Copyright
222
-
223
- Copyright (c) 2010-2017 bokmann. See LICENSE for details.