From f7749920f8d0f5280069fb5cd6cd4425fd037a05 Mon Sep 17 00:00:00 2001 From: Alex Dunae Date: Fri, 12 Nov 2010 14:26:33 -0800 Subject: [PATCH] Remove timezone when comparing DateTimes Closes GH-1 --- lib/holidays.rb | 5 +++++ test/test_date.rb | 23 ++++++++++++++++++++++- test/test_helper.rb | 17 ++++++++++++++--- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/holidays.rb b/lib/holidays.rb index 8dfbd04..c02612e 100644 --- a/lib/holidays.rb +++ b/lib/holidays.rb @@ -83,8 +83,13 @@ module Holidays # => [{:name => 'Canada Day', :regions => [:ca]...} # {:name => 'Independence Day'', :regions => [:us], ...}] def self.between(start_date, end_date, *options) + # remove the timezone + start_date = start_date.new_offset(0) if start_date.respond_to?(:new_offset) + end_date = end_date.new_offset(0) if end_date.respond_to?(:new_offset) + start_date = start_date.to_date if start_date.respond_to?(:to_date) end_date = end_date.to_date if end_date.respond_to?(:to_date) + regions, observed, informal = parse_options(options) holidays = [] diff --git a/test/test_date.rb b/test/test_date.rb index 523f83b..c4f9e77 100644 --- a/test/test_date.rb +++ b/test/test_date.rb @@ -10,6 +10,12 @@ class DateTests < Test::Unit::TestCase assert @date.respond_to?('holiday?') end + def test_extending_datetime_class + dt = DateTime.civil(2008,1,1) + assert dt.respond_to?('holidays') + assert dt.respond_to?('holiday?') + end + def test_calculating_mdays # US Memorial day assert_equal 29, Date.calculate_mday(2006, 5, :last, 1) @@ -95,7 +101,22 @@ class DateTests < Test::Unit::TestCase end end - def test_holiday? + def test_date_holiday? assert Date.civil(2008,1,1).holiday?('ca') + assert Date.today.holiday?('test') + end + + def test_datetime_holiday? + assert DateTime.now.to_date.holiday?('test') + assert DateTime.now.holiday?('test') + end + + # ensure that dates are compared in the same timezone + def test_datetime_offset_holiday? + dt = DateTime.now.new_offset(Rational(23,24)) + assert dt.holiday?('test'), dt.inspect + + dt = DateTime.now.new_offset(Rational(-23,24)) + assert dt.holiday?('test'), dt.inspect end end diff --git a/test/test_helper.rb b/test/test_helper.rb index f859150..203d376 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,6 +1,4 @@ -$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__), '../')) -$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__), '../lib/')) -$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__), '../../lib/')) +$:.unshift(File.expand_path(File.dirname(__FILE__) + '../../lib/')) $KCODE = 'u' @@ -9,3 +7,16 @@ require 'test/unit' require 'date' require 'holidays' require 'holidays/ca' + +module Holidays + # Test region used for generating a holiday on Date.today + module Test # :nodoc: + DEFINED_REGIONS = [:test] + + HOLIDAYS_BY_MONTH = { + Date.today.mon => [{:mday => Date.today.mday, :name => "Test Holiday", :regions => [:test]}] + } + end +end + +Holidays.merge_defs(Holidays::Test::DEFINED_REGIONS, Holidays::Test::HOLIDAYS_BY_MONTH)