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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/business-days.rb +60 -26
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc274075629002d70301e7e805291871778dd331
4
- data.tar.gz: 84911ef3242a3d587ba7451ed4623bb26a376d1c
3
+ metadata.gz: 6841203b5c2dca045e7ccf02ecfebf59cde26b9f
4
+ data.tar.gz: 5da6e7a559f109e2bba79077b3fe78c0958131be
5
5
  SHA512:
6
- metadata.gz: '0689b65d2b4476bf66ce12ad3986c72711f8b4d70341895a734d7bef6277009405282f802f113a53b3acaac1163d5e1c11eb6c32516763a93c982a741777a0a9'
7
- data.tar.gz: 0a2ba51641c4219655e1d2b09394995b48b666b79cdd7b878762c415b1bfa9d8ef3c4a6cd378e951f485f99ba3dd3e6777d5d6a7ffe25e2ac7a9a66cdd78e126
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 BusinessDays
5
- class << self
6
- # Get holidays dates until next year
7
- @@holidays = []
8
- Holidays.between(Date.civil(Time.current.year,1,1), Date.civil(Time.current.year + 1, 12, 31), :br, :br).each do |holiday|
9
- @@holidays.push(holiday[:date])
10
- end
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
- def business_days_from_utc_time(days, time)
13
- # We count days based on Brasilia timezone
14
- date = time.in_time_zone('Brasilia').to_date
15
-
16
- # Add days until we reach the specified business days
17
- count = 0
18
- while count < days
19
- date += 1.days
20
- if business_day?(date)
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
- date
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
- def business_day?(date)
29
- raise ArgumentError.new('Not a date') unless date.kind_of?(Date)
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
- weekend = [6, 7].include?(date.cwday)
32
- !(weekend || @@holidays.include?(date))
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
- def add_holiday(date)
36
- raise ArgumentError.new('Not a date') unless date.kind_of?(Date)
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
- @@holidays.push(date)
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.3
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-27 00:00:00.000000000 Z
11
+ date: 2017-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport