From bdeee809aa12dacb319864495b7c6023994149bd Mon Sep 17 00:00:00 2001 From: Bodaniel Jeanes Date: Tue, 12 Jan 2010 14:53:11 +1000 Subject: [PATCH 1/2] Support calculating mday from negative weeks other than -1 --- lib/holidays.rb | 4 ++-- test/test_date.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/holidays.rb b/lib/holidays.rb index 5eef2f4..717ad1e 100644 --- a/lib/holidays.rb +++ b/lib/holidays.rb @@ -48,7 +48,7 @@ module Holidays @@holidays_by_month = {} @@proc_cache = {} - WEEKS = {:first => 1, :second => 2, :third => 3, :fourth => 4, :fifth => 5, :last => -1} + WEEKS = {:first => 1, :second => 2, :third => 3, :fourth => 4, :fifth => 5, :last => -1, :second_last => -2, :third_last => -3} MONTH_LENGTHS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] DAY_SYMBOLS = Date::DAYNAMES.collect { |n| n.downcase.intern } @@ -399,6 +399,6 @@ class Date days = 29 if month == 2 and Date.leap?(year) - return days - ((Date.civil(year, month, days).wday - wday + 7) % 7) + return days - ((Date.civil(year, month, days).wday - wday + 7) % 7) - (7 * (week.abs - 1)) end end \ No newline at end of file diff --git a/test/test_date.rb b/test/test_date.rb index 920fdf2..ef6b372 100644 --- a/test/test_date.rb +++ b/test/test_date.rb @@ -39,6 +39,18 @@ class DateTests < Test::Unit::TestCase assert_equal 2, Date.calculate_mday(2007, 3, :first, :friday) assert_equal 30, Date.calculate_mday(2012, 1, :last, 1) assert_equal 29, Date.calculate_mday(2016, 2, :last, 1) + + # From end of month + assert_equal 26, Date.calculate_mday(2009, 8, -1, :wednesday) + assert_equal 19, Date.calculate_mday(2009, 8, -2, :wednesday) + assert_equal 12, Date.calculate_mday(2009, 8, -3, :wednesday) + + assert_equal 13, Date.calculate_mday(2008, 8, -3, :wednesday) + assert_equal 12, Date.calculate_mday(2009, 8, -3, :wednesday) + assert_equal 11, Date.calculate_mday(2010, 8, -3, :wednesday) + assert_equal 17, Date.calculate_mday(2011, 8, -3, :wednesday) + assert_equal 15, Date.calculate_mday(2012, 8, -3, :wednesday) + assert_equal 14, Date.calculate_mday(2013, 8, -3, :wednesday) end def test_mday_allows_integers_or_symbols From 3018cb4d537264567000619b16a35c483c4e93bc Mon Sep 17 00:00:00 2001 From: Bodaniel Jeanes Date: Tue, 12 Jan 2010 15:01:39 +1000 Subject: [PATCH 2/2] Added Ekka and a region for Brisbane to hold it to the Australian holidays --- data/au.yaml | 5 +++++ lib/holidays/au.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/data/au.yaml b/data/au.yaml index 3af3070..da7ec70 100644 --- a/data/au.yaml +++ b/data/au.yaml @@ -66,6 +66,11 @@ months: regions: [au_qld] mday: 6 type: informal + 8: + - name: Ekka + regions: [au_qld_brisbane] + week: -3 + wday: 3 10: - name: Labour Day regions: [au_act, au_nsw, au_sa] diff --git a/lib/holidays/au.rb b/lib/holidays/au.rb index 36ef0c0..8c9667c 100644 --- a/lib/holidays/au.rb +++ b/lib/holidays/au.rb @@ -11,7 +11,7 @@ module Holidays # # More definitions are available at http://code.dunae.ca/holidays. module AU # :nodoc: - DEFINED_REGIONS = [:au_qld, :au_nt, :au, :au_tas, :au_wa, :au_act, :au_nsw, :au_sa, :au_vic] + DEFINED_REGIONS = [:au_qld, :au_nt, :au, :au_tas, :au_wa, :au_act, :au_nsw, :au_sa, :au_vic, :au_qld_brisbane] HOLIDAYS_BY_MONTH = { 5 => [{:wday => 1, :week => 1, :name => "Labour Day", :regions => [:au_qld]}, @@ -27,6 +27,7 @@ module Holidays {:mday => 26, :name => "Australia Day", :regions => [:au]}], 12 => [{:mday => 25, :name => "Christmas Day", :regions => [:au]}, {:mday => 26, :name => "Boxing Day", :regions => [:au]}], + 8 => [{:wday => 3, :week => -3, :name => "Ekka", :regions => [:au_qld_brisbane]}], 3 => [{:wday => 1, :week => 1, :name => "Labour Day", :regions => [:au_wa]}, {:wday => 1, :week => 2, :name => "Eight Hours Day", :regions => [:au_tas]}, {:wday => 1, :week => 2, :name => "Labour Day", :regions => [:au_vic]}],