Create a calendar and add it rules for holidays.
gem install feriados
A rule is an object that responds to holiday?(date) and name. There are four
kinds of rules included in Feriados:
DayOfMonth is a holiday that occurs the same day and month every year,
like New Year.
require 'feriados'
calendar = Feriados::Calendar.new
calendar.add Feriados::Rules::DayOfMonth.new(1, 1, 'New Year')
date = Date.new(2020, 1, 1)
calendar.holiday?(date) #=> true
calendar.holiday_name(date) #=> New YearFixWeekDay is a holiday that happens each year the same week day the nth week,
like the fourth Thursday of November.
calendar.add Feriados::Rules::FixWeekDay.new(4, 4, 11, 'Thanksgiving')
date = Date.new(2020, 11, 26)
calendar.holiday?(date) #=> true
calendar.holiday_name(date) #=> ThanksgivingFunction is a holiday that is calculated for every year, like Easter
calendar.add Feriados::Rules::Easter
date = Date.new(2020, 4, 12)
calendar.holiday?(date) #=> true
calendar.holiday_name(date) #=> nil
# You might add a name to easter
Feriados::Rules::Easter.name = 'Easter'
calendar.holiday_name(date) #=> EasterThe other included Functions in Feriados are: HolyThursday,
HolyFriday, CarnivalMonday, CarnivalTuesday.
FixDate is a holiday that happens on a date on an specific year,
like 2020 RubyConf.
calendar.add Feriados::Rules::FixDate.new(2020, 11, 17, 'RubyConf')
date = Date.new(2020, 11, 17)
calendar.holiday?(date) #=> true
calendar.holiday_name(date) #=> RubyConfIt is possible to add rules using a Hash.
rules = [{
month: 1,
day: 1,
name: 'New Year'
}, {
easter: 'Easter'
}, {
holy_thursday: 'Holy Thursday'
}, {
holy_friday: 'Holy Friday'
}, {
carnival_monday: 'Carnival Monday'
}, {
carnival_tuesday: 'Carnival Tuesday'
}, {
month: 11,
day: 4,
week: 4,
name: 'Thanksgiving'
}, {
year: 2020,
month: 11,
day: 17,
name: 'RubyConf'
}]
calendar.load rulesOr you could use a YAML file like test/argentina.yml.
rules = YAML.load_file(file_with_rules)
calendar.load(rules)It is possible to use a refinement for Date class:
require 'feriados'
using Feriados
calendar = Feriados::Calendar.new
calendar.add Feriados::Rules::Easter
Feriados::Rules::Easter.name = 'Easter'
# Set a calendar to Date class
Date.calendar = calendar
Date.new(2020, 4, 12).holiday? #=> true
Date.new(2020, 4, 12).holiday_name #=> Eastercalendar = Feriados::Calendar.new
calendar.load(rules)
# Get all holidays in 2025
holidays = calendar.holidays_in_year(2025)
holidays.each do |holiday|
puts "#{holiday[:date]} - #{holiday[:name]}"
endstart_date = Date.new(2025, 3, 1)
end_date = Date.new(2025, 5, 31)
holidays = calendar.holidays_between(start_date, end_date)
# Returns array of {date: Date, name: String}# Next holiday from today
next_holiday = calendar.next_holiday
# => {date: Date.new(2025, 8, 18), name: "Independence Day"}
# Next holiday from a specific date
next_holiday = calendar.next_holiday(Date.new(2025, 6, 1))
# => {date: Date.new(2025, 6, 20), name: "Flag Day"}