Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add methods [days, weeks, months, years]_between
Inspired by rails/rails#27966
- Loading branch information
1 parent
c490b45
commit ff7c462
Showing
18 changed files
with
273 additions
and
117 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in general_time.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,3 @@ IFS=$'\n\t' | |
set -vx | ||
|
||
bundle install | ||
|
||
# Do any other automated setup that you need to do here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
require "general_time/version" | ||
require "general_time/time" | ||
|
||
module GeneralTime | ||
# Your code goes here... | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "general_time/core_ext/date/calculations" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "date" | ||
require "general_time/core_ext/date_and_time/calculations" | ||
|
||
class Date | ||
include DateAndTime::Calculations | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "general_time/core_ext/time/calculations" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require "general_time/core_ext/date_and_time/calculations" | ||
|
||
class Time | ||
include DateAndTime::Calculations | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require "date" | ||
require "time" | ||
|
||
require "general_time/core_ext/date" | ||
require "general_time/core_ext/time" |
Oops, something went wrong.