Remove timezone when comparing DateTimes

Closes GH-1
This commit is contained in:
Alex Dunae 2010-11-12 14:26:33 -08:00
parent 962989c326
commit f7749920f8
3 changed files with 41 additions and 4 deletions

View file

@ -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 = []

View file

@ -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

View file

@ -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)