years_between 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/lib/years_between.rb +56 -0
- metadata +43 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 1595cb3b2fb61bb65f04e9672f2cf4fb4ae6a27969a75ad6e80488e11c04a46e
|
|
4
|
+
data.tar.gz: 931e4be499ed39cba47bbb0bbc957a5999f201d3a935e31bb99e76469248f16e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c50dbda0316aa44a45487566cd48f55787e22893a708b087500a15402ec43737dc1e908f8ce9555aac6ce003997ad3ccd98a882e6cce01859a294756085f6651
|
|
7
|
+
data.tar.gz: a9d67e7bf3d8f5ca9f6243d3c3fb20a320c27a0122ef9e77717f6e5fc5ca424800dab971815018d04d047c0c862bb3a9d7cd596e88e36e6154c70e6dbe681ef6
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
require 'date'
|
|
2
|
+
|
|
3
|
+
class YearsBetween
|
|
4
|
+
def self.calculate_years_between(start_date, end_date, exclude: nil)
|
|
5
|
+
start_date += 1 if exclude.to_s == 'start_date' || exclude.to_s == 'both'
|
|
6
|
+
end_date -= 1 if exclude.to_s == 'end_date' || exclude.to_s == 'both'
|
|
7
|
+
|
|
8
|
+
raise 'start_date and end_date must be present' if [start_date, end_date].any?(&:nil?)
|
|
9
|
+
raise 'start_date cannot be past end_date' if start_date > end_date
|
|
10
|
+
|
|
11
|
+
start_year = start_date.year
|
|
12
|
+
start_month = start_date.month
|
|
13
|
+
start_day = start_date.day
|
|
14
|
+
|
|
15
|
+
end_year = end_date.year
|
|
16
|
+
end_month = end_date.month
|
|
17
|
+
end_day = end_date.day
|
|
18
|
+
|
|
19
|
+
start_mmdd = start_month * 100 + start_day
|
|
20
|
+
end_mmdd = end_month * 100 + end_day
|
|
21
|
+
|
|
22
|
+
if end_mmdd >= start_mmdd
|
|
23
|
+
anniversary_year = end_year
|
|
24
|
+
else
|
|
25
|
+
anniversary_year = end_year - 1
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
if start_month == 2 && start_day == 29 && !(Date.leap?(anniversary_year))
|
|
29
|
+
anniversary_month = 3
|
|
30
|
+
anniversary_day = 1
|
|
31
|
+
else
|
|
32
|
+
anniversary_month = start_month
|
|
33
|
+
anniversary_day = start_day
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
year_frac_new = anniversary_year - start_year
|
|
37
|
+
|
|
38
|
+
no_of_days = (end_date - Date.new(anniversary_year, anniversary_month, anniversary_day)).to_i + 1
|
|
39
|
+
|
|
40
|
+
if start_mmdd <= 229
|
|
41
|
+
if Date.leap?(anniversary_year)
|
|
42
|
+
tot_no_of_days = 366
|
|
43
|
+
else
|
|
44
|
+
tot_no_of_days = 365
|
|
45
|
+
end
|
|
46
|
+
else
|
|
47
|
+
if Date.leap?(anniversary_year + 1)
|
|
48
|
+
tot_no_of_days = 366
|
|
49
|
+
else
|
|
50
|
+
tot_no_of_days = 365
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
year_frac_new = year_frac_new + (no_of_days.to_f / tot_no_of_days.to_f)
|
|
55
|
+
end
|
|
56
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: years_between
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ivaldi
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2020-10-07 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Calculates years between dates including decimals
|
|
14
|
+
email:
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/years_between.rb
|
|
20
|
+
homepage: https://rubygems.org/gems/years_between
|
|
21
|
+
licenses: []
|
|
22
|
+
metadata: {}
|
|
23
|
+
post_install_message:
|
|
24
|
+
rdoc_options: []
|
|
25
|
+
require_paths:
|
|
26
|
+
- lib
|
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
28
|
+
requirements:
|
|
29
|
+
- - ">="
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '0'
|
|
32
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
33
|
+
requirements:
|
|
34
|
+
- - ">="
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: '0'
|
|
37
|
+
requirements: []
|
|
38
|
+
rubyforge_project:
|
|
39
|
+
rubygems_version: 2.7.9
|
|
40
|
+
signing_key:
|
|
41
|
+
specification_version: 4
|
|
42
|
+
summary: Calculates years between dates including decimals
|
|
43
|
+
test_files: []
|