0.0
No release in over 3 years
Low commit activity in last 3 years
Addition and subtraction for month range.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

MonthRange

Calculate month range union and difference including unterminated end.

Installation

Add this line to your application's Gemfile:

gem 'month_range'

And then execute:

$ bundle install

Usage

  • Month object is MonthRange::Month object. It is special Date object which day is beginning of month. Like this.
Date.parse('2020-01-01') is valid.
Date.parse('2020-01-02') is not valid.
  • range start (called as start_month) is only Month.
  • range end is month or nil is valid.

Example

require 'month_range'

# 和

from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
range_array = [Date.parse('2020-03-01'), Date.parse('2020-05-01')]
MonthRange::Service.add(range_array, from_range_arrays)
# => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-05-01 ((2458971j,0s,0n),+0s,2299161j)>], [#<Date: 2020-07-01 ((2459032j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]


# 和(例:複数期間にまたがる)
from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
range_array = [Date.parse('2020-03-01'), Date.parse('2020-08-01')]
MonthRange::Service.add(range_array, from_range_arrays)
# => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]


# 和(例:隣り合った期間を連結)
from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
range_array = [Date.parse('2020-05-01'), Date.parse('2020-06-01')]
MonthRange::Service.add(range_array, from_range_arrays)
# => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]


# 和(例:終端なし)
from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
range_array = [Date.parse('2020-05-01'), nil]
MonthRange::Service.add(range_array, from_range_arrays)
# => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, nil]]


# 差
from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
range_array = [Date.parse('2020-03-01'), Date.parse('2020-05-01')]
MonthRange::Service.subtraction(range_array, from_range_arrays)
# => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-02-01 ((2458881j,0s,0n),+0s,2299161j)>], [#<Date: 2020-07-01 ((2459032j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]


# 差(例:複数期間にまたがる)
from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
range_array = [Date.parse('2020-03-01'), Date.parse('2020-08-01')]
MonthRange::Service.subtraction(range_array, from_range_arrays)
# => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-02-01 ((2458881j,0s,0n),+0s,2299161j)>], [#<Date: 2020-09-01 ((2459094j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]



# 差(例:期間内)
from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
range_array = [Date.parse('2020-02-01'), Date.parse('2020-03-01')]
MonthRange::Service.subtraction(range_array, from_range_arrays)
# => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>], [#<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>, #<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>], [#<Date: 2020-07-01 ((2459032j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]



# 差(例:終端なし)
from_range_arrays = [[Date.parse('2020-01-01'), Date.parse('2020-04-01')],[Date.parse('2020-07-01'), Date.parse('2020-10-01')],[Date.parse('2020-12-01'), nil]]
range_array = [Date.parse('2020-05-01'), nil]
MonthRange::Service.subtraction(range_array, from_range_arrays)
# => [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>]]

Document

https://rubydoc.info/gems/month_range

Development

TODO

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/month_range. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the MonthRange project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.