Skip to content

Commit

Permalink
Add methods [days, weeks, months, years]_between
Browse files Browse the repository at this point in the history
Inspired by rails/rails#27966
  • Loading branch information
bogdanvlviv committed Feb 12, 2017
1 parent c490b45 commit ff7c462
Show file tree
Hide file tree
Showing 18 changed files with 273 additions and 117 deletions.
74 changes: 0 additions & 74 deletions CODE_OF_CONDUCT.md

This file was deleted.

1 change: 0 additions & 1 deletion Gemfile
@@ -1,4 +1,3 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in general_time.gemspec
gemspec
139 changes: 129 additions & 10 deletions README.md
@@ -1,9 +1,5 @@
# GeneralTime

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/general_time`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

## Installation

Add this line to your application's Gemfile:
Expand All @@ -22,18 +18,141 @@ Or install it yourself as:

## Usage

TODO: Write usage instructions here
### #days_between

## Development
Formula: `((date2.yday - date1.yday) + 365 * (date2.year - date1.year)).abs`

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
```ruby
require "date"

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).
Date.new(2017, 2, 10).days_between(Date.new(2017, 2, 10)) # => 0

## Contributing
Date.new(2017, 2, 10).days_between(Date.new(2017, 2, 10)) # => 0
Date.new(2017, 2, 10).days_between(Date.new(2017, 2, 10)) # => 0

Date.new(2017, 2, 9).days_between(Date.new(2017, 2, 10)) # => 1
Date.new(2017, 2, 10).days_between(Date.new(2017, 2, 9)) # => 1
Date.new(2017, 2, 1).days_between(Date.new(2017, 2, 10)) # => 9
Date.new(2017, 2, 10).days_between(Date.new(2017, 2, 1)) # => 9
Date.new(2016, 2, 10).days_between(Date.new(2017, 2, 10)) # => 365
Date.new(2017, 2, 10).days_between(Date.new(2016, 2, 10)) # => 365
```

```ruby
Time.new(2017, 2, 10, 10, 10, 10).days_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 0

Time.new(2017, 2, 10, 9, 10, 10).days_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 0
Time.new(2017, 2, 10, 10, 10, 10).days_between(Time.new(2017, 2, 10, 9, 10, 10)) # => 0

Time.new(2017, 2, 9, 10, 10, 10).days_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 1
Time.new(2017, 2, 10, 10, 10, 10).days_between(Time.new(2017, 2, 9, 10, 10, 10)) # => 1
Time.new(2017, 2, 1, 10, 10, 10).days_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 9
Time.new(2017, 2, 10, 10, 10, 10).days_between(Time.new(2017, 2, 1, 10, 10, 10)) # => 9
Time.new(2016, 2, 10, 10, 10, 10).days_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 365
Time.new(2017, 2, 10, 10, 10, 10).days_between(Time.new(2016, 2, 10, 10, 10, 10)) # => 365
```

### #weeks_between

Formula: `((date2.cweek - date1.cweek) + 52 * (date2.year - date1.year)).abs`

```ruby
require "date"

Date.new(2017, 2, 10).weeks_between(Date.new(2017, 2, 10)) # => 0

Date.new(2017, 2, 10).weeks_between(Date.new(2017, 2, 10)) # => 0
Date.new(2017, 2, 10).weeks_between(Date.new(2017, 2, 10)) # => 0

Date.new(2017, 2, 3).weeks_between(Date.new(2017, 2, 10)) # => 1
Date.new(2017, 2, 10).weeks_between(Date.new(2017, 2, 3)) # => 1
Date.new(2016, 12, 9).weeks_between(Date.new(2017, 2, 10)) # => 9
Date.new(2017, 2, 10).weeks_between(Date.new(2016, 12, 9)) # => 9
Date.new(2016, 2, 10).weeks_between(Date.new(2017, 2, 10)) # => 52
Date.new(2017, 2, 10).weeks_between(Date.new(2016, 2, 10)) # => 52
```

```ruby
Time.new(2017, 2, 10, 10, 10, 10).weeks_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 0

