business-days 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/business-days.rb +43 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cfbdb115a49dd6890f10db52c93f249963b20ccc
|
4
|
+
data.tar.gz: 2a62fd6c2adba50683dfaa484c47ba4c581bb453
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c161760f5f74a8d6261b8700051401f5b9352ce0c3b3735ad65c28b9fd56dddba90df848280653fe98f6778ce3992dcc225848995e2ec0005f6952210283dac
|
7
|
+
data.tar.gz: b6c3dd701d722704e68228280f6327a44c2069f565271a0e852df9f16436259a807f261aa51aeb97d80293ddaf04fd3b0d98932d91a9d1ac80b680a3183aa455
|
data/lib/business-days.rb
CHANGED
@@ -17,10 +17,9 @@ class BusinessDaysSingleton
|
|
17
17
|
# Include holiday
|
18
18
|
@holidays.push(holiday[:date])
|
19
19
|
|
20
|
-
# 2. If Carnaval, include the day before
|
20
|
+
# 2. If Carnaval, include the day before it (it's a bank holiday)
|
21
21
|
if holiday[:name] == "Carnaval"
|
22
22
|
@holidays.push(holiday[:date] - 1.day)
|
23
|
-
@holidays.push(holiday[:date] + 1.day)
|
24
23
|
end
|
25
24
|
end
|
26
25
|
|
@@ -51,7 +50,7 @@ class BusinessDaysSingleton
|
|
51
50
|
date
|
52
51
|
end
|
53
52
|
|
54
|
-
#
|
53
|
+
# Check if the given date is a business day
|
55
54
|
#
|
56
55
|
# @param date [Date] the non-business date to be add.
|
57
56
|
# @return [Boolean] true if the given date is a business day, false otherwise.
|
@@ -112,5 +111,46 @@ class BusinessDaysSingleton
|
|
112
111
|
end
|
113
112
|
end
|
114
113
|
|
114
|
+
class BusinessDaysInstance
|
115
|
+
def initialize(holidays)
|
116
|
+
@holidays = []
|
117
|
+
|
118
|
+
holidays.each do |holiday|
|
119
|
+
# Include holiday
|
120
|
+
@holidays.push(Date.parse(holiday))
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# Check if the given date is a business day
|
125
|
+
#
|
126
|
+
# @param date [Date] the non-business date to be add.
|
127
|
+
# @return [Boolean] true if the given date is a business day, false otherwise.
|
128
|
+
def business_day?(date)
|
129
|
+
raise ArgumentError.new('Not a date') unless date.kind_of?(Date)
|
130
|
+
|
131
|
+
weekend = [6, 7].include?(date.cwday)
|
132
|
+
!(weekend || @holidays.include?(date))
|
133
|
+
end
|
134
|
+
|
135
|
+
# Count business days between two dates.
|
136
|
+
#
|
137
|
+
# @param from [Date] the starting date (inclusive)
|
138
|
+
# @param to [Date] the ending date (inclusive)
|
139
|
+
|
140
|
+
# @return [Integer] number of business days between the two dates
|
141
|
+
def business_days(from, to)
|
142
|
+
raise ArgumentError.new('Not a date: from') unless from.kind_of?(Date)
|
143
|
+
raise ArgumentError.new('Not a date: to') unless to.kind_of?(Date)
|
144
|
+
count = 0
|
145
|
+
|
146
|
+
while from <= to
|
147
|
+
count +=1 if self.business_day?(from)
|
148
|
+
from = from + 1.day
|
149
|
+
end
|
150
|
+
|
151
|
+
count
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
115
155
|
# Avoid calling the instance method on every call
|
116
156
|
BusinessDays = BusinessDaysSingleton.instance
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: business-days
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Allan Costa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|