business_time 0.11.0 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/lib/business_time/config.rb +2 -2
- data/lib/business_time/version.rb +1 -1
- data/lib/business_time.rb +4 -3
- data/lib/generators/business_time/config_generator.rb +5 -5
- metadata +3 -18
- data/README.rdoc +0 -229
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c76c5a8e2c840f078e70de2e996a33bc11106501cccca459f1e77ce236bf587
|
4
|
+
data.tar.gz: 8d41d5e765ff55f6158049fabd41039c6acb121e2c2be834d579f60a0fa6a3ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46bececd4f088d2a50a061f83510e0e998f427e4fe7ef49d304a8949b7f42f8a1265689f2f83c972829d3a175813dc3a79956b117aa8719a4203b5afd86ab9c6
|
7
|
+
data.tar.gz: 42316dc95c99be2600588ad5a2d765a97b79abf9369c0d41235f375c3e7b6ee3ae63318da62a0a20b0a989499ae9527263da3b7184a65e21c9d2e5bc6b0db2e1
|
data/LICENSE
CHANGED
data/lib/business_time/config.rb
CHANGED
@@ -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:
|
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),
|
@@ -144,7 +144,7 @@ module BusinessTime
|
|
144
144
|
wday_to_int(day_name)
|
145
145
|
end.compact
|
146
146
|
|
147
|
-
self._weekdays =
|
147
|
+
self._weekdays = days.sort.to_set
|
148
148
|
end
|
149
149
|
|
150
150
|
# loads the config data from a yaml file written as:
|
data/lib/business_time.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
-
require '
|
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("
|
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.
|
4
|
+
version: 0.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- bokmann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -38,20 +38,6 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: sorted_set
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :runtime
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: rake
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -116,7 +102,6 @@ extensions: []
|
|
116
102
|
extra_rdoc_files: []
|
117
103
|
files:
|
118
104
|
- LICENSE
|
119
|
-
- README.rdoc
|
120
105
|
- lib/business_time.rb
|
121
106
|
- lib/business_time/business_days.rb
|
122
107
|
- lib/business_time/business_hours.rb
|
@@ -151,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
151
136
|
- !ruby/object:Gem::Version
|
152
137
|
version: '0'
|
153
138
|
requirements: []
|
154
|
-
rubygems_version: 3.
|
139
|
+
rubygems_version: 3.3.12
|
155
140
|
signing_key:
|
156
141
|
specification_version: 4
|
157
142
|
summary: Support for doing time math in business hours and days
|
data/README.rdoc
DELETED
@@ -1,229 +0,0 @@
|
|
1
|
-
= business_time
|
2
|
-
|
3
|
-
{<img src="https://github.com/bokmann/business_time/workflows/CI/badge.svg" />}[https://github.com/bokmann/business_time/actions?query=workflow%3ACI]
|
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
|
-
Or we can temporarily override the configured values
|
65
|
-
BusinessTime::Config.with(beginning_of_workday: "8 am", end_of_workday: "6 pm") do
|
66
|
-
1.business_hour.from_now
|
67
|
-
end
|
68
|
-
|
69
|
-
and we can add holidays that don't count as business days
|
70
|
-
July 5 in 2010 is a monday that the U.S. takes off because our independence day falls on that Sunday.
|
71
|
-
three_day_weekend = Date.parse("July 5th, 2010")
|
72
|
-
BusinessTime::Config.holidays << three_day_weekend
|
73
|
-
friday_afternoon = Time.parse("July 2nd, 2010, 4:50 pm")
|
74
|
-
tuesday_morning = 1.business_hour.after(friday_afternoon)
|
75
|
-
|
76
|
-
plus, we can change the work week:
|
77
|
-
# July 9th in 2010 is a Friday.
|
78
|
-
BusinessTime::Config.work_week = [:sun, :mon, :tue, :wed, :thu]
|
79
|
-
thursday_afternoon = Time.parse("July 8th, 2010, 4:50 pm")
|
80
|
-
sunday_morning = 1.business_hour.after(thursday_afternoon)
|
81
|
-
|
82
|
-
As alternative we also can change the business hours for each work day:
|
83
|
-
BusinessTime::Config.work_hours = {
|
84
|
-
:mon=>["9:00","17:00"],
|
85
|
-
:fri=>["9:00","17:00"],
|
86
|
-
:sat=>["10:00","15:00"]
|
87
|
-
}
|
88
|
-
friday = Time.parse("December 24, 2010 15:00")
|
89
|
-
monday = Time.parse("December 27, 2010 11:00")
|
90
|
-
working_hours = friday.business_time_until(monday) # 9.hours
|
91
|
-
|
92
|
-
You can also calculate business duration between two dates
|
93
|
-
friday = Date.parse("December 24, 2010")
|
94
|
-
monday = Date.parse("December 27, 2010")
|
95
|
-
friday.business_days_until(monday) #=> 1
|
96
|
-
|
97
|
-
Or you can calculate business duration between two Time objects
|
98
|
-
ticket_reported = Time.parse("February 3, 2012, 10:40 am")
|
99
|
-
ticket_resolved = Time.parse("February 4, 2012, 10:50 am")
|
100
|
-
ticket_reported.business_time_until(ticket_resolved) #=> 8.hours + 10.minutes
|
101
|
-
|
102
|
-
You can also determine if a given time is within business hours
|
103
|
-
Time.parse("February 3, 2012, 10:00 am").during_business_hours?
|
104
|
-
|
105
|
-
Note that counterintuitively, durations might not be quite what you expect when involving weekends.
|
106
|
-
Consider the following example:
|
107
|
-
ticket_reported = Time.parse("February 3, 2012, 10:40 am")
|
108
|
-
ticket_resolved = Time.parse("February 4, 2012, 10:40 am")
|
109
|
-
ticket_reported.business_time_until(ticket_resolved) # will equal 6 hours and 20 minutes!
|
110
|
-
|
111
|
-
Why does this happen? Feb 4 2012 is a Saturday. That time will roll over to
|
112
|
-
Monday, Feb 6th 2012, 9:00am. The business time between 10:40am friday and 9am monday is
|
113
|
-
6 hours and 20 minutes. From a quick inspection of the code, it looks like it should be 8 hours.
|
114
|
-
|
115
|
-
Or you can calculate business dates between two dates
|
116
|
-
monday = Date.parse("December 20, 2010")
|
117
|
-
wednesday = Date.parse("December 22, 2010")
|
118
|
-
monday.business_dates_until(wednesday) #=> [Mon, 20 Dec 2010, Tue, 21 Dec 2010]
|
119
|
-
|
120
|
-
You can get the first workday after a time or return itself if it is a workday
|
121
|
-
saturday = Time.parse("Sat Aug 9, 18:00:00, 2014")
|
122
|
-
monday = Time.parse("Mon Aug 11, 18:00:00, 2014")
|
123
|
-
Time.first_business_day(saturday) #=> "Mon Aug 11, 18:00:00, 2014"
|
124
|
-
Time.first_business_day(monday) #=> "Mon Aug 11, 18:00:00, 2014"
|
125
|
-
|
126
|
-
# similar to Time#first_business_day Time#previous_business_day only cares about
|
127
|
-
# workdays:
|
128
|
-
saturday = Time.parse("Sat Aug 9, 18:00:00, 2014")
|
129
|
-
monday = Time.parse("Mon Aug 11, 18:00:00, 2014")
|
130
|
-
Time.previous_business_day(saturday) #=> "Fri Aug 8, 18:00:00, 2014"
|
131
|
-
Time.previous_business_day(monday) #=> "Mon Aug 11, 18:00:00, 2014"
|
132
|
-
== Rails generator
|
133
|
-
|
134
|
-
rails generate business_time:config
|
135
|
-
|
136
|
-
The generator will add a ./config/business_time.yml and a ./config/initializers/business_time.rb
|
137
|
-
file that will cause the start of business day, the end of business day, and your holidays to be loaded from the yaml file.
|
138
|
-
|
139
|
-
You might want to programatically load your holidays from a database table,
|
140
|
-
but you will want to pay attention to how the initializer works -
|
141
|
-
you will want to make sure that the initializer sets stuff up appropriately so
|
142
|
-
rails instances on mongrels or passenger will have the appropriate data as they come up and down.
|
143
|
-
|
144
|
-
== Timezone support
|
145
|
-
This gem strives to be timezone-agnostic.
|
146
|
-
Due to some complications in the handling of timezones in the built in Time class,
|
147
|
-
and some complexities (bugs?) in the timeWithZone class, this was harder than expected... but here's the idea:
|
148
|
-
|
149
|
-
* When you configure the gem with something like 9:00am as the start time,
|
150
|
-
this is agnostic of time zone.
|
151
|
-
* When you are dealing with a Time or TimeWithZone class, the timezone is
|
152
|
-
preserved and the beginning and end of times for the business day are
|
153
|
-
referenced in that time zone.
|
154
|
-
|
155
|
-
This can lead to some weird looking effects if, say, you are in the Eastern time zone but doing everything in UTC times...
|
156
|
-
Your business day will appear to start and end at 9:00 and 5:00 UTC.
|
157
|
-
If this seems perplexing to you, I can almost guarantee you are in over your head with timezones in other ways too,
|
158
|
-
this is just the first place you encountered it.
|
159
|
-
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.
|
160
|
-
|
161
|
-
== Integration with the Holidays gem
|
162
|
-
|
163
|
-
Chris Wise wrote up a great article[http://murmurinfo.wordpress.com/2012/01/11/handling-holidays-and-business-hours/]
|
164
|
-
on using the business_time gem with the holidays[https://github.com/alexdunae/holidays] gem. It boils down to this:
|
165
|
-
|
166
|
-
Holidays.between(Date.civil(2013, 1, 1), 2.years.from_now, :ca_on, :observed).map do |holiday|
|
167
|
-
BusinessTime::Config.holidays << holiday[:date]
|
168
|
-
# Implement long weekends if they apply to the region, eg:
|
169
|
-
# BusinessTime::Config.holidays << holiday[:date].next_week if !holiday[:date].weekday?
|
170
|
-
end
|
171
|
-
|
172
|
-
== Contributors
|
173
|
-
* David Bock http://github.com/bokmann
|
174
|
-
* Ryan McGeary http://github.com/rmm5t
|
175
|
-
* Enrico Bianco http://github.com/enricob
|
176
|
-
* Arild Shirazi http://github.com/ashirazi
|
177
|
-
* Piotr Jakubowski http://github.com/piotrj
|
178
|
-
* Glenn Vanderburg http://github.com/glv
|
179
|
-
* Michael Grosser http://github.com/grosser
|
180
|
-
* Michael Curtis http://github.com/mcurtis
|
181
|
-
* Brian Ewins http://github.com/bazzargh
|
182
|
-
|
183
|
-
(Special thanks for Arild on the complexities of dealing with TimeWithZone)
|
184
|
-
|
185
|
-
== Note on Patches/Pull Requests
|
186
|
-
|
187
|
-
* Fork the project.
|
188
|
-
* Make your feature addition or bug fix.
|
189
|
-
* Add tests for it. This is important so I don't break it in a
|
190
|
-
future version unintentionally.
|
191
|
-
* Commit, do not mess with rakefile, version, or history.
|
192
|
-
(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)
|
193
|
-
* Send me a pull request. Bonus points for topic branches.
|
194
|
-
|
195
|
-
== TODO
|
196
|
-
|
197
|
-
* Arild has pointed out that there may be some logical inconsistencies
|
198
|
-
regarding the beginning_of_workday and end_of workday times not actually
|
199
|
-
being considered inside of the workday. I'd like to make sure that they
|
200
|
-
work as if the beginning_of_workday is included and the end_of_workday is
|
201
|
-
not included, just like the '...' range operator in Ruby.
|
202
|
-
|
203
|
-
== NOT TODO
|
204
|
-
|
205
|
-
* I spent way too much time in my previous java-programmer life building frameworks that worshipped complexity,
|
206
|
-
always trying to give the developer-user ultimate flexibility at the expense of the 'surface area' of the api.
|
207
|
-
Never again - I will sooner limit functionality to 80% so that something stays usable and let people fork.
|
208
|
-
* While there have been requests to add 'business minutes' and even 'business seconds' to this gem, I won't
|
209
|
-
entertain a pull request with such things. If you find it useful, great. Most users won't, and they don't
|
210
|
-
need the baggage.
|
211
|
-
|
212
|
-
|
213
|
-
== A note on stability and change
|
214
|
-
|
215
|
-
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.
|
216
|
-
|
217
|
-
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.
|
218
|
-
|
219
|
-
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.
|
220
|
-
|
221
|
-
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.
|
222
|
-
|
223
|
-
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.
|
224
|
-
|
225
|
-
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.
|
226
|
-
|
227
|
-
== Copyright
|
228
|
-
|
229
|
-
Copyright (c) 2010-2021 bokmann. See LICENSE for details.
|