date_breakup 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +82 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/date_breakup.gemspec +41 -0
- data/lib/date_breakup.rb +183 -0
- data/lib/date_breakup/version.rb +3 -0
- data/lib/date_breakup_v1.rb +215 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3f5169901ddfd246e1258b86eae3a6d7947c6f685a0c986f6b098d09519347e2
|
4
|
+
data.tar.gz: 9bad2347cb86b12f4e53552c0a2c59f22036445045f68e7580d1c6290442e56a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d73dae5066c6f626f9bbf5a7be5aed0a11cc88f29138d88f907b8aca317534468229c4ccc049b4d6583bd8bdcc40e628255d27acf1fdc2b638a177cf3336818f
|
7
|
+
data.tar.gz: e85a1c91c06bea7d3a060230f8b9295f6a5a2f7f2baead68262b88dd6c3f3f09fd39672ea16da673c081b9e6714854339a108d23f6908bf19a872a3f3b244396
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Huzaifa Saifuddin
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# DateBreakup
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'date_breakup'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle install
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install date_breakup
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Pass in 2 Date(String) & get a breakup of dates in years, months, weeks & days
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
DateBreakup::Range.between('01/01/2019', '31/12/2019').get_accurate
|
25
|
+
=> {
|
26
|
+
:years=>[
|
27
|
+
{:year=>2019, :start_date=>Sat, 01 Jan 2019, :end_date=>Sat, 31 Dec 2019}
|
28
|
+
]
|
29
|
+
}
|
30
|
+
```
|
31
|
+
|
32
|
+
### More Further Apart Dates
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
DateBreakup::Range.between('10/10/2010', '12/12/2012').get_accurate
|
36
|
+
=> {
|
37
|
+
:years=>[
|
38
|
+
{:year=>2011, :start_date=>Sat, 01 Jan 2011, :end_date=>Sat, 31 Dec 2011}
|
39
|
+
],
|
40
|
+
:months=>[
|
41
|
+
{:month=>11, :year=>2010, :start_date=>Mon, 01 Nov 2010, :end_date=>Tue, 30 Nov 2010},
|
42
|
+
{:month=>12, :year=>2010, :start_date=>Wed, 01 Dec 2010, :end_date=>Fri, 31 Dec 2010},
|
43
|
+
{:month=>1, :year=>2012, :start_date=>Sun, 01 Jan 2012, :end_date=>Tue, 31 Jan 2012},
|
44
|
+
{:month=>2, :year=>2012, :start_date=>Wed, 01 Feb 2012, :end_date=>Wed, 29 Feb 2012},
|
45
|
+
{:month=>3, :year=>2012, :start_date=>Thu, 01 Mar 2012, :end_date=>Sat, 31 Mar 2012},
|
46
|
+
{:month=>4, :year=>2012, :start_date=>Sun, 01 Apr 2012, :end_date=>Mon, 30 Apr 2012},
|
47
|
+
{:month=>5, :year=>2012, :start_date=>Tue, 01 May 2012, :end_date=>Thu, 31 May 2012},
|
48
|
+
{:month=>6, :year=>2012, :start_date=>Fri, 01 Jun 2012, :end_date=>Sat, 30 Jun 2012},
|
49
|
+
{:month=>7, :year=>2012, :start_date=>Sun, 01 Jul 2012, :end_date=>Tue, 31 Jul 2012},
|
50
|
+
{:month=>8, :year=>2012, :start_date=>Wed, 01 Aug 2012, :end_date=>Fri, 31 Aug 2012},
|
51
|
+
{:month=>9, :year=>2012, :start_date=>Sat, 01 Sep 2012, :end_date=>Sun, 30 Sep 2012},
|
52
|
+
{:month=>10, :year=>2012, :start_date=>Mon, 01 Oct 2012, :end_date=>Wed, 31 Oct 2012},
|
53
|
+
{:month=>11, :year=>2012, :start_date=>Thu, 01 Nov 2012, :end_date=>Fri, 30 Nov 2012}],
|
54
|
+
:weeks=>[
|
55
|
+
{:week=>41, :month=>10, :year=>2010, :start_date=>Mon, 11 Oct 2010, :end_date=>Sun, 17 Oct 2010},
|
56
|
+
{:week=>42, :month=>10, :year=>2010, :start_date=>Mon, 18 Oct 2010, :end_date=>Sun, 24 Oct 2010},
|
57
|
+
{:week=>43, :month=>10, :year=>2010, :start_date=>Mon, 25 Oct 2010, :end_date=>Sun, 31 Oct 2010},
|
58
|
+
{:week=>49, :month=>12, :year=>2012, :start_date=>Mon, 03 Dec 2012, :end_date=>Sun, 09 Dec 2012}],
|
59
|
+
:days=>[
|
60
|
+
{:day=>283, :month_day=>10, :month=>10, :year=>2010, :start_date=>Sun, 10 Oct 2010, :end_date=>Sun, 10 Oct 2010},
|
61
|
+
{:day=>336, :month_day=>1, :month=>12, :year=>2012, :start_date=>Sat, 01 Dec 2012, :end_date=>Sat, 01 Dec 2012},
|
62
|
+
{:day=>337, :month_day=>2, :month=>12, :year=>2012, :start_date=>Sun, 02 Dec 2012, :end_date=>Sun, 02 Dec 2012},
|
63
|
+
{:day=>345, :month_day=>10, :month=>12, :year=>2012, :start_date=>Mon, 10 Dec 2012, :end_date=>Mon, 10 Dec 2012},
|
64
|
+
{:day=>346, :month_day=>11, :month=>12, :year=>2012, :start_date=>Tue, 11 Dec 2012, :end_date=>Tue, 11 Dec 2012},
|
65
|
+
{:day=>347, :month_day=>12, :month=>12, :year=>2012, :start_date=>Wed, 12 Dec 2012, :end_date=>Wed, 12 Dec 2012}
|
66
|
+
]
|
67
|
+
}
|
68
|
+
```
|
69
|
+
|
70
|
+
<!-- ## Development
|
71
|
+
|
72
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
73
|
+
|
74
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/date_breakup. -->
|
79
|
+
|
80
|
+
## License
|
81
|
+
|
82
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "date_breakup"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "date_breakup/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "date_breakup"
|
8
|
+
spec.version = DateBreakup::VERSION
|
9
|
+
spec.authors = ["Huzaifa Saifuddin"]
|
10
|
+
spec.email = ["Huzaifa@Huzaifas-MacBook-Pro.local"]
|
11
|
+
|
12
|
+
spec.summary = "Feed in 2 dates and get a breakup of dates grouped in years, months, weeks & days"
|
13
|
+
spec.description = "Feed in 2 dates and get a breakup of dates grouped in years, months, weeks & days"
|
14
|
+
spec.homepage = "https://github.com/HuzaifaSaifuddin/date_breakup"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
|
+
end
|
31
|
+
spec.bindir = "exe"
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
38
|
+
spec.add_development_dependency "pry", "~> 1.0"
|
39
|
+
|
40
|
+
spec.add_runtime_dependency 'date', '~> 2.0'
|
41
|
+
end
|
data/lib/date_breakup.rb
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
require "date_breakup/version"
|
2
|
+
require "time_difference"
|
3
|
+
|
4
|
+
module DateBreakup
|
5
|
+
class Range
|
6
|
+
private_class_method :new
|
7
|
+
|
8
|
+
def self.between(date1, date2)
|
9
|
+
new(Date.parse(date1), Date.parse(date2))
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_years
|
13
|
+
get_year_array(@date1, @date2)
|
14
|
+
|
15
|
+
output = { years: @year_array, months: @month_array, weeks: @week_array, days: @day_array }
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_months
|
19
|
+
get_month_array(@date1, @date2)
|
20
|
+
|
21
|
+
output = { years: @year_array, months: @month_array, weeks: @week_array, days: @day_array }
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_weeks
|
25
|
+
get_week_array(@date1, @date2)
|
26
|
+
|
27
|
+
output = { years: @year_array, months: @month_array, weeks: @week_array, days: @day_array }
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_days
|
31
|
+
get_day_array(@date1, @date2)
|
32
|
+
|
33
|
+
output = { years: @year_array, months: @month_array, weeks: @week_array, days: @day_array }
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def initialize(date1, date2)
|
39
|
+
@date1 = date1
|
40
|
+
@date2 = date2
|
41
|
+
@year_array = Array.new
|
42
|
+
@month_array = Array.new
|
43
|
+
@week_array = Array.new
|
44
|
+
@day_array = Array.new
|
45
|
+
end
|
46
|
+
|
47
|
+
def get_year_array(date, end_date)
|
48
|
+
move_to_date = true
|
49
|
+
# YEAR
|
50
|
+
if move_to_date
|
51
|
+
if date.beginning_of_year == date
|
52
|
+
if date.end_of_year <= @date2
|
53
|
+
date, move_to_date = get_year_data(date)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# MONTH
|
59
|
+
if move_to_date
|
60
|
+
if date.beginning_of_month == date
|
61
|
+
if date.end_of_month <= @date2
|
62
|
+
date, move_to_date = get_month_data(date)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Week
|
68
|
+
if move_to_date
|
69
|
+
if date.beginning_of_week == date && date.end_of_week <= @date2
|
70
|
+
if date.beginning_of_week.month == date.end_of_week.month || date.end_of_week.end_of_month >= @date2
|
71
|
+
date, move_to_date = get_week_data(date)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# DAY
|
77
|
+
if move_to_date
|
78
|
+
date, move_to_date = get_date_data(date)
|
79
|
+
end
|
80
|
+
|
81
|
+
if date <= end_date
|
82
|
+
get_year_array(date, end_date)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def get_month_array(date, end_date)
|
87
|
+
move_to_date = true
|
88
|
+
# MONTH
|
89
|
+
if move_to_date
|
90
|
+
if date.beginning_of_month == date
|
91
|
+
if date.end_of_month <= @date2
|
92
|
+
date, move_to_date = get_month_data(date)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Week
|
98
|
+
if move_to_date
|
99
|
+
if date.beginning_of_week == date && date.end_of_week <= @date2
|
100
|
+
if date.beginning_of_week.month == date.end_of_week.month || date.end_of_week.end_of_month >= @date2
|
101
|
+
date, move_to_date = get_week_data(date)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# DAY
|
107
|
+
if move_to_date
|
108
|
+
date, move_to_date = get_date_data(date)
|
109
|
+
end
|
110
|
+
|
111
|
+
if date <= end_date
|
112
|
+
get_month_array(date, end_date)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def get_week_array(date, end_date)
|
117
|
+
move_to_date = true
|
118
|
+
# Week
|
119
|
+
if move_to_date
|
120
|
+
if date.beginning_of_week == date && date.end_of_week <= @date2
|
121
|
+
date, move_to_date = get_week_data(date)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# DAY
|
126
|
+
if move_to_date
|
127
|
+
date, move_to_date = get_date_data(date)
|
128
|
+
end
|
129
|
+
|
130
|
+
if date <= end_date
|
131
|
+
get_week_array(date, end_date)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def get_day_array(date, end_date)
|
136
|
+
move_to_date = true
|
137
|
+
# DAY
|
138
|
+
if move_to_date
|
139
|
+
date, move_to_date = get_date_data(date)
|
140
|
+
end
|
141
|
+
|
142
|
+
if date <= end_date
|
143
|
+
get_day_array(date, end_date)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def get_year_data(date)
|
148
|
+
year_hash = { year: date.year, start_date: date, end_date: date.end_of_year }
|
149
|
+
@year_array << year_hash
|
150
|
+
new_date = date.end_of_year + 1.day
|
151
|
+
move_to_date = false
|
152
|
+
|
153
|
+
return new_date, move_to_date
|
154
|
+
end
|
155
|
+
|
156
|
+
def get_month_data(date)
|
157
|
+
month_hash = { month: date.month, year: date.year, start_date: date, end_date: date.end_of_month }
|
158
|
+
@month_array << month_hash
|
159
|
+
new_date = date.end_of_month + 1.day
|
160
|
+
move_to_date = false
|
161
|
+
|
162
|
+
return new_date, move_to_date
|
163
|
+
end
|
164
|
+
|
165
|
+
def get_week_data(date)
|
166
|
+
week_hash = { week: date.cweek, month: date.month, year: date.year, start_date: date, end_date: date.end_of_week }
|
167
|
+
@week_array << week_hash
|
168
|
+
new_date = date.end_of_week + 1.day
|
169
|
+
move_to_date = false
|
170
|
+
|
171
|
+
return new_date, move_to_date
|
172
|
+
end
|
173
|
+
|
174
|
+
def get_date_data(date)
|
175
|
+
day_hash = { day: date.yday, month_day: date.mday, month: date.month, year: date.year, start_date: date, end_date: date }
|
176
|
+
@day_array << day_hash
|
177
|
+
new_date = date + 1.day
|
178
|
+
move_to_date = false
|
179
|
+
|
180
|
+
return new_date, move_to_date
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
@@ -0,0 +1,215 @@
|
|
1
|
+
require "date_breakup/version"
|
2
|
+
require "time_difference"
|
3
|
+
|
4
|
+
module DateBreakup
|
5
|
+
class Range
|
6
|
+
private_class_method :new
|
7
|
+
|
8
|
+
def self.between(date1, date2)
|
9
|
+
new(Date.parse(date1), Date.parse(date2))
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_accurate
|
13
|
+
years, remaining_dates_year = get_years(@date1, @date2)
|
14
|
+
|
15
|
+
months, remaining_dates_month = get_months(remaining_dates_year)
|
16
|
+
|
17
|
+
weeks, remaining_dates_week = get_weeks(remaining_dates_month)
|
18
|
+
|
19
|
+
days, remaining_dates_days = get_days(remaining_dates_week)
|
20
|
+
|
21
|
+
output = { years: years, months: months, weeks: weeks, days: days }
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def initialize(date1, date2)
|
27
|
+
@date1 = date1
|
28
|
+
@date2 = date2
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_years(date1, date2)
|
32
|
+
year_array = []
|
33
|
+
remaining_dates = []
|
34
|
+
|
35
|
+
if date1.beginning_of_year == date1 && date1.end_of_year <= date2
|
36
|
+
year_hash = { year: date1.year, start_date: date1.beginning_of_year, end_date: date1.end_of_year }
|
37
|
+
year_array << year_hash
|
38
|
+
end
|
39
|
+
|
40
|
+
get_year_range(date1, date2, year_array, year_hash)
|
41
|
+
|
42
|
+
if year_array.count > 0
|
43
|
+
if(date1 != year_array[0][:start_date])
|
44
|
+
remaining_dates_hash = { date1: date1, date2: year_array[0][:start_date] - 1 }
|
45
|
+
remaining_dates << remaining_dates_hash
|
46
|
+
end
|
47
|
+
|
48
|
+
if(date2 != year_array[-1][:end_date])
|
49
|
+
remaining_dates_hash = { date1: year_array[-1][:end_date] + 1, date2: date2 }
|
50
|
+
remaining_dates << remaining_dates_hash
|
51
|
+
end
|
52
|
+
else
|
53
|
+
remaining_dates << { date1: date1, date2: date2 }
|
54
|
+
end
|
55
|
+
|
56
|
+
return year_array, remaining_dates
|
57
|
+
end
|
58
|
+
|
59
|
+
def get_months(remaining_dates_year)
|
60
|
+
month_array = []
|
61
|
+
remaining_dates = []
|
62
|
+
|
63
|
+
remaining_dates_year.each do |rd|
|
64
|
+
date1, date2 = rd[:date1], rd[:date2]
|
65
|
+
if date1.beginning_of_month == date1 && date1.end_of_month <= date2
|
66
|
+
month_hash = { month: date1.month, year: date1.year, start_date: date1.beginning_of_month, end_date: date1.end_of_month }
|
67
|
+
month_array << month_hash
|
68
|
+
end
|
69
|
+
|
70
|
+
get_month_range(date1, date2, month_array, month_hash)
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
if month_array.count > 0
|
75
|
+
month_arrx1 = month_array.sort_by{ |ma| ma[:start_date] }.select{ |wa| wa[:year] == remaining_dates_year[0][:date1].year }.first
|
76
|
+
|
77
|
+
if month_arrx1
|
78
|
+
if(remaining_dates_year[0][:date1] != month_arrx1[:start_date])
|
79
|
+
remaining_dates_hash = { date1: remaining_dates_year[0][:date1], date2: month_arrx1[:start_date] - 1 }
|
80
|
+
remaining_dates << remaining_dates_hash
|
81
|
+
end
|
82
|
+
else
|
83
|
+
remaining_dates_hash = { date1: remaining_dates_year[0][:date1], date2: remaining_dates_year[0][:date2] }
|
84
|
+
remaining_dates << remaining_dates_hash
|
85
|
+
end
|
86
|
+
|
87
|
+
month_arrx2 = month_array.sort_by{ |ma| ma[:start_date] }.select{ |wa| wa[:year] == remaining_dates_year[-1][:date2].year }.last
|
88
|
+
if month_arrx2
|
89
|
+
if(remaining_dates_year[-1][:date2] != month_arrx2[:end_date])
|
90
|
+
remaining_dates_hash = { date1: month_arrx2[:end_date] + 1, date2: remaining_dates_year[-1][:date2] }
|
91
|
+
remaining_dates << remaining_dates_hash
|
92
|
+
end
|
93
|
+
else
|
94
|
+
remaining_dates_hash = { date1: remaining_dates_year[-1][:date1], date2: remaining_dates_year[-1][:date2] }
|
95
|
+
remaining_dates << remaining_dates_hash
|
96
|
+
end
|
97
|
+
else
|
98
|
+
remaining_dates = remaining_dates_year
|
99
|
+
end
|
100
|
+
|
101
|
+
return month_array, remaining_dates
|
102
|
+
end
|
103
|
+
|
104
|
+
def get_weeks(remaining_dates_month)
|
105
|
+
week_array = []
|
106
|
+
remaining_dates = []
|
107
|
+
|
108
|
+
remaining_dates_month.each do |rd|
|
109
|
+
date1, date2 = rd[:date1], rd[:date2]
|
110
|
+
if date1.beginning_of_week == date1 && date1.end_of_week <= date2
|
111
|
+
week_hash = { week: date1.cweek, month: date1.month, year: date1.year, start_date: date1.beginning_of_week, end_date: date1.end_of_week }
|
112
|
+
week_array << week_hash
|
113
|
+
end
|
114
|
+
|
115
|
+
get_week_range(date1, date2, week_array, week_hash)
|
116
|
+
end
|
117
|
+
|
118
|
+
if week_array.count > 0
|
119
|
+
week_arrx1 = week_array.sort_by{ |ma| ma[:start_date] }.select{ |wa| wa[:month] == remaining_dates_month[0][:date1].month }.first
|
120
|
+
if week_arrx1
|
121
|
+
if(remaining_dates_month[0][:date1] != week_arrx1[:start_date])
|
122
|
+
remaining_dates_hash = { date1: remaining_dates_month[0][:date1], date2: week_arrx1[:start_date] - 1 }
|
123
|
+
remaining_dates << remaining_dates_hash
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
week_arrx2 = week_array.sort_by{ |ma| ma[:start_date] }.select{ |wa| wa[:month] == remaining_dates_month[0][:date2].month }.last
|
128
|
+
if week_arrx2
|
129
|
+
if(remaining_dates_month[0][:date2] != week_arrx2[:end_date])
|
130
|
+
remaining_dates_hash = { date1: week_arrx2[:end_date] + 1, date2: remaining_dates_month[0][:date2] }
|
131
|
+
remaining_dates << remaining_dates_hash
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
week_arrx3 = week_array.sort_by{ |ma| ma[:start_date] }.select{ |wa| wa[:month] == remaining_dates_month[-1][:date1].month }.first
|
136
|
+
if week_arrx3
|
137
|
+
if(remaining_dates_month[0][:date1] != week_arrx3[:end_date])
|
138
|
+
remaining_dates_hash = { date1: remaining_dates_month[-1][:date1], date2: week_arrx3[:start_date] - 1 }
|
139
|
+
remaining_dates << remaining_dates_hash
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
week_arrx4 = week_array.sort_by{ |ma| ma[:start_date] }.select{ |wa| wa[:month] == remaining_dates_month[-1][:date1].month }.last
|
144
|
+
if week_arrx4
|
145
|
+
if(remaining_dates_month[-1][:date2] != week_arrx4[:end_date])
|
146
|
+
remaining_dates_hash = { date1: week_arrx4[:end_date] + 1, date2: remaining_dates_month[-1][:date2] }
|
147
|
+
remaining_dates << remaining_dates_hash
|
148
|
+
end
|
149
|
+
end
|
150
|
+
else
|
151
|
+
remaining_dates = remaining_dates_month
|
152
|
+
end
|
153
|
+
|
154
|
+
return week_array, remaining_dates
|
155
|
+
end
|
156
|
+
|
157
|
+
def get_days(remaining_dates_week)
|
158
|
+
day_array = []
|
159
|
+
remaining_dates = []
|
160
|
+
|
161
|
+
remaining_dates_week.each do |rd|
|
162
|
+
date1, date2 = rd[:date1], rd[:date2]
|
163
|
+
dates = (date1).upto(date2).to_a
|
164
|
+
|
165
|
+
dates.each do |date|
|
166
|
+
unless day_array.find{ |da| da[:start_date] == date }.present?
|
167
|
+
day_hash = { day: date.yday, month_day: date.mday, month: date.month, year: date.year, start_date: date, end_date: date }
|
168
|
+
day_array << day_hash
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
return day_array, remaining_dates
|
174
|
+
end
|
175
|
+
|
176
|
+
def get_year_range(date1, date2, year_array, year_hash)
|
177
|
+
next_year = (date1.end_of_year + 1.day)
|
178
|
+
end_next_year = next_year.end_of_year
|
179
|
+
if end_next_year <= date2
|
180
|
+
year = end_next_year.year
|
181
|
+
start_date = next_year.beginning_of_year
|
182
|
+
end_date = end_next_year
|
183
|
+
year_hash = { year: year, start_date: start_date, end_date: end_date }
|
184
|
+
year_array << year_hash
|
185
|
+
get_year_range(next_year, date2, year_array, year_hash)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def get_month_range(date1, date2, month_array, month_hash)
|
190
|
+
next_month = (date1.end_of_month + 1.day)
|
191
|
+
end_next_month = next_month.end_of_month
|
192
|
+
if end_next_month <= date2
|
193
|
+
month = end_next_month.month
|
194
|
+
start_date = next_month.beginning_of_month
|
195
|
+
end_date = end_next_month
|
196
|
+
month_hash = { month: month, year: end_next_month.year, start_date: start_date, end_date: end_date }
|
197
|
+
month_array << month_hash
|
198
|
+
get_month_range(next_month, date2, month_array, month_hash)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
def get_week_range(date1, date2, week_array, week_hash)
|
203
|
+
next_week = (date1.end_of_week + 1.day)
|
204
|
+
end_next_week = next_week.end_of_week
|
205
|
+
if end_next_week <= date2
|
206
|
+
week = end_next_week.cweek
|
207
|
+
start_date = next_week.beginning_of_week
|
208
|
+
end_date = end_next_week
|
209
|
+
week_hash = { week: week, month: end_next_week.month, year: end_next_week.year, start_date: start_date, end_date: end_date }
|
210
|
+
week_array << week_hash
|
211
|
+
get_week_range(next_week, date2, week_array, week_hash)
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date_breakup
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Huzaifa Saifuddin
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-02-19 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: date
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.0'
|
83
|
+
description: Feed in 2 dates and get a breakup of dates grouped in years, months,
|
84
|
+
weeks & days
|
85
|
+
email:
|
86
|
+
- Huzaifa@Huzaifas-MacBook-Pro.local
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- date_breakup.gemspec
|
101
|
+
- lib/date_breakup.rb
|
102
|
+
- lib/date_breakup/version.rb
|
103
|
+
- lib/date_breakup_v1.rb
|
104
|
+
homepage: https://github.com/HuzaifaSaifuddin/date_breakup
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata:
|
108
|
+
allowed_push_host: https://rubygems.org
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.7.7
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Feed in 2 dates and get a breakup of dates grouped in years, months, weeks
|
129
|
+
& days
|
130
|
+
test_files: []
|