date_helper 1.0.0
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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +34 -0
- data/Rakefile +7 -0
- data/date_helper.gemspec +24 -0
- data/lib/date_helper.rb +27 -0
- data/spec/lib/date_helper_spec.rb +89 -0
- metadata +95 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 886c8aa86a73debebd4984a1409bc9c40525e405
|
|
4
|
+
data.tar.gz: c5c03e3977ad960beeceb7c149b28e3501a3435d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 560f1be171db1b7c53c9952caa8996fd56b2a9cdde97226c00f8484792681abf380a72a52e304162c09a90ccad23dcb262a65fa05bfa0eacd0c9c80c22213cdf
|
|
7
|
+
data.tar.gz: f9a6893840b4b7f6b0abbb282bcd7501104475c012697fb185992395e26ce70d3b27ac8cfe267d9bf50392060c6ab503809d1a7787ae0aa43fdafddfdaaeaec3
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2014 Appartisan Limited
|
|
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,34 @@
|
|
|
1
|
+
# DateHelper
|
|
2
|
+
|
|
3
|
+
Adds `months_between` method to the `Date` class.
|
|
4
|
+
|
|
5
|
+
`months_between` returns an array of `Date` objects for the first day of each month, inclusive of the starting and ending months.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
gem 'date_helper'
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install date_helper
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
```ruby
|
|
23
|
+
xmas = Date.parse("2013-12-25")
|
|
24
|
+
hksar_establishment_day = Date.parse("2014-07-01")
|
|
25
|
+
|
|
26
|
+
Date.months_between(xmas,hksar_establishment_day)
|
|
27
|
+
```
|
|
28
|
+
## Contributing
|
|
29
|
+
|
|
30
|
+
1. Fork it
|
|
31
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
32
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
33
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
34
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/date_helper.gemspec
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'date_helper'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "date_helper"
|
|
8
|
+
spec.version = DateHelper::VERSION
|
|
9
|
+
spec.authors = ["Larry Salibra"]
|
|
10
|
+
spec.email = ["larry@pay4bugs.com"]
|
|
11
|
+
spec.description = "Adds `months_between` method to the `Date` class."
|
|
12
|
+
spec.summary = spec.description
|
|
13
|
+
spec.homepage = "https://github.com/pay4bugs/date_helper"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files`.split($/)
|
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
spec.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
|
22
|
+
spec.add_development_dependency "rake"
|
|
23
|
+
spec.add_development_dependency "rspec"
|
|
24
|
+
end
|
data/lib/date_helper.rb
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require 'date'
|
|
2
|
+
|
|
3
|
+
module DateHelper
|
|
4
|
+
VERSION = "1.0.0"
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
class Date
|
|
8
|
+
|
|
9
|
+
# return array with one entry for each month between date1 and date2 (inclusive)
|
|
10
|
+
def self.months_between(date1, date2)
|
|
11
|
+
months = []
|
|
12
|
+
if date2 < date1
|
|
13
|
+
start_date = Date.civil(date2.year, date2.month, 1)
|
|
14
|
+
end_date = Date.civil(date1.year, date1.month, 1)
|
|
15
|
+
else
|
|
16
|
+
start_date = Date.civil(date1.year, date1.month, 1)
|
|
17
|
+
end_date = Date.civil(date2.year, date2.month, 1)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
while (start_date < end_date)
|
|
21
|
+
months << start_date
|
|
22
|
+
start_date = start_date >>1
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
months << end_date
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
describe DateHelper do
|
|
2
|
+
context "two dates that are the same" do
|
|
3
|
+
before :each do
|
|
4
|
+
date = Date.parse("2014-01-15")
|
|
5
|
+
@result = Date.months_between(date,date)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "should return 1 month" do
|
|
9
|
+
expect(@result).to have(1).items
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "month should be first day of month" do
|
|
13
|
+
expect(@result[0]).to be == Date.parse("2014-01-01")
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
context "two dates in same month" do
|
|
18
|
+
before :each do
|
|
19
|
+
@earlier_date = Date.parse("2014-01-01")
|
|
20
|
+
@later_date = Date.parse("2014-01-15")
|
|
21
|
+
end
|
|
22
|
+
context "earlier date first" do
|
|
23
|
+
before :each do
|
|
24
|
+
@result = Date.months_between(@earlier_date,@later_date)
|
|
25
|
+
end
|
|
26
|
+
it "should return 1 month" do
|
|
27
|
+
expect(@result).to have(1).items
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "month should be first day of month" do
|
|
31
|
+
expect(@result[0]).to be == Date.parse("2014-01-01")
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
context "later date first" do
|
|
35
|
+
before :each do
|
|
36
|
+
@result = Date.months_between(@later_date,@earlier_date)
|
|
37
|
+
end
|
|
38
|
+
it "should return 1 month" do
|
|
39
|
+
expect(@result).to have(1).items
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "month should be first day of month" do
|
|
43
|
+
expect(@result[0]).to be == Date.parse("2014-01-01")
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
context "two dates in different months" do
|
|
49
|
+
before :each do
|
|
50
|
+
@earlier_date = Date.parse("2013-08-03")
|
|
51
|
+
@later_date = Date.parse("2014-01-15")
|
|
52
|
+
end
|
|
53
|
+
context "earlier date first" do
|
|
54
|
+
before :each do
|
|
55
|
+
@result = Date.months_between(@earlier_date,@later_date)
|
|
56
|
+
end
|
|
57
|
+
it "should return 6 months" do
|
|
58
|
+
expect(@result).to have(6).items
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it "month should be first day of month and in ascending order" do
|
|
62
|
+
expect(@result[0]).to be == Date.parse("2013-08-01")
|
|
63
|
+
expect(@result[1]).to be == Date.parse("2013-09-01")
|
|
64
|
+
expect(@result[2]).to be == Date.parse("2013-10-01")
|
|
65
|
+
expect(@result[3]).to be == Date.parse("2013-11-01")
|
|
66
|
+
expect(@result[4]).to be == Date.parse("2013-12-01")
|
|
67
|
+
expect(@result[5]).to be == Date.parse("2014-01-01")
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
context "later date first" do
|
|
71
|
+
before :each do
|
|
72
|
+
@result = Date.months_between(@later_date,@earlier_date)
|
|
73
|
+
end
|
|
74
|
+
it "should return 6 months" do
|
|
75
|
+
expect(@result).to have(6).items
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it "month should be first day of month and in ascending order" do
|
|
79
|
+
expect(@result[0]).to be == Date.parse("2013-08-01")
|
|
80
|
+
expect(@result[1]).to be == Date.parse("2013-09-01")
|
|
81
|
+
expect(@result[2]).to be == Date.parse("2013-10-01")
|
|
82
|
+
expect(@result[3]).to be == Date.parse("2013-11-01")
|
|
83
|
+
expect(@result[4]).to be == Date.parse("2013-12-01")
|
|
84
|
+
expect(@result[5]).to be == Date.parse("2014-01-01")
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: date_helper
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Larry Salibra
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2014-04-26 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ~>
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.3'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ~>
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.3'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - '>='
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - '>='
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - '>='
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - '>='
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
description: Adds `months_between` method to the `Date` class.
|
|
56
|
+
email:
|
|
57
|
+
- larry@pay4bugs.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- .gitignore
|
|
63
|
+
- Gemfile
|
|
64
|
+
- LICENSE.txt
|
|
65
|
+
- README.md
|
|
66
|
+
- Rakefile
|
|
67
|
+
- date_helper.gemspec
|
|
68
|
+
- lib/date_helper.rb
|
|
69
|
+
- spec/lib/date_helper_spec.rb
|
|
70
|
+
homepage: https://github.com/pay4bugs/date_helper
|
|
71
|
+
licenses:
|
|
72
|
+
- MIT
|
|
73
|
+
metadata: {}
|
|
74
|
+
post_install_message:
|
|
75
|
+
rdoc_options: []
|
|
76
|
+
require_paths:
|
|
77
|
+
- lib
|
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - '>='
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
|
+
requirements:
|
|
85
|
+
- - '>='
|
|
86
|
+
- !ruby/object:Gem::Version
|
|
87
|
+
version: '0'
|
|
88
|
+
requirements: []
|
|
89
|
+
rubyforge_project:
|
|
90
|
+
rubygems_version: 2.1.8
|
|
91
|
+
signing_key:
|
|
92
|
+
specification_version: 4
|
|
93
|
+
summary: Adds `months_between` method to the `Date` class.
|
|
94
|
+
test_files:
|
|
95
|
+
- spec/lib/date_helper_spec.rb
|