holidate 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +16 -0
- data/Gemfile +1 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +9 -0
- data/holidate.gemspec +18 -0
- data/lib/holidate.rb +43 -42
- data/spec/holidate_spec.rb +125 -0
- metadata +9 -2
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
gemspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Cohen Carlisle
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Holidate
|
2
|
+
|
3
|
+
A simple gem that returns Date objects for holidays
|
4
|
+
|
5
|
+
# Usage
|
6
|
+
|
7
|
+
Currently has methods to find the 10 US federal holidays, with year as an optional argument that defaults to the current year.
|
8
|
+
```ruby
|
9
|
+
Holidate.new_years_day # alias: new_year
|
10
|
+
Holidate.martin_luther_king_jr_day # alias: mlk
|
11
|
+
Holidate.washingtons_birthday # alias: washington
|
12
|
+
Holidate.memorial_day # alias: memorial
|
13
|
+
Holidate.independence_day # alias: july_4
|
14
|
+
Holidate.labor_day # alias: labor
|
15
|
+
Holidate.columbus_day # alias: columbus
|
16
|
+
Holidate.veterans_day # alias: veteran
|
17
|
+
Holidate.thanksgiving_day # alias: thanksgiving
|
18
|
+
Holidate.christmas_day # alias: xmas
|
19
|
+
```
|
20
|
+
|
21
|
+
Also has a ::find method that allows you to find the nth day of the week in a given month and year.
|
22
|
+
```ruby
|
23
|
+
Holidate.find('1st', 'Friday', 'December', '1985')
|
24
|
+
```
|
25
|
+
|
26
|
+
You can also find the nth to last day.
|
27
|
+
```ruby
|
28
|
+
Holidate.find(:third_to_last, :wed , :oct, :'2013')
|
29
|
+
```
|
30
|
+
|
31
|
+
::find is case-insensitive and accepts both strings and symbols.
|
32
|
+
It also accepts abbreviations for months and days of the week and allows first, second, etc. to be abbreviated as 1st, 2nd, etc.
|
33
|
+
You can also give ::find integers. Day of the week and month should correspond to Date#wday and Date#month, respectively.
|
34
|
+
```ruby
|
35
|
+
Holidate.find(-1, 0, 7, 1985) # == Holidate.find('last', 'Sunday', 'July', '1985')
|
36
|
+
```
|
37
|
+
|
38
|
+
Of course, magic numbers can make code harder to understand, so use with caution.
|
data/Rakefile
ADDED
data/holidate.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'holidate'
|
5
|
+
s.version = '0.0.4'
|
6
|
+
s.date = Date.today.to_s
|
7
|
+
s.summary = 'Holiday Dates'
|
8
|
+
s.description = 'A simple gem that returns Date objects for holidays'
|
9
|
+
s.authors = ['Cohen Carlisle']
|
10
|
+
s.email = 'holidate.gem@gmail.com'
|
11
|
+
s.files = `git ls-files`.split("\n")
|
12
|
+
s.homepage = 'http://rubygems.org/gems/holidate'
|
13
|
+
s.license = 'MIT'
|
14
|
+
|
15
|
+
s.add_development_dependency 'bundler', '~> 1.7'
|
16
|
+
s.add_development_dependency 'rake', '~> 10.0'
|
17
|
+
s.add_development_dependency 'minitest', '~> 5.5'
|
18
|
+
end
|
data/lib/holidate.rb
CHANGED
@@ -1,91 +1,92 @@
|
|
1
1
|
require 'date'
|
2
2
|
|
3
3
|
class Holidate
|
4
|
-
def self.find
|
4
|
+
def self.find(nth_, wday_, month_, year_=Date.today.year)
|
5
5
|
nth = nth_int(nth_)
|
6
6
|
wday = wday_int(wday_)
|
7
7
|
month = month_int(month_)
|
8
8
|
year = year_int(year_)
|
9
9
|
if nth > 0
|
10
|
-
Date.new
|
10
|
+
Date.new(year, month, (1 + (nth - 1) * 7 + (wday - Date.new(year, month, 1).wday) % 7))
|
11
11
|
else
|
12
|
-
days_in_month = ((Date.new(year, month) >> 1) -1).day
|
13
|
-
Date.new
|
12
|
+
days_in_month = ((Date.new(year, month) >> 1) - 1).day
|
13
|
+
Date.new(year, month, (days_in_month - (-nth - 1) * 7 - (Date.new(year, month, days_in_month).wday - wday) % 7))
|
14
14
|
end
|
15
15
|
end
|
16
16
|
singleton_class.send(:alias_method, :find_day, :find)
|
17
17
|
|
18
|
-
def self.new_years_day
|
19
|
-
Date.new
|
18
|
+
def self.new_years_day(year=Date.today.year)
|
19
|
+
Date.new(year, 1, 1)
|
20
20
|
end
|
21
21
|
singleton_class.send(:alias_method, :new_year, :new_years_day)
|
22
22
|
|
23
|
-
def self.martin_luther_king_jr_day
|
24
|
-
find
|
23
|
+
def self.martin_luther_king_jr_day(year=Date.today.year)
|
24
|
+
find('third', 'monday', 'january', year)
|
25
25
|
end
|
26
26
|
singleton_class.send(:alias_method, :mlk, :martin_luther_king_jr_day)
|
27
27
|
|
28
|
-
def self.washingtons_birthday
|
29
|
-
find
|
28
|
+
def self.washingtons_birthday(year=Date.today.year)
|
29
|
+
find('third', 'monday', 'february', year)
|
30
30
|
end
|
31
31
|
singleton_class.send(:alias_method, :washington, :washingtons_birthday)
|
32
32
|
|
33
|
-
def self.memorial_day
|
34
|
-
find
|
33
|
+
def self.memorial_day(year=Date.today.year)
|
34
|
+
find('last', 'monday', 'may', year)
|
35
35
|
end
|
36
36
|
singleton_class.send(:alias_method, :memorial, :memorial_day)
|
37
37
|
|
38
|
-
def self.independence_day
|
39
|
-
Date.new
|
38
|
+
def self.independence_day(year=Date.today.year)
|
39
|
+
Date.new(year, 7, 4)
|
40
40
|
end
|
41
41
|
singleton_class.send(:alias_method, :july_4, :independence_day)
|
42
42
|
|
43
|
-
def self.labor_day
|
44
|
-
find
|
43
|
+
def self.labor_day(year=Date.today.year)
|
44
|
+
find('first', 'monday', 'september', year)
|
45
45
|
end
|
46
46
|
singleton_class.send(:alias_method, :labor, :labor_day)
|
47
47
|
|
48
|
-
def self.columbus_day
|
49
|
-
find
|
48
|
+
def self.columbus_day(year=Date.today.year)
|
49
|
+
find('second', 'monday', 'october', year)
|
50
50
|
end
|
51
51
|
singleton_class.send(:alias_method, :columbus, :columbus_day)
|
52
52
|
|
53
|
-
def self.veterans_day
|
54
|
-
Date.new
|
53
|
+
def self.veterans_day(year=Date.today.year)
|
54
|
+
Date.new(year, 11, 11)
|
55
55
|
end
|
56
56
|
singleton_class.send(:alias_method, :veteran, :veterans_day)
|
57
57
|
|
58
|
-
def self.thanksgiving_day
|
59
|
-
find
|
58
|
+
def self.thanksgiving_day(year=Date.today.year)
|
59
|
+
find('fourth', 'thursday', 'november', year)
|
60
60
|
end
|
61
61
|
singleton_class.send(:alias_method, :thanksgiving, :thanksgiving_day)
|
62
62
|
|
63
|
-
def self.christmas_day
|
64
|
-
Date.new
|
63
|
+
def self.christmas_day(year=Date.today.year)
|
64
|
+
Date.new(year, 12, 25)
|
65
65
|
end
|
66
66
|
singleton_class.send(:alias_method, :xmas, :christmas_day)
|
67
67
|
|
68
|
-
|
68
|
+
class << self
|
69
|
+
protected
|
69
70
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
end
|
71
|
+
def nth_int(nth_)
|
72
|
+
h = { 'first' => 1, 'second' => 2, 'third' => 3, 'fourth' => 4, 'fifth' => 5, 'last' => 1 }
|
73
|
+
nth = nth_.to_s.downcase
|
74
|
+
nth_int = (nth.to_i == 0 ? h[nth.split(/[-_ ]/).first] : nth.to_i)
|
75
|
+
nth.end_with?('last') ? -nth_int : nth_int
|
76
|
+
end
|
77
77
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
78
|
+
def wday_int(wday)
|
79
|
+
h = { 'sun' => 0, 'mon' => 1, 'tue' => 2, 'wed' => 3, 'thu' => 4, 'fri' => 5, 'sat' => 6 }
|
80
|
+
(0..6).include?(wday) ? wday : h[wday.to_s.downcase[0..2]]
|
81
|
+
end
|
82
82
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
83
|
+
def month_int(month)
|
84
|
+
h = { 'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4, 'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8, 'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12 }
|
85
|
+
(1..12).include?(month) ? month : h[month.to_s.downcase[0..2]]
|
86
|
+
end
|
87
87
|
|
88
|
-
|
89
|
-
|
88
|
+
def year_int(year)
|
89
|
+
year.to_s.to_i
|
90
|
+
end
|
90
91
|
end
|
91
92
|
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
gem 'minitest'
|
2
|
+
require 'minitest/spec'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'holidate'
|
5
|
+
|
6
|
+
describe Holidate do
|
7
|
+
|
8
|
+
describe '::new_years_day' do
|
9
|
+
it "finds New Year's Day" do
|
10
|
+
Holidate.new_year(2014).must_equal Date.new(2014, 1, 1)
|
11
|
+
Holidate.new_year(2015).must_equal Date.new(2015, 1, 1)
|
12
|
+
Holidate.new_year(2016).must_equal Date.new(2016, 1, 1)
|
13
|
+
Holidate.new_year(2017).must_equal Date.new(2017, 1, 1)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '::martin_luther_king_jr_day' do
|
18
|
+
it 'finds Martin Luther King, Jr. Day' do
|
19
|
+
Holidate.mlk(2014).must_equal Date.new(2014, 1, 20)
|
20
|
+
Holidate.mlk(2015).must_equal Date.new(2015, 1, 19)
|
21
|
+
Holidate.mlk(2016).must_equal Date.new(2016, 1, 18)
|
22
|
+
Holidate.mlk(2017).must_equal Date.new(2017, 1, 16)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '::washingtons_birthday' do
|
27
|
+
it "finds Washington's Birthday" do
|
28
|
+
Holidate.washington(2014).must_equal Date.new(2014, 2, 17)
|
29
|
+
Holidate.washington(2015).must_equal Date.new(2015, 2, 16)
|
30
|
+
Holidate.washington(2016).must_equal Date.new(2016, 2, 15)
|
31
|
+
Holidate.washington(2017).must_equal Date.new(2017, 2, 20)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '::memorial_day' do
|
36
|
+
it 'finds Memorial Day' do
|
37
|
+
Holidate.memorial(2014).must_equal Date.new(2014, 5, 26)
|
38
|
+
Holidate.memorial(2015).must_equal Date.new(2015, 5, 25)
|
39
|
+
Holidate.memorial(2016).must_equal Date.new(2016, 5, 30)
|
40
|
+
Holidate.memorial(2017).must_equal Date.new(2017, 5, 29)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '::independence_day' do
|
45
|
+
it 'finds Independence Day' do
|
46
|
+
Holidate.july_4(2014).must_equal Date.new(2014, 7, 4)
|
47
|
+
Holidate.july_4(2015).must_equal Date.new(2015, 7, 4)
|
48
|
+
Holidate.july_4(2016).must_equal Date.new(2016, 7, 4)
|
49
|
+
Holidate.july_4(2017).must_equal Date.new(2017, 7, 4)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '::labor_day' do
|
54
|
+
it 'finds Labor Day' do
|
55
|
+
Holidate.labor(2014).must_equal Date.new(2014, 9, 1)
|
56
|
+
Holidate.labor(2015).must_equal Date.new(2015, 9, 7)
|
57
|
+
Holidate.labor(2016).must_equal Date.new(2016, 9, 5)
|
58
|
+
Holidate.labor(2017).must_equal Date.new(2017, 9, 4)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '::columbus_day' do
|
63
|
+
it 'finds Columbus Day' do
|
64
|
+
Holidate.columbus(2014).must_equal Date.new(2014, 10, 13)
|
65
|
+
Holidate.columbus(2015).must_equal Date.new(2015, 10, 12)
|
66
|
+
Holidate.columbus(2016).must_equal Date.new(2016, 10, 10)
|
67
|
+
Holidate.columbus(2017).must_equal Date.new(2017, 10, 9)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '::veterans_day' do
|
72
|
+
it 'finds Veterans Day' do
|
73
|
+
Holidate.veteran(2014).must_equal Date.new(2014, 11, 11)
|
74
|
+
Holidate.veteran(2015).must_equal Date.new(2015, 11, 11)
|
75
|
+
Holidate.veteran(2016).must_equal Date.new(2016, 11, 11)
|
76
|
+
Holidate.veteran(2017).must_equal Date.new(2017, 11, 11)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe '::thanksgiving_day' do
|
81
|
+
it 'finds Thanksgiving Day' do
|
82
|
+
Holidate.thanksgiving(2014).must_equal Date.new(2014, 11, 27)
|
83
|
+
Holidate.thanksgiving(2015).must_equal Date.new(2015, 11, 26)
|
84
|
+
Holidate.thanksgiving(2016).must_equal Date.new(2016, 11, 24)
|
85
|
+
Holidate.thanksgiving(2017).must_equal Date.new(2017, 11, 23)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe '::christmas_day' do
|
90
|
+
it 'finds Christmas Day' do
|
91
|
+
Holidate.xmas(2014).must_equal Date.new(2014, 12, 25)
|
92
|
+
Holidate.xmas(2015).must_equal Date.new(2015, 12, 25)
|
93
|
+
Holidate.xmas(2016).must_equal Date.new(2016, 12, 25)
|
94
|
+
Holidate.xmas(2017).must_equal Date.new(2017, 12, 25)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe '::find' do
|
99
|
+
it 'accepts a strings, symbols, or integers as arguments' do
|
100
|
+
Holidate.find('first', 'Friday', 'December', '1985').must_equal Date.new(1985, 12, 6)
|
101
|
+
Holidate.find(:third, :wednesday , :october, :'2013').must_equal Date.new(2013, 10, 16)
|
102
|
+
Holidate.find(4, 0, 7, 1985).must_equal Date.new(1985, 7, 28)
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'allows you to search from the beginning or end of a month' do
|
106
|
+
Holidate.find('second', 'Tuesday', 'July', 2012).must_equal Date.new(2012, 7, 10)
|
107
|
+
Holidate.find('last', 'Thursday', 'January', 2006).must_equal Date.new(2006, 1, 26)
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'allows spaces, underscores, or hyphens to separate words in the first argument when searching from the end of a month' do
|
111
|
+
Holidate.find('second to last', 'Tuesday', 'September', 1991).must_equal Date.new(1991, 9, 17)
|
112
|
+
Holidate.find('fourth-from-last', 'Thursday', 'April', 2005).must_equal Date.new(2005, 4, 7)
|
113
|
+
Holidate.find(:fourth_to_last, :wednesday, :june, 1989).must_equal Date.new(1989, 6, 7)
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'ignores case for string and symbol arguments' do
|
117
|
+
Holidate.find('third', 'wednesday', 'october', 2004).must_equal Date.new(2004, 10, 20)
|
118
|
+
Holidate.find(:last, :Sunday, :August, 2006).must_equal Date.new(2006, 8, 27)
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'allows abbreviations for the first three arguments' do
|
122
|
+
Holidate.find('1st', 'thu', 'jan', 1970).must_equal Date.new(1970, 1, 1)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: holidate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-02-
|
12
|
+
date: 2015-02-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -65,7 +65,14 @@ executables: []
|
|
65
65
|
extensions: []
|
66
66
|
extra_rdoc_files: []
|
67
67
|
files:
|
68
|
+
- .gitignore
|
69
|
+
- Gemfile
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- holidate.gemspec
|
68
74
|
- lib/holidate.rb
|
75
|
+
- spec/holidate_spec.rb
|
69
76
|
homepage: http://rubygems.org/gems/holidate
|
70
77
|
licenses:
|
71
78
|
- MIT
|