class DateOperations

Different date operations, primarily around business days, based on the Holidays Gem.

Attributes

country[RW]

Switches to the calendar of the respective country:

  • :de = Germany

  • :us = USA

Public Class Methods

business_days_between(start_date, end_date) click to toggle source

Returns an array with dates for the “business days” = “week days” (= Monday to Friday) without holidays between a given start and end date.

# File lib/dateoperations.rb, line 35
def self.business_days_between(start_date, end_date)
  week_days = week_days_between(start_date, end_date)
  week_days.reject { |b| holiday?(b) }
end
holiday?(date) click to toggle source

Checks whether a given date is a holiday based on the calendar for the respective country.

# File lib/dateoperations.rb, line 55
def self.holiday?(date)
  return false if Holidays.on(date, DateOperations.country, :informal).empty?
  true
end
number_of_business_days_between(start_date, end_date) click to toggle source

Returns the number of “business days”

“week days” (= Monday to Friday)

without holidays between a given start and end date.

# File lib/dateoperations.rb, line 44
def self.number_of_business_days_between(start_date, end_date)
  business_days = business_days_between(start_date, end_date)
  business_days.length
end
number_of_week_days_between(start_date, end_date) click to toggle source

returns the number of “week days” (= Monday to Friday) between a given start and end date.

# File lib/dateoperations.rb, line 26
def self.number_of_week_days_between(start_date, end_date)
  week_days = week_days_between(start_date, end_date)
  week_days.length
end
week_days_between(start_date, end_date) click to toggle source

Returns an array with dates for the “week days” (= Monday to Friday) between a given start and end date.

# File lib/dateoperations.rb, line 20
def self.week_days_between(start_date, end_date)
  week_day_nbrs = (1..5)
  (start_date..end_date).select { |w| week_day_nbrs.include?(w.wday) }
end
weekend?(date) click to toggle source

Checks whether a given date is a weekend (Saturday, Sunday).

# File lib/dateoperations.rb, line 50
def self.weekend?(date)
  date.saturday? || date.sunday?
end