Time.new(2017, 2, 10, 9, 10, 10).weeks_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 0
Time.new(2017, 2, 10, 10, 10, 10).weeks_between(Time.new(2017, 2, 10, 9, 10, 10)) # => 0

Time.new(2017, 2, 3, 10, 10, 10).weeks_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 1
Time.new(2017, 2, 10, 10, 10, 10).weeks_between(Time.new(2017, 2, 3, 10, 10, 10)) # => 1
Time.new(2016, 12, 9, 10, 10, 10).weeks_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 9
Time.new(2017, 2, 10, 10, 10, 10).weeks_between(Time.new(2016, 12, 9, 10, 10, 10)) # => 9
Time.new(2016, 2, 10, 10, 10, 10).weeks_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 52
Time.new(2017, 2, 10, 10, 10, 10).weeks_between(Time.new(2016, 2, 10, 10, 10, 10)) # => 52
```

### #months_between

Formula: `((date2.month - date1.month) + 12 * (date2.year - date1.year)).abs`

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/general_time. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
```ruby
require "date"

Date.new(2017, 2, 10).months_between(Date.new(2017, 2, 10)) # => 0

Date.new(2017, 2, 9).months_between(Date.new(2017, 2, 10)) # => 0
Date.new(2017, 2, 10).months_between(Date.new(2017, 2, 9)) # => 0

Date.new(2017, 1, 10).months_between(Date.new(2017, 2, 10)) # => 1
Date.new(2017, 2, 10).months_between(Date.new(2017, 1, 10)) # => 1
Date.new(2016, 5, 10).months_between(Date.new(2017, 2, 10)) # => 9
Date.new(2017, 2, 10).months_between(Date.new(2016, 5, 1)) # => 9
Date.new(2016, 2, 10).months_between(Date.new(2017, 2, 10)) # => 12
Date.new(2017, 2, 10).months_between(Date.new(2016, 2, 10)) # => 12
```

```ruby
Time.new(2017, 2, 10, 10, 10, 10).months_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 0

Time.new(2017, 2, 9, 10, 10, 10).months_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 0
Time.new(2017, 2, 10, 10, 10, 10).months_between(Time.new(2017, 2, 9, 10, 10, 10)) # => 0

Time.new(2017, 1, 10, 10, 10, 10).months_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 1
Time.new(2017, 2, 10, 10, 10, 10).months_between(Time.new(2017, 1, 10, 10, 10, 10)) # => 1
Time.new(2016, 5, 10, 10, 10, 10).months_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 9
Time.new(2017, 2, 10, 10, 10, 10).months_between(Time.new(2016, 5, 1, 10, 10, 10)) # => 9
Time.new(2016, 2, 10, 10, 10, 10).months_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 12
Time.new(2017, 2, 10, 10, 10, 10).months_between(Time.new(2016, 2, 10, 10, 10, 10)) # => 12
```

### #years_between

Formula: `(date2.year - date1.year).abs`

```ruby
require "date"

Date.new(2017, 2, 10).years_between(Date.new(2017, 2, 10)) # => 0

Date.new(2017, 1, 10).years_between(Date.new(2017, 2, 10)) # => 0
Date.new(2017, 2, 10).years_between(Date.new(2017, 1, 10)) # => 0

Date.new(2016, 2, 10).years_between(Date.new(2017, 2, 10)) # => 1
Date.new(2017, 2, 10).years_between(Date.new(2016, 2, 10)) # => 1
Date.new(2008, 2, 10).years_between(Date.new(2017, 2, 10)) # => 9
Date.new(2017, 2, 10).years_between(Date.new(2008, 2, 10)) # => 9
```

```ruby
Time.new(2017, 2, 10, 10, 10, 10).years_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 0

Time.new(2017, 1, 10, 10, 10, 10).years_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 0
Time.new(2017, 2, 10, 10, 10, 10).years_between(Time.new(2017, 1, 10, 10, 10, 10)) # => 0

