business-days 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/business-days.rb +60 -26
- 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: 6841203b5c2dca045e7ccf02ecfebf59cde26b9f
|
4
|
+
data.tar.gz: 5da6e7a559f109e2bba79077b3fe78c0958131be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 946f2d3816308da6a8f8695e7d8ca4b4f52ef77eb6e32455fefa05534b93c65beefdcc659921f04cd68fc491a14b8c5b86e16a958f6c7909eceaf68fdeb6dcd2
|
7
|
+
data.tar.gz: a4a0688c717256ae7caef6dc60b71d740a03c66fc0c84c4a99a9ed3c10161f23352bf522bd18924e4bdbe31ab8c14ee2b9b5d71c05f011757284846368181e11
|
data/lib/business-days.rb
CHANGED
@@ -1,41 +1,75 @@
|
|
1
1
|
require 'active_support/all'
|
2
2
|
require 'holidays'
|
3
|
+
require 'singleton'
|
3
4
|
|
4
|
-
class
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
class BusinessDaysSingleton
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@holidays = []
|
10
|
+
|
11
|
+
# Setup holidays in an interval of 3 years
|
12
|
+
from = Time.current.year
|
13
|
+
to = Time.current.year + 2
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
date
|
20
|
-
|
21
|
-
count +=1
|
22
|
-
end
|
15
|
+
# 1. Get all holidays in given years interval from Holidays gem
|
16
|
+
Holidays.between(Date.civil(from, 1, 1), Date.civil(to, 12, 31), :br, :informal).each do |holiday|
|
17
|
+
# Include holiday
|
18
|
+
@holidays.push(holiday[:date])
|
19
|
+
|
20
|
+
# 2. If Carnaval, include the day before and after it (they are bank holidays)
|
21
|
+
if holiday[:name] == "Carnaval"
|
22
|
+
@holidays.push(holiday[:date] - 1.day)
|
23
|
+
@holidays.push(holiday[:date] + 1.day)
|
23
24
|
end
|
25
|
+
end
|
24
26
|
|
25
|
-
|
27
|
+
# 3. Add the latest business day in each year (it's a bank holiday)
|
28
|
+
(from..to).each do |year|
|
29
|
+
@holidays.push(latest_non_bank_businness_day(year))
|
26
30
|
end
|
31
|
+
end
|
27
32
|
|
28
|
-
|
29
|
-
|
33
|
+
def business_days_from_utc_time(days, time)
|
34
|
+
# We count days based on Brasilia timezone
|
35
|
+
date = time.in_time_zone('Brasilia').to_date
|
30
36
|
|
31
|
-
|
32
|
-
|
37
|
+
# Add days until we reach the specified business days
|
38
|
+
count = 0
|
39
|
+
while count < days
|
40
|
+
date += 1.days
|
41
|
+
if business_day?(date)
|
42
|
+
count +=1
|
43
|
+
end
|
33
44
|
end
|
34
45
|
|
35
|
-
|
36
|
-
|
46
|
+
date
|
47
|
+
end
|
48
|
+
|
49
|
+
def business_day?(date)
|
50
|
+
raise ArgumentError.new('Not a date') unless date.kind_of?(Date)
|
37
51
|
|
38
|
-
|
52
|
+
weekend = [6, 7].include?(date.cwday)
|
53
|
+
!(weekend || @holidays.include?(date))
|
54
|
+
end
|
55
|
+
|
56
|
+
def add_holiday(date)
|
57
|
+
raise ArgumentError.new('Not a date') unless date.kind_of?(Date)
|
58
|
+
|
59
|
+
@holidays.push(date)
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def latest_non_bank_businness_day(year)
|
65
|
+
d = Date.civil(year, 12, 31)
|
66
|
+
while !self.business_day?(d)
|
67
|
+
d = d - 1.day
|
39
68
|
end
|
69
|
+
|
70
|
+
d
|
40
71
|
end
|
41
72
|
end
|
73
|
+
|
74
|
+
# Avoid calling the instance method on every call
|
75
|
+
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.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Allan Costa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-12-
|
11
|
+
date: 2017-12-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|