mirror of
https://github.com/github/rails.git
synced 2026-02-03 02:35:08 -05:00
Test multiple timezones' DST. Closes #5617.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5420 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -10,24 +10,42 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def test_seconds_since_midnight_at_daylight_savings_time_start
|
||||
with_us_timezone do
|
||||
with_timezone 'US/Eastern' do
|
||||
# dt: US: 2005 April 3rd 2:00am ST => April 3rd 3:00am DT
|
||||
assert_equal 3600+59*60+59, Time.local(2005,4,3,1,59,59).seconds_since_midnight, 'just before DST start'
|
||||
assert_equal 3600+59*60+59+2,Time.local(2005,4,3,3, 0, 1).seconds_since_midnight, 'just after DST start'
|
||||
assert_equal 2*3600-1, Time.local(2005,4,3,1,59,59).seconds_since_midnight, 'just before DST start'
|
||||
assert_equal 2*3600+1, Time.local(2005,4,3,3, 0, 1).seconds_since_midnight, 'just after DST start'
|
||||
end
|
||||
|
||||
with_timezone 'NZ' do
|
||||
# dt: New Zealand: 2006 October 1st 2:00am ST => October 1st 3:00am DT
|
||||
assert_equal 2*3600-1, Time.local(2006,10,1,1,59,59).seconds_since_midnight, 'just before DST start'
|
||||
assert_equal 2*3600+1, Time.local(2006,10,1,3, 0, 1).seconds_since_midnight, 'just after DST start'
|
||||
end
|
||||
end
|
||||
|
||||
def test_seconds_since_midnight_at_daylight_savings_time_end
|
||||
with_us_timezone do
|
||||
with_timezone 'US/Eastern' do
|
||||
# st: US: 2005 October 30th 2:00am DT => October 30th 1:00am ST
|
||||
# avoid setting a time between 1:00 and 2:00 since that requires specifying whether DST is active
|
||||
assert_equal 3599, Time.local(2005,10,30,0,59,59).seconds_since_midnight, 'just before DST end'
|
||||
assert_equal 1*3600-1, Time.local(2005,10,30,0,59,59).seconds_since_midnight, 'just before DST end'
|
||||
assert_equal 3*3600+1, Time.local(2005,10,30,2, 0, 1).seconds_since_midnight, 'just after DST end'
|
||||
|
||||
# now set a time between 1:00 and 2:00 by specifying whether DST is active
|
||||
# uses: Time.local( sec, min, hour, day, month, year, wday, yday, isdst, tz )
|
||||
assert_equal 1*3600+30*60, Time.local(0,30,1,30,10,2005,0,0,true,'EST5EDT').seconds_since_midnight, 'before DST end'
|
||||
assert_equal 2*3600+30*60, Time.local(0,30,1,30,10,2005,0,0,false,'EST5EDT').seconds_since_midnight, 'after DST end'
|
||||
assert_equal 1*3600+30*60, Time.local(0,30,1,30,10,2005,0,0,true,ENV['TZ']).seconds_since_midnight, 'before DST end'
|
||||
assert_equal 2*3600+30*60, Time.local(0,30,1,30,10,2005,0,0,false,ENV['TZ']).seconds_since_midnight, 'after DST end'
|
||||
end
|
||||
|
||||
with_timezone 'NZ' do
|
||||
# st: New Zealand: 2006 March 19th 3:00am DT => March 19th 2:00am ST
|
||||
# avoid setting a time between 2:00 and 3:00 since that requires specifying whether DST is active
|
||||
assert_equal 2*3600-1, Time.local(2006,3,19,1,59,59).seconds_since_midnight, 'just before DST end'
|
||||
assert_equal 4*3600+1, Time.local(2006,3,19,3, 0, 1).seconds_since_midnight, 'just after DST end'
|
||||
|
||||
# now set a time between 2:00 and 3:00 by specifying whether DST is active
|
||||
# uses: Time.local( sec, min, hour, day, month, year, wday, yday, isdst, tz )
|
||||
assert_equal 2*3600+30*60, Time.local(0,30,2,19,3,2006,0,0,true, ENV['TZ']).seconds_since_midnight, 'before DST end'
|
||||
assert_equal 3*3600+30*60, Time.local(0,30,2,19,3,2006,0,0,false,ENV['TZ']).seconds_since_midnight, 'after DST end'
|
||||
end
|
||||
end
|
||||
|
||||
@@ -44,6 +62,14 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
|
||||
|
||||
def test_beginning_of_day
|
||||
assert_equal Time.local(2005,2,4,0,0,0), Time.local(2005,2,4,10,10,10).beginning_of_day
|
||||
with_timezone 'US/Eastern' do
|
||||
assert_equal Time.local(2006,4,2,0,0,0), Time.local(2006,4,2,10,10,10).beginning_of_day, 'start DST'
|
||||
assert_equal Time.local(2006,10,29,0,0,0), Time.local(2006,10,29,10,10,10).beginning_of_day, 'ends DST'
|
||||
end
|
||||
with_timezone 'NZ' do
|
||||
assert_equal Time.local(2006,3,19,0,0,0), Time.local(2006,3,19,10,10,10).beginning_of_day, 'ends DST'
|
||||
assert_equal Time.local(2006,10,1,0,0,0), Time.local(2006,10,1,10,10,10).beginning_of_day, 'start DST'
|
||||
end
|
||||
end
|
||||
|
||||
def test_beginning_of_month
|
||||
@@ -111,19 +137,29 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def test_daylight_savings_time_crossings_backward_start
|
||||
with_us_timezone do
|
||||
with_timezone 'US/Eastern' do
|
||||
# dt: US: 2005 April 3rd 4:18am
|
||||
assert_equal Time.local(2005,4,2,4,18,0), Time.local(2005,4,3,4,18,0).ago(86400), 'dt-1.day=>st'
|
||||
assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(86400), 'st-1.day=>st'
|
||||
end
|
||||
with_timezone 'NZ' do
|
||||
# dt: New Zealand: 2006 October 1st 4:18am
|
||||
assert_equal Time.local(2006,9,30,4,18,0), Time.local(2006,10,1,4,18,0).ago(86400), 'dt-1.day=>st'
|
||||
assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(86400), 'st-1.day=>st'
|
||||
end
|
||||
end
|
||||
|
||||
def test_daylight_savings_time_crossings_backward_end
|
||||
with_us_timezone do
|
||||
with_timezone 'US/Eastern' do
|
||||
# st: US: 2005 October 30th 4:03am
|
||||
assert_equal Time.local(2005,10,29,4,3), Time.local(2005,10,30,4,3,0).ago(86400), 'st-1.day=>dt'
|
||||
assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(86400), 'dt-1.day=>dt'
|
||||
end
|
||||
with_timezone 'NZ' do
|
||||
# st: New Zealand: 2006 March 19th 4:03am
|
||||
assert_equal Time.local(2006,3,18,4,3), Time.local(2006,3,19,4,3,0).ago(86400), 'st-1.day=>dt'
|
||||
assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(86400), 'dt-1.day=>dt'
|
||||
end
|
||||
end
|
||||
|
||||
def test_since
|
||||
@@ -134,18 +170,28 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def test_daylight_savings_time_crossings_forward_start
|
||||
with_us_timezone do
|
||||
with_timezone 'US/Eastern' do
|
||||
# st: US: 2005 April 2nd 7:27pm
|
||||
assert_equal Time.local(2005,4,3,19,27,0), Time.local(2005,4,2,19,27,0).since(86400), 'st+1.day=>dt'
|
||||
assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400), 'dt+1.day=>dt'
|
||||
end
|
||||
with_timezone 'NZ' do
|
||||
# st: New Zealand: 2006 September 30th 7:27pm
|
||||
assert_equal Time.local(2006,10,1,19,27,0), Time.local(2006,9,30,19,27,0).since(86400), 'st+1.day=>dt'
|
||||
assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(86400), 'dt+1.day=>dt'
|
||||
end
|
||||
end
|
||||
|
||||
def test_daylight_savings_time_crossings_forward_end
|
||||
with_us_timezone do
|
||||
with_timezone 'US/Eastern' do
|
||||
# dt: US: 2005 October 30th 12:45am
|
||||
assert_equal Time.local(2005,10,31,1,45,0), Time.local(2005,10,30,1,45,0).since(86400), 'dt+1.day=>st'
|
||||
assert_equal Time.local(2005,11, 1,1,45,0), Time.local(2005,10,31,1,45,0).since(86400), 'st+1.day=>st'
|
||||
assert_equal Time.local(2005,10,31,0,45,0), Time.local(2005,10,30,0,45,0).since(86400), 'dt+1.day=>st'
|
||||
assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(86400), 'st+1.day=>st'
|
||||
end
|
||||
with_timezone 'NZ' do
|
||||
# dt: New Zealand: 2006 March 19th 1:45am
|
||||
assert_equal Time.local(2006,3,20,1,45,0), Time.local(2006,3,19,1,45,0).since(86400), 'dt+1.day=>st'
|
||||
assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(86400), 'st+1.day=>st'
|
||||
end
|
||||
end
|
||||
|
||||
@@ -205,11 +251,21 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def test_next_week_near_daylight_start
|
||||
assert_equal Time.local(2006,4,3), Time.local(2006,4,2,23,1,0).next_week, 'just crossed standard => daylight'
|
||||
with_timezone 'US/Eastern' do
|
||||
assert_equal Time.local(2006,4,3), Time.local(2006,4,2,23,1,0).next_week, 'just crossed standard => daylight'
|
||||
end
|
||||
with_timezone 'NZ' do
|
||||
assert_equal Time.local(2006,10,2), Time.local(2006,10,1,23,1,0).next_week, 'just crossed standard => daylight'
|
||||
end
|
||||
end
|
||||
|
||||
def test_next_week_near_daylight_end
|
||||
assert_equal Time.local(2006,10,30), Time.local(2006,10,29,23,1,0).next_week, 'just crossed daylight => standard'
|
||||
with_timezone 'US/Eastern' do
|
||||
assert_equal Time.local(2006,10,30), Time.local(2006,10,29,23,1,0).next_week, 'just crossed daylight => standard'
|
||||
end
|
||||
with_timezone 'NZ' do
|
||||
assert_equal Time.local(2006,3,20), Time.local(2006,3,19,23,1,0).next_week, 'just crossed daylight => standard'
|
||||
end
|
||||
end
|
||||
|
||||
def test_to_s
|
||||
@@ -270,8 +326,8 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
protected
|
||||
def with_us_timezone
|
||||
old_tz, ENV['TZ'] = ENV['TZ'], 'US/Pacific'
|
||||
def with_timezone new_tz='US/Eastern'
|
||||
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
|
||||
yield
|
||||
ensure
|
||||
ENV['TZ'] = old_tz
|
||||
|
||||
Reference in New Issue
Block a user