Time.new(2016, 2, 10, 10, 10, 10).years_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 1
Time.new(2017, 2, 10, 10, 10, 10).years_between(Time.new(2016, 2, 10, 10, 10, 10)) # => 1
Time.new(2008, 2, 10, 10, 10, 10).years_between(Time.new(2017, 2, 10, 10, 10, 10)) # => 9
Time.new(2017, 2, 10, 10, 10, 10).years_between(Time.new(2008, 2, 10, 10, 10, 10)) # => 9
```

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/bogdanvlviv/general_time. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.

## License

Expand Down
7 changes: 0 additions & 7 deletions bin/console
Expand Up @@ -3,12 +3,5 @@
require "bundler/setup"
require "general_time"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start
2 changes: 0 additions & 2 deletions bin/setup
Expand Up @@ -4,5 +4,3 @@ IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
21 changes: 4 additions & 17 deletions general_time.gemspec
Expand Up @@ -9,25 +9,12 @@ Gem::Specification.new do |spec|
spec.authors = ["bogdanvlviv"]
spec.email = ["bogdanvlviv@gmail.com"]

spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
spec.description = %q{TODO: Write a longer description or delete this line.}
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.summary = "Allows getting general time between two instances of Date/Time classes"
spec.description = ""
spec.homepage = "https://github.com/bogdanvlviv/general_time"
spec.license = "MIT"

# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
else
raise "RubyGems 2.0 or newer is required to protect against " \
"public gem pushes."
end

spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.files = `git ls-files -z`.split("\x0")
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.13"
Expand Down
2 changes: 1 addition & 1 deletion lib/general_time.rb
@@ -1,5 +1,5 @@
require "general_time/version"
require "general_time/time"

module GeneralTime
# Your code goes here...
end
1 change: 1 addition & 0 deletions lib/general_time/core_ext/date.rb
@@ -0,0 +1 @@
require "general_time/core_ext/date/calculations"
6 changes: 6 additions & 0 deletions lib/general_time/core_ext/date/calculations.rb
@@ -0,0 +1,6 @@
require "date"
require "general_time/core_ext/date_and_time/calculations"

class Date
include DateAndTime::Calculations
end
35 changes: 35 additions & 0 deletions lib/general_time/core_ext/date_and_time/calculations.rb
@@ -0,0 +1,35 @@
module DateAndTime
module Calculations
AMOUNT_DAYS_PER_YEAR = 365
AMOUNT_WEEKS_PER_YEAR = 52
AMOUNT_MONTHS_PER_YEAR = 12

# Returns the number of days to given date, default to +Date.current+.
def days_between(date = ::Date.current)
date1, date2 = self.to_date, date.to_date

((date2.yday - date1.yday) + AMOUNT_DAYS_PER_YEAR * (date2.year - date1.year)).abs
end

# Returns the number of weeks to given date, default to +Date.current+.
def weeks_between(date = ::Date.current)
date1, date2 = self.to_date, date.to_date

((date2.cweek - date1.cweek) + AMOUNT_WEEKS_PER_YEAR * (date2.year - date1.year)).abs
end

# Returns the number of months to given date, default to +Date.current+.
def months_between(date = ::Date.current)
date1, date2 = self.to_date, date.to_date

((date2.month - date1.month) + AMOUNT_MONTHS_PER_YEAR * (date2.year - date1.year)).abs
end

# Returns the number of years to given date, default to +Date.current+.
def years_between(date = ::Date.current)
date1, date2 = self.to_date, date.to_date

(date2.year - date1.year).abs
end
end
end
1 change: 1 addition & 0 deletions lib/general_time/core_ext/time.rb
@@ -0,0 +1 @@
require "general_time/core_ext/time/calculations"
5 changes: 5 additions & 0 deletions lib/general_time/core_ext/time/calculations.rb
@@ -0,0 +1,5 @@
require "general_time/core_ext/date_and_time/calculations"

class Time
include DateAndTime::Calculations
end
5 changes: 5 additions & 0 deletions lib/general_time/time.rb
@@ -0,0 +1,5 @@
require "date"
require "time"

require "general_time/core_ext/date"
require "general_time/core_ext/time"

0 comments on commit ff7c462

Please sign in to comment.