Skip to content

Commit

Permalink
Merge 927e5bc into b54eab6
Browse files Browse the repository at this point in the history
  • Loading branch information
ppeble committed Dec 8, 2015
2 parents b54eab6 + 927e5bc commit 6fb0ffc
Show file tree
Hide file tree
Showing 123 changed files with 2,413 additions and 1,196 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ notifications:
disabled: true
sudo: false
rvm:
- 1.9.2
- 1.9.3
- 2.0.0
- 2.1.0
- 2.2.0
Expand Down
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ To install the gem from RubyGems:

gem install holidays

The Holidays gem is tested on Ruby 1.9.2, 1.9.3, 2.0.0, 2.1.0, 2.2.0 and JRuby.
The Holidays gem is tested on Ruby 2.0.0, 2.1.0, 2.2.0 and JRuby.

## Time zones

Expand Down Expand Up @@ -76,14 +76,21 @@ Multiple files can also be passed:

### Extending Ruby's Date class

Check which holidays occur in Iceland on January 1, 2008.
To extend the 'Date' class:

require 'holidays/core_extensions/date'
class Date
include Holidays::CoreExtensions::Date
end

Now you can check which holidays occur in Iceland on January 1, 2008:

d = Date.civil(2008,7,1)

d.holidays(:is)
=> [{:name => 'Nýársdagur'}...]

Lookup Canada Day in different regions.
Or lookup Canada Day in different regions:

d = Date.civil(2008,7,1)

Expand All @@ -96,6 +103,11 @@ Lookup Canada Day in different regions.
d.holiday?(:fr) # France
=> false

Or you can calculate the day of the month:

Date.calculate_mday(2015, 4, :first, 2)
=> 7

### Caching Holiday Lookups

