date_diff 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +40 -0
- data/Rakefile +10 -0
- data/date_diff.gemspec +28 -0
- data/lib/date_diff.rb +48 -0
- data/lib/date_diff/version.rb +3 -0
- data/lib/en.yml +7 -0
- data/test/helper.rb +6 -0
- data/test/test_date_diff.rb +24 -0
- metadata +123 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
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,40 @@
|
|
1
|
+
# DateDiff
|
2
|
+
|
3
|
+
Gem which calculates how many years, months and days are there between two dates
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'date_diff'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install date_diff
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
require 'date_diff'
|
22
|
+
components = Date.diff(start_date, end_date)
|
23
|
+
|
24
|
+
This will return the hash of date difference in terms of years, months, days.
|
25
|
+
|
26
|
+
You can use the difference like:
|
27
|
+
|
28
|
+
components[:year], components[:month], components[:day], components[:difference]
|
29
|
+
|
30
|
+
## Examples
|
31
|
+
> Date.diff(Date.parse('2000-01-01'), Date.parse('2013-06-19'))
|
32
|
+
=> {:year => 13, :month => 5, :day => 18, :difference => "13 years 5 months 18 days"}
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
1. Fork it
|
37
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
39
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
40
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/date_diff.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'date_diff/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "date_diff"
|
8
|
+
spec.version = DateDiff::VERSION
|
9
|
+
spec.authors = ["nithin stany dsouza"]
|
10
|
+
spec.email = ["nithinstany@gmail.com"]
|
11
|
+
spec.description = %q{How many years, months and days are there between two dates}
|
12
|
+
spec.summary = %q{How many years, months and days are there between two dates}
|
13
|
+
spec.homepage = "https://github.com/nithinstany/date_diff"
|
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 "activesupport"
|
24
|
+
spec.add_development_dependency "i18n"
|
25
|
+
if RUBY_VERSION == "1.8.7"
|
26
|
+
s.add_development_dependency 'minitest', '3.2.0'
|
27
|
+
end
|
28
|
+
end
|
data/lib/date_diff.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require "date_diff/version"
|
2
|
+
require 'rubygems'
|
3
|
+
require 'active_support/all'
|
4
|
+
require 'i18n'
|
5
|
+
|
6
|
+
|
7
|
+
class Date
|
8
|
+
def self.diff(start_date, end_date)
|
9
|
+
common_year_days_in_month = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
10
|
+
start_date, end_date = end_date, start_date if start_date > end_date
|
11
|
+
|
12
|
+
start_date_month, start_date_day = start_date.month, start_date.day
|
13
|
+
end_date_month, end_date_day = end_date.month, end_date.day
|
14
|
+
|
15
|
+
day_difference = end_date_day - start_date_day
|
16
|
+
|
17
|
+
if (day_difference < 0)
|
18
|
+
day_difference = nday[start_date_month-1] - start_date_day + end_date_day
|
19
|
+
end_date_month = end_date_month -1
|
20
|
+
end
|
21
|
+
|
22
|
+
if (end_date_month < 0)
|
23
|
+
end_date_month = end_date_month + 12
|
24
|
+
end_date.year = end_date.year -1
|
25
|
+
end
|
26
|
+
|
27
|
+
month_difference = end_date_month - start_date_month
|
28
|
+
|
29
|
+
if (month_difference < 0)
|
30
|
+
month_difference = month_difference + 12
|
31
|
+
end_date.year = end_date.year -1
|
32
|
+
end
|
33
|
+
|
34
|
+
year_difference = end_date.year - start_date.year
|
35
|
+
|
36
|
+
|
37
|
+
difference_hash = { :year => year_difference, :month => month_difference, :day => day_difference }
|
38
|
+
|
39
|
+
difference_array = []
|
40
|
+
difference_hash.each do |key, value|
|
41
|
+
difference_array << value.to_s + " " + ((value > 1)? I18n.t(key.to_s.pluralize, :default => key.to_s.pluralize.to_s) : I18n.t(key.to_s, :default => key.to_s)).to_s if value > 0
|
42
|
+
end
|
43
|
+
difference_hash[:difference] = difference_array.join(" ").to_s
|
44
|
+
difference_hash
|
45
|
+
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
data/lib/en.yml
ADDED
data/test/helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
class TestDateDiff < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
def test_date_diff_method
|
7
|
+
[
|
8
|
+
[Date.parse('2013-01-01'), Date.parse('2013-01-01'), {:year => 0, :month => 0, :day => 0, :difference => ""}],
|
9
|
+
[Date.parse('2013-01-01'), Date.parse('2013-01-02'), {:year => 0, :month => 0, :day => 1, :difference => "1 day"}],
|
10
|
+
[Date.parse('2013-01-01'), Date.parse('2013-02-01'), {:year => 0, :month => 1, :day => 0, :difference => "1 month"}],
|
11
|
+
|
12
|
+
[Date.parse('2013-01-01'), Date.parse('2013-03-03'), {:year => 0, :month => 2, :day => 2, :difference => "2 months 2 days"}],
|
13
|
+
|
14
|
+
[Date.parse('2012-01-01'), Date.parse('2013-01-01'), {:year => 1, :month => 0, :day => 0, :difference => "1 year"}],
|
15
|
+
[Date.parse('2000-01-01'), Date.parse('2013-01-01'), {:year => 13, :month => 0, :day => 0, :difference => "13 years"}],
|
16
|
+
[Date.parse('2013-08-28'), Date.parse('2014-08-28'), {:year => 1, :month => 0, :day => 0, :difference => "1 year"}],
|
17
|
+
|
18
|
+
[Date.parse('2000-01-01'), Date.parse('2013-06-19'), {:year => 13, :month => 5, :day => 18, :difference => "13 years 5 months 18 days"}]
|
19
|
+
].each do |input|
|
20
|
+
assert_equal(Date.diff(input[0], input[1]), input[2])
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date_diff
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- nithin stany dsouza
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-11-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: activesupport
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: i18n
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: How many years, months and days are there between two dates
|
79
|
+
email:
|
80
|
+
- nithinstany@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- date_diff.gemspec
|
91
|
+
- lib/date_diff.rb
|
92
|
+
- lib/date_diff/version.rb
|
93
|
+
- lib/en.yml
|
94
|
+
- test/helper.rb
|
95
|
+
- test/test_date_diff.rb
|
96
|
+
homepage: https://github.com/nithinstany/date_diff
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.8.25
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: How many years, months and days are there between two dates
|
121
|
+
test_files:
|
122
|
+
- test/helper.rb
|
123
|
+
- test/test_date_diff.rb
|