Skip to content

Commit

Permalink
Extract additional Utils methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rossta committed Feb 21, 2016
1 parent 1a7a0f9 commit 5da4dd7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
9 changes: 1 addition & 8 deletions lib/montrose/rule/day_of_year.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,9 @@ def include?(time)
private

def included_from_end_of_month?(time)
year_days = days_in_year(time.year) # given by activesupport
year_days = ::Montrose::Utils.days_in_year(time.year) # given by activesupport
@days.any? { |d| year_days + d + 1 == time.yday }
end

# Returns the number of days in the given year.
# If no year is specified, it will use the current year.
# https://github.com/rails/rails/pull/22244
def days_in_year(year)
::Montrose::Utils.days_in_month(2, year) + 337
end
end
end
end
10 changes: 1 addition & 9 deletions lib/montrose/rule/nth_day_of_month.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@ def first_wday
end

def total_days
days_in_month(@time)
end

private

# Get the days in the month for +time
def days_in_month(time)
date = Date.new(time.year, time.month, 1)
((date >> 1) - date).to_i
::Montrose::Utils.days_in_month(@time.month, @time.year)
end
end
end
Expand Down
12 changes: 10 additions & 2 deletions lib/montrose/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,16 @@ def day_number!(name)
"Did not recognize day #{name}, must be one of #{DAYS.inspect}"
end

def days_in_month(*args)
::Time.days_in_month(*args)
def days_in_month(month, year = current_time.year)
date = ::Date.new(year, month, 1)
((date >> 1) - date).to_i
end

# Returns the number of days in the given year.
# If no year is specified, it will use the current year.
# https://github.com/rails/rails/pull/22244
def days_in_year(year)
::Montrose::Utils.days_in_month(2, year) + 337
end
end
end

0 comments on commit 5da4dd7

Please sign in to comment.