If you are checking holidays regularly you can cache your results for improved performance. Run this before looking up a holiday (eg. in an initializer):
Expand All @@ -108,14 +120,16 @@ See the [original pull request](https://github.com/alexdunae/holidays/pull/36) f

### How to contribute

To make changes to any of the definitions, edit the YAML files only.
To make changes to any of the definitions, edit the YAML files under the 'data' directory only. These YAML files are read by the 'generate' rake target and the final definition files are created automatically. Don't directly edit any other files.

Tests are also added at the end of the YAML files. Please add tests, it makes the pull requests go around.
Tests are also allowed at the end of the YAML files. Please add tests. PRs will not be accepted unless there are tests proving that the new definitions work.

After you're satisfied with the YAML file, edit the index.yaml file, run `rake generate`, which will generate the Ruby files that make up the actual code as well as the tests. Then run `rake test`.
After you're satisfied with the YAML file if you have added a brand-new region you must edit the index.yaml file to include the new region. Then run `rake generate`, which will generate the Ruby files that make up the actual code as well as the tests. Then run `rake test`. If that passes then add all of the changed files to a commit and open a pull request.

It is also very appreciated if documentation is attached to the pull request. A simple Wikipedia or government link referencing the change would be perfect.

If you wish to test out your changes via irb you can run `bin/console`. This will load all required lib files and open an irb session.

### Credits and code

* Started by [Alex Dunae](http://dunae.ca) (e-mail 'code' at the same domain), 2007-12
Expand Down
32 changes: 25 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,60 @@ task :default => :test

namespace :generate do
DATA_PATH = 'data'
TEST_DEFS_PATH = 'test/defs'

desc 'Generate the holiday definition files'
task :definitions do
# load the index
def_index = YAML.load_file("#{DATA_PATH}/index.yaml")

# create a dir for the generated tests
FileUtils.mkdir_p('test/defs')
FileUtils.mkdir_p(TEST_DEFS_PATH)

all_regions = []

def_index['defs'].each do |region, files|
puts "Building #{region} definition module:"
files = files.collect { |f| "#{DATA_PATH}/#{f}" }.uniq

module_src, test_src = Holidays.parse_definition_files_and_return_source(region, files)
File.open("lib/holidays/#{region.downcase.to_s}.rb","w") do |file|
module_src, test_src, regions = Holidays.parse_definition_files_and_return_source(region, files)
File.open("lib/#{Holidays::DEFINITIONS_PATH}/#{region.downcase.to_s}.rb","w") do |file|
file.puts module_src
end
unless test_src.empty?
File.open("test/defs/test_defs_#{region.downcase.to_s}.rb","w") do |file|
File.open("#{TEST_DEFS_PATH}/test_defs_#{region.downcase.to_s}.rb","w") do |file|
file.puts test_src
end
end

all_regions << regions

puts "Done.\n\n"
end

puts "Building regions master file for later validation:"

File.open("lib/#{Holidays::DEFINITIONS_PATH}/REGIONS.rb","w") do |file|
file.puts <<-EOR
# encoding: utf-8
module Holidays
REGIONS = [:#{all_regions.join(', :')}]
end
EOR
end

puts "Done.\n\n"
end

desc 'Build the definition manifest'
task :manifest do
File.open("lib/holidays/MANIFEST","w") do |file|
File.open("lib/#{Holidays::DEFINITIONS_PATH}/MANIFEST","w") do |file|
file.puts <<-EOH
==== Regional definitions
The following definition files are included in this installation:
EOH
FileList.new('lib/holidays/*.rb').exclude(/version/).each do |str|
FileList.new("lib/#{Holidays::DEFINITIONS_PATH}/*.rb").exclude(/version/).each do |str|
file.puts('* ' + str.gsub(/^lib\/|\.rb$/, ''))
end
end
Expand All @@ -59,4 +78,3 @@ The following definition files are included in this installation:
end

task :generate => ['generate:definitions', 'generate:manifest']

7 changes: 7 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "holidays"

require "irb"
IRB.start
5 changes: 5 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

bundle install
2 changes: 1 addition & 1 deletion data/at.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ tests: |
assert_equal name, (Holidays.on(date, :at, :informal)[0] || {})[:name]
end
assert !Date.civil(2010,5,8).holiday?(:at), '2010-05-08 is not a holiday in Austria'
assert_equal [], Holidays.on(Date.civil(2010,5,8), :at), '2010-05-08 is not a holiday in Austria'
92 changes: 46 additions & 46 deletions data/au.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ methods:
# http://www.justice.qld.gov.au/fair-and-safe-work/industrial-relations/public-holidays/dates
# changed from may to october after 2012
def self.qld_labour_day_may(year)
year <= 2012 ? Date.calculate_mday(year, 5, 1, 1) : nil
year <= 2012 ? Holidays.calculate_day_of_month(year, 5, 1, 1) : nil
end
qld_labour_day_october: |
# http://www.justice.qld.gov.au/fair-and-safe-work/industrial-relations/public-holidays/dates
# changed from may to october after 2012
def self.qld_labour_day_october(year)
year <= 2012 ? nil : Date.calculate_mday(year, 10, 1, 1)
year <= 2012 ? nil : Holidays.calculate_day_of_month(year, 10, 1, 1)
end
g20_day_2014_only: |
# http://www.justice.qld.gov.au/fair-and-safe-work/industrial-relations/public-holidays/dates
Expand All @@ -183,7 +183,7 @@ methods:
# http://worksafe.tas.gov.au/__data/assets/pdf_file/0008/287036/Public_Holidays_2014.pdf
# The Thursday before the fourth Saturday in October.
def self.hobart_show_day(year)
fourth_sat_in_oct = Date.civil(year, 10, Date.calculate_mday(year, 10, 4, :saturday))
fourth_sat_in_oct = Date.civil(year, 10, Holidays.calculate_day_of_month(year, 10, 4, :saturday))
fourth_sat_in_oct - 2 # the thursday before
end
march_pub_hol_sa: |
Expand Down Expand Up @@ -217,72 +217,63 @@ tests: |
end
[:au_sa, :au_act, :au_nsw, :au_].each do |r|
assert_equal 'Labour Day', Date.civil(2007,10,1).holidays(r)[0][:name]
assert_equal 'Labour Day', Holidays.on(Date.civil(2007,10,1), r)[0][:name]
end
[:au_sa, :au_act, :au_nsw, :au_vic, :au_tas, :au_qld, :au_nt, :au_].each do |r|
assert_equal 'Queen\'s Birthday', Date.civil(2007,6,11).holidays(r)[0][:name]
assert_equal 'Queen\'s Birthday', Holidays.on(Date.civil(2007,6,11), r)[0][:name]
end
assert_equal 'Labour Day', Date.civil(2007,3,5).holidays(:au_wa)[0][:name]
assert_equal 'Labour Day', Date.civil(2007,3,12).holidays(:au_vic)[0][:name]
assert_equal 'Labour Day', Date.civil(2007,5,7).holidays(:au_qld)[0][:name]
assert_equal 'Labour Day', Holidays.on(Date.civil(2007,3,5), :au_wa)[0][:name]
assert_equal 'Labour Day', Holidays.on(Date.civil(2007,3,12), :au_vic)[0][:name]
assert_equal 'Labour Day', Holidays.on(Date.civil(2007,5,7), :au_qld)[0][:name]
assert_equal 'Easter Sunday', Date.civil(2007,4,8).holidays(:au_nsw)[0][:name]
assert_equal 'Easter Sunday', Holidays.on(Date.civil(2007,4,8), :au_nsw)[0][:name]
assert_equal 'May Day', Date.civil(2007,5,7).holidays(:au_nt)[0][:name]
assert_equal 'Eight Hours Day', Date.civil(2007,3,12).holidays(:au_tas)[0][:name]
assert_equal 'May Day', Holidays.on(Date.civil(2007,5,7), :au_nt)[0][:name]
assert_equal 'Labour Day', Date.civil(2013,10,7).holidays(:au_qld)[0][:name]
assert_equal 'Labour Day', Date.civil(2012,5,7).holidays(:au_qld)[0][:name]
assert_equal 'Eight Hours Day', Holidays.on(Date.civil(2007,3,12), :au_tas)[0][:name]
assert_equal "Queen's Birthday", Date.civil(2012,6,11).holidays(:au_qld)[0][:name]
assert_equal "Queen's Birthday", Date.civil(2012,10,1).holidays(:au_qld)[0][:name]
assert_equal "Queen's Birthday", Date.civil(2013,6,10).holidays(:au_qld)[0][:name]
assert_equal 'Labour Day', Holidays.on(Date.civil(2013,10,7), :au_qld)[0][:name]
assert_equal 'Labour Day', Holidays.on(Date.civil(2012,5,7), :au_qld)[0][:name]
assert_equal "Queen's Birthday", Date.civil(2014, 9, 29).holidays(:au_wa)[0][:name]
assert_equal "Queen's Birthday", Date.civil(2015, 9, 28).holidays(:au_wa)[0][:name]
assert_equal "Queen's Birthday", Date.civil(2016, 9, 26).holidays(:au_wa)[0][:name]
assert_equal "Queen's Birthday", Holidays.on(Date.civil(2012,6,11), :au_qld)[0][:name]
assert_equal "Queen's Birthday", Holidays.on(Date.civil(2012,10,1), :au_qld)[0][:name]
assert_equal "Queen's Birthday", Holidays.on(Date.civil(2013,6,10), :au_qld)[0][:name]
assert_equal "Family & Community Day", Date.civil(2014, 9, 29).holidays(:au_act)[0][:name]
assert_equal "Family & Community Day", Date.civil(2015, 9, 28).holidays(:au_act)[0][:name]
assert_equal "Family & Community Day", Date.civil(2016, 9, 26).holidays(:au_act)[0][:name]
assert_equal "Queen's Birthday", Holidays.on(Date.civil(2014, 9, 29), :au_wa)[0][:name]
assert_equal "Queen's Birthday", Holidays.on(Date.civil(2015, 9, 28), :au_wa)[0][:name]
assert_equal "Queen's Birthday", Holidays.on(Date.civil(2016, 9, 26), :au_wa)[0][:name]
assert_equal 'Australia Day', Date.civil(2014,1,26).holidays(:au_qld)[0][:name]
assert_equal 'Australia Day', Date.civil(2014,1,27).holidays(:au_qld, :observed)[0][:name]
assert_equal "Family & Community Day", Holidays.on(Date.civil(2014, 9, 29), :au_act)[0][:name]
assert_equal "Family & Community Day", Holidays.on(Date.civil(2015, 9, 28), :au_act)[0][:name]
assert_equal "Family & Community Day", Holidays.on(Date.civil(2016, 9, 26), :au_act)[0][:name]
assert_equal 'Cairns Show', Date.civil(2014,7,18).holidays(:au_qld_cairns)[0][:name]
assert_equal 'Cairns Show', Date.civil(2013,7,19).holidays(:au_qld_cairns)[0][:name]
assert_equal 'Australia Day', Holidays.on(Date.civil(2014,1,26), :au_qld)[0][:name]
assert_equal 'Australia Day', Holidays.on(Date.civil(2014,1,27), :au_qld, :observed)[0][:name]
assert_equal 'Royal Hobart Show', Date.civil(2013, 10, 24).holidays(:au_tas_south)[0][:name]
assert_equal 'Royal Hobart Show', Date.civil(2014, 10, 23).holidays(:au_tas_south)[0][:name]
assert_equal 'Royal Hobart Show', Date.civil(2015, 10, 22).holidays(:au_tas_south)[0][:name]
assert_equal 'Royal Hobart Show', Date.civil(2016, 10, 20).holidays(:au_tas_south)[0][:name]
assert_equal 'Cairns Show', Holidays.on(Date.civil(2014,7,18), :au_qld_cairns)[0][:name]
assert_equal 'Cairns Show', Holidays.on(Date.civil(2013,7,19), :au_qld_cairns)[0][:name]
assert_equal 'Recreation Day', Date.civil(2013, 11, 4).holidays(:au_tas_north)[0][:name]
assert_equal 'Recreation Day', Date.civil(2014, 11, 3).holidays(:au_tas_north)[0][:name]
assert_equal 'Recreation Day', Date.civil(2015, 11, 2).holidays(:au_tas_north)[0][:name]
assert_equal 'Recreation Day', Date.civil(2016, 11, 7).holidays(:au_tas_north)[0][:name]
assert_equal 'Royal Hobart Show', Holidays.on(Date.civil(2013, 10, 24), :au_tas_south)[0][:name]
assert_equal 'Royal Hobart Show', Holidays.on(Date.civil(2014, 10, 23), :au_tas_south)[0][:name]
assert_equal 'Royal Hobart Show', Holidays.on(Date.civil(2015, 10, 22), :au_tas_south)[0][:name]
assert_equal 'Royal Hobart Show', Holidays.on(Date.civil(2016, 10, 20), :au_tas_south)[0][:name]
assert_equal 'G20 Day', Date.civil(2014,11,14).holidays(:au_qld_brisbane)[0][:name]
assert_equal [], Date.civil(2014,11,14).holidays(:au_qld)
assert_equal [], Date.civil(2015,11,14).holidays(:au_qld_brisbane)
assert_equal 'Recreation Day', Holidays.on(Date.civil(2013, 11, 4), :au_tas_north)[0][:name]
assert_equal 'Recreation Day', Holidays.on(Date.civil(2014, 11, 3), :au_tas_north)[0][:name]
assert_equal 'Recreation Day', Holidays.on(Date.civil(2015, 11, 2), :au_tas_north)[0][:name]
assert_equal 'Recreation Day', Holidays.on(Date.civil(2016, 11, 7), :au_tas_north)[0][:name]
assert_nil Date.civil(2014,11,4).holidays(:au_vic)[0]
assert_nil Date.civil(2015,11,3).holidays(:au_vic)[0]
assert_equal 'Melbourne Cup Day', Date.civil(2014,11,4).holidays(:au_vic_melbourne)[0][:name]
assert_equal 'Melbourne Cup Day', Date.civil(2015,11,3).holidays(:au_vic_melbourne)[0][:name]
assert_equal 'Melbourne Cup Day', Holidays.on(Date.civil(2014,11,4), :au_vic_melbourne)[0][:name]
assert_equal 'Melbourne Cup Day', Holidays.on(Date.civil(2015,11,3), :au_vic_melbourne)[0][:name]
assert_equal 'Friday before the AFL Grand Final', Date.civil(2015,10,2).holidays(:au_vic)[0][:name]
assert_nil Date.civil(2016,10,2).holidays(:au_vic)[0]
assert_equal 'Royal Hobart Regatta', Date.civil(2012, 2, 13).holidays(:au_tas_south)[0][:name]
assert_equal 'Royal Hobart Regatta', Date.civil(2014, 2, 10).holidays(:au_tas_south)[0][:name]
assert_equal 'Royal Hobart Regatta', Date.civil(2015, 2, 9).holidays(:au_tas_south)[0][:name]
assert_equal 'Royal Hobart Regatta', Date.civil(2016, 2, 8).holidays(:au_tas_south)[0][:name]
assert_equal "May Public Holiday", Date.civil(2005, 5, 16).holidays(:au_sa)[0][:name]
assert_equal [], Date.civil(2014, 5, 19).holidays(:au_sa)
Expand All @@ -307,4 +298,13 @@ tests: |
assert_nil Date.civil(2015, 12, 26).holidays(:au_tas)[0]
assert_equal "Boxing Day", Date.civil(2015, 12, 28).holidays(:au_tas)[0][:name]
assert_nil Date.civil(2015, 12, 26).holidays(:au_nt)[0]
assert_equal "Boxing Day", Date.civil(2015, 12, 28).holidays(:au_nt)[0][:name]
assert_equal "Boxing Day", Date.civil(2015, 12, 28).holidays(:au_nt)[0][:name]
assert_equal 'G20 Day', Holidays.on(Date.civil(2014,11,14), :au_qld_brisbane)[0][:name]
assert_equal [], Holidays.on(Date.civil(2014,11,14), :au_qld)
assert_equal [], Holidays.on(Date.civil(2015,11,14), :au_qld_brisbane)
assert_equal 'Royal Hobart Regatta', Holidays.on(Date.civil(2012, 2, 13), :au_tas_south)[0][:name]
assert_equal 'Royal Hobart Regatta', Holidays.on(Date.civil(2014, 2, 10), :au_tas_south)[0][:name]
assert_equal 'Royal Hobart Regatta', Holidays.on(Date.civil(2015, 2, 9), :au_tas_south)[0][:name]
assert_equal 'Royal Hobart Regatta', Holidays.on(Date.civil(2016, 2, 8), :au_tas_south)[0][:name]
52 changes: 14 additions & 38 deletions data/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,65 +88,41 @@ tests: |
end
[:de_bw, :de_by, :de_st, :de_].each do |r|
assert_equal 'Heilige Drei Könige', Date.civil(2009,1,6).holidays(r)[0][:name]
assert_equal 'Heilige Drei Könige', Holidays.on(Date.civil(2009,1,6), r)[0][:name]
end
[:de_bw, :de_by, :de_he, :de_nw, :de_rp, :de_sl, :de_].each do |r|
assert_equal 'Fronleichnam', Date.civil(2009,6,11).holidays(r)[0][:name]
assert_equal 'Fronleichnam', Holidays.on(Date.civil(2009,6,11), r)[0][:name]
end
[:de_by, :de_sl, :de_].each do |r|
assert_equal 'Mariä Himmelfahrt', Date.civil(2009,8,15).holidays(r)[0][:name]
assert_equal 'Mariä Himmelfahrt', Holidays.on(Date.civil(2009,8,15), r)[0][:name]
end
[:de_bb, :de_mv, :de_sn, :de_st, :de_th, :de_].each do |r|
assert_equal 'Reformationstag', Date.civil(2009,10,31).holidays(r)[0][:name]
assert_equal 'Reformationstag', Holidays.on(Date.civil(2009,10,31), r)[0][:name]
end
[:de_bw, :de_by, :de_nw, :de_rp, :de_sl, :de_].each do |r|
assert_equal 'Allerheiligen', Date.civil(2009,11,1).holidays(r)[0][:name]
end
[:de_be, :de_hb, :de_hh, :de_ni, :de_sh].each do |r|
assert_equal 'Tag der Deutschen Einheit', Date.civil(2009,10,3).holidays(r)[0][:name]
assert_equal 'Allerheiligen', Holidays.on(Date.civil(2009,11,1), r)[0][:name]
end
[:de_by_aux].each do |r|
assert_equal 'Friedensfest', Date.civil(2015,8,8).holidays(r)[0][:name]
end
[:de_be, :de_hb, :de_hh, :de_ni, :de_sh].each do |r|
assert !Date.civil(2009,1,6).holiday?(r), "Heilige Drei Könige is not a holiday in #{r}"
end
[:de_be, :de_hb, :de_hh, :de_ni, :de_sh].each do |r|
assert !Date.civil(2009,6,11).holiday?(r), "Fronleichnam is not a holiday in #{r}"
end
[:de_be, :de_hb, :de_hh, :de_ni, :de_sh].each do |r|
assert !Date.civil(2009,8,15).holiday?(r), "Mariä Himmelfahrt is not a holiday in #{r}"
end
[:de_be, :de_hb, :de_hh, :de_ni, :de_sh].each do |r|
assert !Date.civil(2009,10,31).holiday?(r), 'Reformationstag is not a holiday in #{r}'
end
[:de_be, :de_hb, :de_hh, :de_ni, :de_sh].each do |r|
assert !Date.civil(2009,11,1).holiday?(r), "Allerheiligen is not a holiday in #{r}"
end
[:de,
:de_bw, :de_by, :de_be, :de_bb, :de_hb, :de_hh, :de_he, :de_mv,
:de_ni, :de_nw, :de_rp, :de_sl, :de_sn, :de_st, :de_sh, :de_th].each do |r|
assert !Date.civil(2015,8,8).holiday?(r), "Friedensfest is not a holiday in #{r}"
:de_bw, :de_by, :de_bb, :de_he, :de_mv,
:de_nw, :de_rp, :de_sl, :de_sn, :de_st, :de_th].each do |r|
assert_equal [], Holidays.on(Date.civil(2015,8,8), r), "Friedensfest is not a holiday in #{r}"
end
assert !Date.civil(2010,5,8).holiday?(:de), '2010-05-08 is not a holiday in Germany'
assert_equal [], Holidays.on(Date.civil(2010,5,8), :de), '2010-05-08 is not a holiday in Germany'
assert !Date.civil(2015,8,8).holiday?(:de), '2015-08-08 is not a holiday in Germany'
assert_equal [], Holidays.on(Date.civil(2010,8,8), :de), '2010-08-08 is not a holiday in Germany'
# Repentance Day
assert_equal 'Buß- und Bettag', Date.civil(2004,11,17).holidays(:de_sn)[0][:name]
assert_equal 'Buß- und Bettag', Date.civil(2005,11,16).holidays(:de_sn)[0][:name]
assert_equal 'Buß- und Bettag', Date.civil(2006,11,22).holidays(:de_sn)[0][:name]
assert_equal 'Buß- und Bettag', Date.civil(2009,11,18).holidays(:de_sn)[0][:name]
assert_equal 'Buß- und Bettag', Holidays.on(Date.civil(2004,11,17), :de_sn)[0][:name]
assert_equal 'Buß- und Bettag', Holidays.on(Date.civil(2005,11,16), :de_sn)[0][:name]
assert_equal 'Buß- und Bettag', Holidays.on(Date.civil(2006,11,22), :de_sn)[0][:name]
assert_equal 'Buß- und Bettag', Holidays.on(Date.civil(2009,11,18), :de_sn)[0][:name]

0 comments on commit 6fb0ffc

Please sign in to comment.