roxbury 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.rubocop.yml +98 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +92 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/roxbury/business_calendar.rb +117 -0
- data/lib/roxbury/business_day.rb +33 -0
- data/lib/roxbury/empty_working_hours.rb +20 -0
- data/lib/roxbury/version.rb +3 -0
- data/lib/roxbury/working_hours.rb +60 -0
- data/lib/roxbury.rb +6 -0
- data/roxbury.gemspec +34 -0
- metadata +178 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 13b52faa3fb9374270da742934fdee4f0765ccf4774c8502f364aa05a411795d
|
4
|
+
data.tar.gz: 902edc63311964fbeb521a73f9cb4a5350c944852fa0ed8062a1d96f8416b4d1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e1aea73afe80cb96c4cfea67486bd208eba3587f903ffcce6f4c45db4493ecd9e987549035c0ba8405c2618ac780e3b213192c7c90dc4830aa3aa4b2be50698f
|
7
|
+
data.tar.gz: 12b5aac1014909993f5119a5bb327182e09818bb85ba5899aa0621ee157c918ae9af385995212092420158962b893be412b78a880bb5ee50cb86079c06b58dfd
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
Style/Documentation:
|
2
|
+
Enabled: false
|
3
|
+
|
4
|
+
Style/MethodDefParentheses:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
Style/ParallelAssignment:
|
8
|
+
Enabled: false
|
9
|
+
|
10
|
+
Style/FrozenStringLiteralComment:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Style/FormatStringToken:
|
14
|
+
Enabled: false
|
15
|
+
|
16
|
+
Style/EmptyMethod:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Style/GuardClause:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Style/CommentAnnotation:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Style/AsciiComments:
|
26
|
+
Enabled: false
|
27
|
+
|
28
|
+
Style/StabbyLambdaParentheses:
|
29
|
+
EnforcedStyle: require_no_parentheses
|
30
|
+
|
31
|
+
Style/MultilineIfModifier:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
Style/SafeNavigation:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
Style/FormatString:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
Style/AndOr:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
Style/Next:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
Style/NumericPredicate:
|
47
|
+
Enabled: false
|
48
|
+
|
49
|
+
Style/SymbolArray:
|
50
|
+
Enabled: false
|
51
|
+
|
52
|
+
Style/MutableConstant:
|
53
|
+
Enabled: false
|
54
|
+
|
55
|
+
Style/RescueModifier:
|
56
|
+
Enabled: false
|
57
|
+
|
58
|
+
Layout/SpaceInsideHashLiteralBraces:
|
59
|
+
Enabled: false
|
60
|
+
|
61
|
+
Layout/MultilineMethodCallIndentation:
|
62
|
+
Enabled: false
|
63
|
+
|
64
|
+
Layout/TrailingBlankLines:
|
65
|
+
Enabled: false
|
66
|
+
|
67
|
+
Layout/IndentArray:
|
68
|
+
EnforcedStyle: consistent
|
69
|
+
|
70
|
+
Layout/AlignParameters:
|
71
|
+
EnforcedStyle: with_fixed_indentation
|
72
|
+
|
73
|
+
Layout/SpaceInLambdaLiteral:
|
74
|
+
Enabled: false
|
75
|
+
|
76
|
+
Metrics:
|
77
|
+
Enabled: false
|
78
|
+
|
79
|
+
Bundler:
|
80
|
+
Enabled: false
|
81
|
+
|
82
|
+
Lint/AssignmentInCondition:
|
83
|
+
Enabled: false
|
84
|
+
|
85
|
+
Lint/ShadowingOuterLocalVariable:
|
86
|
+
Enabled: false
|
87
|
+
|
88
|
+
Lint/AmbiguousRegexpLiteral:
|
89
|
+
Enabled: false
|
90
|
+
|
91
|
+
Lint/AmbiguousOperator:
|
92
|
+
Enabled: false
|
93
|
+
|
94
|
+
Lint/AmbiguousBlockAssociation:
|
95
|
+
Enabled: false
|
96
|
+
|
97
|
+
Naming/UncommunicativeMethodParamName:
|
98
|
+
Enabled: false
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.5.0
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at emmanicolau@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
roxbury (0.1.0)
|
5
|
+
activesupport (>= 4.2)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (5.2.3)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (>= 0.7, < 2)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
tzinfo (~> 1.1)
|
15
|
+
byebug (11.0.1)
|
16
|
+
coderay (1.1.2)
|
17
|
+
concurrent-ruby (1.1.5)
|
18
|
+
diff-lcs (1.3)
|
19
|
+
ffi (1.10.0)
|
20
|
+
formatador (0.2.5)
|
21
|
+
guard (2.14.2)
|
22
|
+
formatador (>= 0.2.4)
|
23
|
+
listen (>= 2.7, < 4.0)
|
24
|
+
lumberjack (>= 1.0.12, < 2.0)
|
25
|
+
nenv (~> 0.1)
|
26
|
+
notiffany (~> 0.0)
|
27
|
+
pry (>= 0.9.12)
|
28
|
+
shellany (~> 0.0)
|
29
|
+
thor (>= 0.18.1)
|
30
|
+
guard-compat (1.2.1)
|
31
|
+
guard-rspec (4.7.3)
|
32
|
+
guard (~> 2.1)
|
33
|
+
guard-compat (~> 1.1)
|
34
|
+
rspec (>= 2.99.0, < 4.0)
|
35
|
+
i18n (1.6.0)
|
36
|
+
concurrent-ruby (~> 1.0)
|
37
|
+
listen (3.1.5)
|
38
|
+
rb-fsevent (~> 0.9, >= 0.9.4)
|
39
|
+
rb-inotify (~> 0.9, >= 0.9.7)
|
40
|
+
ruby_dep (~> 1.2)
|
41
|
+
lumberjack (1.0.13)
|
42
|
+
method_source (0.9.2)
|
43
|
+
minitest (5.11.3)
|
44
|
+
nenv (0.3.0)
|
45
|
+
notiffany (0.1.1)
|
46
|
+
nenv (~> 0.1)
|
47
|
+
shellany (~> 0.0)
|
48
|
+
pry (0.11.3)
|
49
|
+
coderay (~> 1.1.0)
|
50
|
+
method_source (~> 0.9.0)
|
51
|
+
pry-byebug (3.7.0)
|
52
|
+
byebug (~> 11.0)
|
53
|
+
pry (~> 0.10)
|
54
|
+
rake (10.5.0)
|
55
|
+
rb-fsevent (0.10.3)
|
56
|
+
rb-inotify (0.10.0)
|
57
|
+
ffi (~> 1.0)
|
58
|
+
rspec (3.8.0)
|
59
|
+
rspec-core (~> 3.8.0)
|
60
|
+
rspec-expectations (~> 3.8.0)
|
61
|
+
rspec-mocks (~> 3.8.0)
|
62
|
+
rspec-core (3.8.2)
|
63
|
+
rspec-support (~> 3.8.0)
|
64
|
+
rspec-expectations (3.8.4)
|
65
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
66
|
+
rspec-support (~> 3.8.0)
|
67
|
+
rspec-mocks (3.8.1)
|
68
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
69
|
+
rspec-support (~> 3.8.0)
|
70
|
+
rspec-support (3.8.2)
|
71
|
+
ruby_dep (1.5.0)
|
72
|
+
shellany (0.0.1)
|
73
|
+
thor (0.20.3)
|
74
|
+
thread_safe (0.3.6)
|
75
|
+
tzinfo (1.2.5)
|
76
|
+
thread_safe (~> 0.1)
|
77
|
+
|
78
|
+
PLATFORMS
|
79
|
+
ruby
|
80
|
+
|
81
|
+
DEPENDENCIES
|
82
|
+
bundler (~> 2.0)
|
83
|
+
guard
|
84
|
+
guard-rspec
|
85
|
+
pry
|
86
|
+
pry-byebug
|
87
|
+
rake (~> 10.0)
|
88
|
+
roxbury!
|
89
|
+
rspec (~> 3.0)
|
90
|
+
|
91
|
+
BUNDLED WITH
|
92
|
+
2.0.2
|
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Emmanuel Nicolau
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Roxbury
|
2
|
+
|
3
|
+
A Ruby library for handling business days calculations, e.g., working days/hours between two dates, add working days/hours to a date, etc.
|
4
|
+
|
5
|
+
[![Build Status](https://travis-ci.org/eeng/roxbury.svg?branch=master)](https://travis-ci.org/eeng/roxbury)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'roxbury'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
calendar = Roxbury::BusinessCalendar.new(
|
23
|
+
working_hours: {
|
24
|
+
'Mon' => 9..17,
|
25
|
+
'Tue' => 9..17,
|
26
|
+
'Wed' => 9..17,
|
27
|
+
'Thu' => 9..17,
|
28
|
+
'Fri' => 9..17,
|
29
|
+
'Sat' => 9..13
|
30
|
+
}
|
31
|
+
)
|
32
|
+
|
33
|
+
calendar.working_days_between(Date.new(2019, 8, 1), Date.new(2019, 8, 5))
|
34
|
+
# => 3.5
|
35
|
+
|
36
|
+
calendar.add_working_days(Date.new(2019, 8, 3), 1)
|
37
|
+
# => Date.new(2019, 8, 5)
|
38
|
+
|
39
|
+
calendar.add_working_days(Time.new(2019, 8, 3, 9, 0), 1)
|
40
|
+
# => Time.new(2019, 8, 5, 13, 0)
|
41
|
+
|
42
|
+
calendar.working_hours_between(Time.new(2019, 8, 2, 8, 0), Time.new(2019, 8, 3, 14, 0))
|
43
|
+
# => 12
|
44
|
+
|
45
|
+
calendar.roll_forward(Time.new(2019, 8, 4))
|
46
|
+
# => Time.new(2019, 8, 5, 9, 0)
|
47
|
+
```
|
48
|
+
|
49
|
+
Please refer to the tests in `spec/roxbury/business_calendar_spec.rb` for more examples.
|
50
|
+
|
51
|
+
### Holidays
|
52
|
+
|
53
|
+
You can specify the list of holidays in the constructor:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
calendar = Roxbury::BusinessCalendar.new(
|
57
|
+
working_hours: Hash.new(9..17),
|
58
|
+
holidays: [Date.new(2019, 12, 25)]
|
59
|
+
)
|
60
|
+
|
61
|
+
calendar.working_days_between(Date.new(2019, 12, 24), Date.new(2019, 12, 26))
|
62
|
+
# => 2.0
|
63
|
+
```
|
64
|
+
|
65
|
+
## Development
|
66
|
+
|
67
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/eeng/roxbury. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
72
|
+
|
73
|
+
## Code of Conduct
|
74
|
+
|
75
|
+
Everyone interacting in the Roxbury project’s codebases and issue trackers is expected to follow the [code of conduct](https://github.com/eeng/roxbury/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'roxbury'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
require 'pry'
|
11
|
+
Pry.start
|
12
|
+
|
13
|
+
# require "irb"
|
14
|
+
# IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
module Roxbury
|
2
|
+
class BusinessCalendar
|
3
|
+
DAYS_OF_THE_WEEK = %w[Mon Tue Wed Thu Fri Sat Sun]
|
4
|
+
|
5
|
+
attr_reader :working_hours
|
6
|
+
|
7
|
+
def initialize working_hours: {}, holidays: []
|
8
|
+
@working_hours = DAYS_OF_THE_WEEK.inject({}) do |wh, dow|
|
9
|
+
wh.merge dow => WorkingHours.parse(working_hours[dow])
|
10
|
+
end
|
11
|
+
if @working_hours.values.all?(&:non_working?)
|
12
|
+
raise ArgumentError, 'You must specify at least one working day in working_hours.'
|
13
|
+
end
|
14
|
+
@holidays = Set.new(holidays)
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param from [Date, Time] if it's a date, it's handled as the beginning of the day
|
18
|
+
# @param to [Date, Time] if it's a date, it's handled as the end of the day
|
19
|
+
# @return [Float] the number of working hours between the given dates
|
20
|
+
def working_hours_between from, to
|
21
|
+
from, to, sign = invert_if_needed cast_time(from, :start), cast_time(to, :end)
|
22
|
+
|
23
|
+
working_hours_per_day = (from.to_date..to.to_date).map do |date|
|
24
|
+
filters = {}
|
25
|
+
filters[:from] = from if date == from.to_date
|
26
|
+
filters[:to] = to if date == to.to_date
|
27
|
+
business_day(date).number_of_working_hours filters
|
28
|
+
end
|
29
|
+
|
30
|
+
working_hours_per_day.sum.round(2) * sign
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_working_hours to, number_of_hours
|
34
|
+
raise ArgumentError, 'number_of_hours must not be negative' if number_of_hours < 0
|
35
|
+
to = cast_time(to, :start)
|
36
|
+
rolling_timestamp = roll_forward(to)
|
37
|
+
remaining_hours = number_of_hours
|
38
|
+
|
39
|
+
until (bday = business_day(rolling_timestamp)).include?(rolling_timestamp + remaining_hours.hours)
|
40
|
+
remaining_hours -= bday.number_of_working_hours(from: rolling_timestamp)
|
41
|
+
rolling_timestamp = at_beginning_of_next_business_day(rolling_timestamp)
|
42
|
+
end
|
43
|
+
|
44
|
+
rolling_timestamp + remaining_hours.hours
|
45
|
+
end
|
46
|
+
|
47
|
+
# @param from [Date, Time] if it's a date, it's handled as the beginning of the day
|
48
|
+
# @param to [Date, Time] if it's a date, it's handled as the end of the day
|
49
|
+
# @return [Float] the number of working days between the given dates.
|
50
|
+
def working_days_between from, to
|
51
|
+
working_hours_between(from, to) / max_working_hours_in_a_day.to_f
|
52
|
+
end
|
53
|
+
|
54
|
+
# @param to [Date, Time]
|
55
|
+
# @param number_of_days [Integer, Float]
|
56
|
+
# @return [Date, Time] The result of adding the number_of_days to the given date. If a Date is given returns a Date, otherwise if a Time is given returns a Time.
|
57
|
+
def add_working_days to, number_of_days
|
58
|
+
result = add_working_hours(to, number_of_days * max_working_hours_in_a_day)
|
59
|
+
to.is_a?(Date) ? result.to_date : result
|
60
|
+
end
|
61
|
+
|
62
|
+
# Snaps the date to the beginning of the next business day, unless it is already within the working hours.
|
63
|
+
#
|
64
|
+
# @param date [Date, Time]
|
65
|
+
def roll_forward date
|
66
|
+
bday = business_day(date)
|
67
|
+
if bday.include?(date)
|
68
|
+
date
|
69
|
+
elsif bday.starts_after?(date)
|
70
|
+
bday.at_beginning
|
71
|
+
else
|
72
|
+
roll_forward date.tomorrow.beginning_of_day
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Snaps the date to the beginning of the next business day.
|
77
|
+
def at_beginning_of_next_business_day date
|
78
|
+
roll_forward date.tomorrow.beginning_of_day
|
79
|
+
end
|
80
|
+
|
81
|
+
def holiday? date_or_time
|
82
|
+
@holidays.include?(date_or_time.to_date)
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def cast_time date_or_time, start_or_end
|
88
|
+
case date_or_time
|
89
|
+
when Time then
|
90
|
+
date_or_time
|
91
|
+
when Date then
|
92
|
+
start_or_end == :end ? date_or_time.to_time.end_of_day : date_or_time.to_time
|
93
|
+
else
|
94
|
+
raise ArgumentError, "Type #{date_or_time.class} not supported"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def invert_if_needed from, to
|
99
|
+
if from > to
|
100
|
+
from, to = to, from
|
101
|
+
sign = -1
|
102
|
+
else
|
103
|
+
sign = 1
|
104
|
+
end
|
105
|
+
[from, to, sign]
|
106
|
+
end
|
107
|
+
|
108
|
+
def business_day date
|
109
|
+
dow = date.strftime '%a'
|
110
|
+
BusinessDay.new date, (holiday?(date) ? EmptyWorkingHours.new : @working_hours[dow])
|
111
|
+
end
|
112
|
+
|
113
|
+
def max_working_hours_in_a_day
|
114
|
+
@working_hours.values.map(&:quantity).max
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Roxbury
|
2
|
+
# Wraps a specific date with its working hours schedule
|
3
|
+
class BusinessDay
|
4
|
+
def initialize date, working_hours
|
5
|
+
@date = date
|
6
|
+
@working_hours = working_hours
|
7
|
+
end
|
8
|
+
|
9
|
+
def number_of_working_hours *args
|
10
|
+
@working_hours.quantity *args
|
11
|
+
end
|
12
|
+
|
13
|
+
def same_day? timestamp
|
14
|
+
timestamp.to_date == @date.to_date
|
15
|
+
end
|
16
|
+
|
17
|
+
def include? timestamp
|
18
|
+
same_day?(timestamp) && @working_hours.include?(timestamp)
|
19
|
+
end
|
20
|
+
|
21
|
+
def starts_after? timestamp
|
22
|
+
same_day?(timestamp) && @working_hours.starts_after?(timestamp)
|
23
|
+
end
|
24
|
+
|
25
|
+
def at_beginning
|
26
|
+
@working_hours.at_beginning @date
|
27
|
+
end
|
28
|
+
|
29
|
+
def at_end
|
30
|
+
@working_hours.at_end @date
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Roxbury
|
2
|
+
# Null object version of WorkingHours for holidays and other non working days
|
3
|
+
class EmptyWorkingHours < WorkingHours
|
4
|
+
def initialize
|
5
|
+
super begins_at: 0, ends_at: 0
|
6
|
+
end
|
7
|
+
|
8
|
+
def include? _timestamp
|
9
|
+
false
|
10
|
+
end
|
11
|
+
|
12
|
+
def starts_after? _timestamp
|
13
|
+
false
|
14
|
+
end
|
15
|
+
|
16
|
+
def non_working?
|
17
|
+
true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Roxbury
|
2
|
+
class WorkingHours
|
3
|
+
attr_accessor :begins_at, :ends_at
|
4
|
+
|
5
|
+
def self.parse bday_spec
|
6
|
+
case bday_spec
|
7
|
+
when Range
|
8
|
+
if bday_spec.last <= 0
|
9
|
+
EmptyWorkingHours.new
|
10
|
+
else
|
11
|
+
new begins_at: bday_spec.first, ends_at: bday_spec.last
|
12
|
+
end
|
13
|
+
when nil
|
14
|
+
EmptyWorkingHours.new
|
15
|
+
else
|
16
|
+
raise ArgumentError, "Business day spec not supported: #{bday_spec.inspect}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize begins_at:, ends_at:
|
21
|
+
@begins_at, @ends_at = begins_at, ends_at
|
22
|
+
end
|
23
|
+
|
24
|
+
def quantity from: nil, to: nil
|
25
|
+
from = from ? hours_from_midnight(from) : begins_at
|
26
|
+
to = to ? hours_from_midnight(to) : ends_at
|
27
|
+
[[ends_at, to].min - [from, begins_at].max, 0].max
|
28
|
+
end
|
29
|
+
|
30
|
+
def include? timestamp
|
31
|
+
(at_beginning(timestamp)..at_end(timestamp)).cover?(timestamp)
|
32
|
+
end
|
33
|
+
|
34
|
+
def starts_after? timestamp
|
35
|
+
timestamp < at_beginning(timestamp)
|
36
|
+
end
|
37
|
+
|
38
|
+
def at_beginning timestamp
|
39
|
+
timestamp.change(hour: begins_at, min: 0, sec: 0)
|
40
|
+
end
|
41
|
+
|
42
|
+
def at_end timestamp
|
43
|
+
timestamp.change(hour: [ends_at - 1, 0].max, min: 59, sec: 59)
|
44
|
+
end
|
45
|
+
|
46
|
+
def non_working?
|
47
|
+
false
|
48
|
+
end
|
49
|
+
|
50
|
+
def working_day?
|
51
|
+
!non_working?
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def hours_from_midnight time
|
57
|
+
time.seconds_since_midnight / 1.0.hour
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/roxbury.rb
ADDED
data/roxbury.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "roxbury/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "roxbury"
|
7
|
+
spec.version = Roxbury::VERSION
|
8
|
+
spec.authors = ["Emmanuel Nicolau"]
|
9
|
+
spec.email = ["emmanicolau@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Ruby library for business days calculations}
|
12
|
+
spec.description = %q{A Ruby library for handling business days calculations, e.g., working days/hours between two dates, add working days/hours to a date, etc.}
|
13
|
+
spec.homepage = "https://github.com/eeng/roxbury"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_dependency "activesupport", ">= 4.2"
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
30
|
+
spec.add_development_dependency "guard"
|
31
|
+
spec.add_development_dependency "guard-rspec"
|
32
|
+
spec.add_development_dependency 'pry'
|
33
|
+
spec.add_development_dependency 'pry-byebug'
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: roxbury
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Emmanuel Nicolau
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-08-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: guard-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry-byebug
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: A Ruby library for handling business days calculations, e.g., working
|
126
|
+
days/hours between two dates, add working days/hours to a date, etc.
|
127
|
+
email:
|
128
|
+
- emmanicolau@gmail.com
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- ".gitignore"
|
134
|
+
- ".rspec"
|
135
|
+
- ".rubocop.yml"
|
136
|
+
- ".ruby-version"
|
137
|
+
- ".travis.yml"
|
138
|
+
- CODE_OF_CONDUCT.md
|
139
|
+
- Gemfile
|
140
|
+
- Gemfile.lock
|
141
|
+
- Guardfile
|
142
|
+
- LICENSE.txt
|
143
|
+
- README.md
|
144
|
+
- Rakefile
|
145
|
+
- bin/console
|
146
|
+
- bin/setup
|
147
|
+
- lib/roxbury.rb
|
148
|
+
- lib/roxbury/business_calendar.rb
|
149
|
+
- lib/roxbury/business_day.rb
|
150
|
+
- lib/roxbury/empty_working_hours.rb
|
151
|
+
- lib/roxbury/version.rb
|
152
|
+
- lib/roxbury/working_hours.rb
|
153
|
+
- roxbury.gemspec
|
154
|
+
homepage: https://github.com/eeng/roxbury
|
155
|
+
licenses:
|
156
|
+
- MIT
|
157
|
+
metadata: {}
|
158
|
+
post_install_message:
|
159
|
+
rdoc_options: []
|
160
|
+
require_paths:
|
161
|
+
- lib
|
162
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
168
|
+
requirements:
|
169
|
+
- - ">="
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
version: '0'
|
172
|
+
requirements: []
|
173
|
+
rubyforge_project:
|
174
|
+
rubygems_version: 2.7.3
|
175
|
+
signing_key:
|
176
|
+
specification_version: 4
|
177
|
+
summary: Ruby library for business days calculations
|
178
|
+
test_files: []
|