hg_date_breakup 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5cb25588e7c354ae7e4be09bc472a3f4590ea500712f76325cf372273b8f53cf
4
+ data.tar.gz: 79f3fa99566991d3648abce2d82c470cf48f31faa89eabe712d9b8bf5a842ddd
5
+ SHA512:
6
+ metadata.gz: 01b7af55a33b7033d64156df262297078a59e22429a089248c1aa62ac274015410ecfbaa72442f0232c39cc5b50ea12c144fce69d9d5a6232efd66d80965bfc0
7
+ data.tar.gz: 60647ae0dba39f7412c660908c624e27e517174dbe819aa1426ce511843dd5632c3c013bd6ea525fac8c16bf52978c943d573021a84cea3283139cba91048309
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in hg_date_breakup.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Mohit Maniar
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,170 @@
1
+ # DateBreakup
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'hg_date_breakup'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install hg_date_breakup
18
+
19
+ ## Usage
20
+
21
+ ### Pass in 2 Date(String) & get a breakup of dates in years, months, weeks & days
22
+
23
+ #### FOR YEARS (BREAKUP in YEARS, MONTHS, WEEKS, DAYS)
24
+ ```ruby
25
+ DateBreakup::Range.between('01/01/2019', '31/12/2019').get_years
26
+ => {
27
+ :years=>[
28
+ {:year=>2019, :start_date=>Sat, 01 Jan 2019, :end_date=>Sat, 31 Dec 2019}
29
+ ],
30
+ :months=>[],
31
+ :weeks=>[],
32
+ :days=>[]
33
+ }
34
+ ```
35
+
36
+ #### FOR MONTHS (BREAKUP in MONTHS, WEEKS, DAYS)
37
+ ```ruby
38
+ DateBreakup::Range.between('01/01/2019', '31/12/2019').get_months
39
+ => {
40
+ :years=>[],
41
+ :months=>[
42
+ {:month=>1, :year=>2019, :start_date=>Tue, 01 Jan 2019, :end_date=>Thu, 31 Jan 2019},
43
+ {:month=>2, :year=>2019, :start_date=>Fri, 01 Feb 2019, :end_date=>Thu, 28 Feb 2019},
44
+ {:month=>3, :year=>2019, :start_date=>Fri, 01 Mar 2019, :end_date=>Sun, 31 Mar 2019},
45
+ {:month=>4, :year=>2019, :start_date=>Mon, 01 Apr 2019, :end_date=>Tue, 30 Apr 2019},
46
+ {:month=>5, :year=>2019, :start_date=>Wed, 01 May 2019, :end_date=>Fri, 31 May 2019},
47
+ {:month=>6, :year=>2019, :start_date=>Sat, 01 Jun 2019, :end_date=>Sun, 30 Jun 2019},
48
+ {:month=>7, :year=>2019, :start_date=>Mon, 01 Jul 2019, :end_date=>Wed, 31 Jul 2019},
49
+ {:month=>8, :year=>2019, :start_date=>Thu, 01 Aug 2019, :end_date=>Sat, 31 Aug 2019},
50
+ {:month=>9, :year=>2019, :start_date=>Sun, 01 Sep 2019, :end_date=>Mon, 30 Sep 2019},
51
+ {:month=>10, :year=>2019, :start_date=>Tue, 01 Oct 2019, :end_date=>Thu, 31 Oct 2019},
52
+ {:month=>11, :year=>2019, :start_date=>Fri, 01 Nov 2019, :end_date=>Sat, 30 Nov 2019},
53
+ {:month=>12, :year=>2019, :start_date=>Sun, 01 Dec 2019, :end_date=>Tue, 31 Dec 2019}
54
+ ],
55
+ :weeks=>[],
56
+ :days=>[]
57
+ }
58
+ ```
59
+ #### FOR WEEKS (BREAKUP in WEEKS, DAYS)
60
+ ```ruby
61
+ DateBreakup::Range.between('01/01/2019', '31/12/2019').get_weeks
62
+ => {
63
+ :years=>[],
64
+ :months=>[],
65
+ :weeks=>[
66
+ {:week=>2, :month=>1, :year=>2019, :start_date=>Mon, 07 Jan 2019, :end_date=>Sun, 13 Jan 2019},
67
+ {:week=>3, :month=>1, :year=>2019, :start_date=>Mon, 14 Jan 2019, :end_date=>Sun, 20 Jan 2019},
68
+ {:week=>4, :month=>1, :year=>2019, :start_date=>Mon, 21 Jan 2019, :end_date=>Sun, 27 Jan 2019},
69
+ {:week=>5, :month=>1, :year=>2019, :start_date=>Mon, 28 Jan 2019, :end_date=>Sun, 03 Feb 2019},
70
+ {:week=>6, :month=>2, :year=>2019, :start_date=>Mon, 04 Feb 2019, :end_date=>Sun, 10 Feb 2019},
71
+ {:week=>7, :month=>2, :year=>2019, :start_date=>Mon, 11 Feb 2019, :end_date=>Sun, 17 Feb 2019},
72
+ {:week=>8, :month=>2, :year=>2019, :start_date=>Mon, 18 Feb 2019, :end_date=>Sun, 24 Feb 2019},
73
+ {:week=>9, :month=>2, :year=>2019, :start_date=>Mon, 25 Feb 2019, :end_date=>Sun, 03 Mar 2019},
74
+ {:week=>10, :month=>3, :year=>2019, :start_date=>Mon, 04 Mar 2019, :end_date=>Sun, 10 Mar 2019},
75
+ ...
76
+ {:week=>43, :month=>10, :year=>2019, :start_date=>Mon, 21 Oct 2019, :end_date=>Sun, 27 Oct 2019},
77
+ {:week=>44, :month=>10, :year=>2019, :start_date=>Mon, 28 Oct 2019, :end_date=>Sun, 03 Nov 2019},
78
+ {:week=>45, :month=>11, :year=>2019, :start_date=>Mon, 04 Nov 2019, :end_date=>Sun, 10 Nov 2019},
79
+ {:week=>46, :month=>11, :year=>2019, :start_date=>Mon, 11 Nov 2019, :end_date=>Sun, 17 Nov 2019},
80
+ {:week=>47, :month=>11, :year=>2019, :start_date=>Mon, 18 Nov 2019, :end_date=>Sun, 24 Nov 2019},
81
+ {:week=>48, :month=>11, :year=>2019, :start_date=>Mon, 25 Nov 2019, :end_date=>Sun, 01 Dec 2019},
82
+ {:week=>49, :month=>12, :year=>2019, :start_date=>Mon, 02 Dec 2019, :end_date=>Sun, 08 Dec 2019},
83
+ {:week=>50, :month=>12, :year=>2019, :start_date=>Mon, 09 Dec 2019, :end_date=>Sun, 15 Dec 2019},
84
+ {:week=>51, :month=>12, :year=>2019, :start_date=>Mon, 16 Dec 2019, :end_date=>Sun, 22 Dec 2019},
85
+ {:week=>52, :month=>12, :year=>2019, :start_date=>Mon, 23 Dec 2019, :end_date=>Sun, 29 Dec 2019}
86
+ ],
87
+ :days=>[
88
+ {:day=>1, :month_day=>1, :month=>1, :year=>2019, :start_date=>Tue, 01 Jan 2019, :end_date=>Tue, 01 Jan 2019},
89
+ {:day=>2, :month_day=>2, :month=>1, :year=>2019, :start_date=>Wed, 02 Jan 2019, :end_date=>Wed, 02 Jan 2019},
90
+ {:day=>3, :month_day=>3, :month=>1, :year=>2019, :start_date=>Thu, 03 Jan 2019, :end_date=>Thu, 03 Jan 2019},
91
+ {:day=>4, :month_day=>4, :month=>1, :year=>2019, :start_date=>Fri, 04 Jan 2019, :end_date=>Fri, 04 Jan 2019},
92
+ {:day=>5, :month_day=>5, :month=>1, :year=>2019, :start_date=>Sat, 05 Jan 2019, :end_date=>Sat, 05 Jan 2019},
93
+ {:day=>6, :month_day=>6, :month=>1, :year=>2019, :start_date=>Sun, 06 Jan 2019, :end_date=>Sun, 06 Jan 2019},
94
+ {:day=>364, :month_day=>30, :month=>12, :year=>2019, :start_date=>Mon, 30 Dec 2019, :end_date=>Mon, 30 Dec 2019},
95
+ {:day=>365, :month_day=>31, :month=>12, :year=>2019, :start_date=>Tue, 31 Dec 2019, :end_date=>Tue, 31 Dec 2019}
96
+ ]
97
+ }
98
+ ```
99
+ #### FOR DAYS (BREAKUP in DAYS)
100
+ ```ruby
101
+ DateBreakup::Range.between('01/01/2019', '31/12/2019').get_days
102
+ => {
103
+ :years=>[],
104
+ :months=>[],
105
+ :weeks=>[],
106
+ :days=>[
107
+ {:day=>1, :month_day=>1, :month=>1, :year=>2019, :start_date=>Tue, 01 Jan 2019, :end_date=>Tue, 01 Jan 2019},
108
+ {:day=>2, :month_day=>2, :month=>1, :year=>2019, :start_date=>Wed, 02 Jan 2019, :end_date=>Wed, 02 Jan 2019},
109
+ {:day=>3, :month_day=>3, :month=>1, :year=>2019, :start_date=>Thu, 03 Jan 2019, :end_date=>Thu, 03 Jan 2019},
110
+ {:day=>4, :month_day=>4, :month=>1, :year=>2019, :start_date=>Fri, 04 Jan 2019, :end_date=>Fri, 04 Jan 2019},
111
+ {:day=>5, :month_day=>5, :month=>1, :year=>2019, :start_date=>Sat, 05 Jan 2019, :end_date=>Sat, 05 Jan 2019},
112
+ {:day=>6, :month_day=>6, :month=>1, :year=>2019, :start_date=>Sun, 06 Jan 2019, :end_date=>Sun, 06 Jan 2019},
113
+ ...
114
+ {:day=>364, :month_day=>30, :month=>12, :year=>2019, :start_date=>Mon, 30 Dec 2019, :end_date=>Mon, 30 Dec 2019},
115
+ {:day=>365, :month_day=>31, :month=>12, :year=>2019, :start_date=>Tue, 31 Dec 2019, :end_date=>Tue, 31 Dec 2019}
116
+ ]
117
+ }
118
+ ```
119
+
120
+ ### More Further Apart Dates
121
+
122
+ ```ruby
123
+ DateBreakup::Range.between('10/10/2010', '12/12/2012').get_years
124
+ => {
125
+ :years=>[
126
+ {:year=>2011, :start_date=>Sat, 01 Jan 2011, :end_date=>Sat, 31 Dec 2011}
127
+ ],
128
+ :months=>[
129
+ {:month=>11, :year=>2010, :start_date=>Mon, 01 Nov 2010, :end_date=>Tue, 30 Nov 2010},
130
+ {:month=>12, :year=>2010, :start_date=>Wed, 01 Dec 2010, :end_date=>Fri, 31 Dec 2010},
131
+ {:month=>1, :year=>2012, :start_date=>Sun, 01 Jan 2012, :end_date=>Tue, 31 Jan 2012},
132
+ {:month=>2, :year=>2012, :start_date=>Wed, 01 Feb 2012, :end_date=>Wed, 29 Feb 2012},
133
+ {:month=>3, :year=>2012, :start_date=>Thu, 01 Mar 2012, :end_date=>Sat, 31 Mar 2012},
134
+ {:month=>4, :year=>2012, :start_date=>Sun, 01 Apr 2012, :end_date=>Mon, 30 Apr 2012},
135
+ {:month=>5, :year=>2012, :start_date=>Tue, 01 May 2012, :end_date=>Thu, 31 May 2012},
136
+ {:month=>6, :year=>2012, :start_date=>Fri, 01 Jun 2012, :end_date=>Sat, 30 Jun 2012},
137
+ {:month=>7, :year=>2012, :start_date=>Sun, 01 Jul 2012, :end_date=>Tue, 31 Jul 2012},
138
+ {:month=>8, :year=>2012, :start_date=>Wed, 01 Aug 2012, :end_date=>Fri, 31 Aug 2012},
139
+ {:month=>9, :year=>2012, :start_date=>Sat, 01 Sep 2012, :end_date=>Sun, 30 Sep 2012},
140
+ {:month=>10, :year=>2012, :start_date=>Mon, 01 Oct 2012, :end_date=>Wed, 31 Oct 2012},
141
+ {:month=>11, :year=>2012, :start_date=>Thu, 01 Nov 2012, :end_date=>Fri, 30 Nov 2012}],
142
+ :weeks=>[
143
+ {:week=>41, :month=>10, :year=>2010, :start_date=>Mon, 11 Oct 2010, :end_date=>Sun, 17 Oct 2010},
144
+ {:week=>42, :month=>10, :year=>2010, :start_date=>Mon, 18 Oct 2010, :end_date=>Sun, 24 Oct 2010},
145
+ {:week=>43, :month=>10, :year=>2010, :start_date=>Mon, 25 Oct 2010, :end_date=>Sun, 31 Oct 2010},
146
+ {:week=>49, :month=>12, :year=>2012, :start_date=>Mon, 03 Dec 2012, :end_date=>Sun, 09 Dec 2012}],
147
+ :days=>[
148
+ {:day=>283, :month_day=>10, :month=>10, :year=>2010, :start_date=>Sun, 10 Oct 2010, :end_date=>Sun, 10 Oct 2010},
149
+ {:day=>336, :month_day=>1, :month=>12, :year=>2012, :start_date=>Sat, 01 Dec 2012, :end_date=>Sat, 01 Dec 2012},
150
+ {:day=>337, :month_day=>2, :month=>12, :year=>2012, :start_date=>Sun, 02 Dec 2012, :end_date=>Sun, 02 Dec 2012},
151
+ {:day=>345, :month_day=>10, :month=>12, :year=>2012, :start_date=>Mon, 10 Dec 2012, :end_date=>Mon, 10 Dec 2012},
152
+ {:day=>346, :month_day=>11, :month=>12, :year=>2012, :start_date=>Tue, 11 Dec 2012, :end_date=>Tue, 11 Dec 2012},
153
+ {:day=>347, :month_day=>12, :month=>12, :year=>2012, :start_date=>Wed, 12 Dec 2012, :end_date=>Wed, 12 Dec 2012}
154
+ ]
155
+ }
156
+ ```
157
+
158
+ <!-- ## Development
159
+
160
+ 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.
161
+
162
+ 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).
163
+
164
+ ## Contributing
165
+
166
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/date_breakup. -->
167
+
168
+ ## License
169
+
170
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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 = "hg_date_breakup"
8
+ spec.version = DateBreakup::VERSION
9
+ spec.authors = ["Mohit Maniar", "Anoop Bajpai"]
10
+ spec.email = ["85148901+mohit-maniar@users.noreply.github.com"]
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/healthgraph/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', '~> 3.0'
41
+ end
@@ -0,0 +1,3 @@
1
+ module DateBreakup
2
+ VERSION = "1.0.0"
3
+ end
@@ -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: hg_date_breakup
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mohit Maniar
8
+ - Anoop Bajpai
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2021-06-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.16'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.16'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: pry
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '1.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: date
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '3.0'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3.0'
84
+ description: Feed in 2 dates and get a breakup of dates grouped in years, months,
85
+ weeks & days
86
+ email:
87
+ - 85148901+mohit-maniar@users.noreply.github.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".travis.yml"
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - hg_date_breakup.gemspec
102
+ - lib/date_breakup.rb
103
+ - lib/date_breakup/version.rb
104
+ - lib/date_breakup_v1.rb
105
+ homepage: https://github.com/healthgraph/date_breakup
106
+ licenses:
107
+ - MIT
108
+ metadata:
109
+ allowed_push_host: https://rubygems.org
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubygems_version: 3.1.4
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: []