2008-05-18 06:22:34 +02:00
|
|
|
require 'abstract_unit'
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
class TimeZoneTest < Test::Unit::TestCase
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_utc_to_local
|
2009-02-28 02:23:00 +01:00
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 19), zone.utc_to_local(Time.utc(2000, 1)) # standard offset -0500
|
|
|
|
assert_equal Time.utc(2000, 6, 30, 20), zone.utc_to_local(Time.utc(2000, 7)) # dst offset -0400
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_local_to_utc
|
2009-02-28 02:23:00 +01:00
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
|
|
|
assert_equal Time.utc(2000, 1, 1, 5), zone.local_to_utc(Time.utc(2000, 1)) # standard offset -0500
|
|
|
|
assert_equal Time.utc(2000, 7, 1, 4), zone.local_to_utc(Time.utc(2000, 7)) # dst offset -0400
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_period_for_local
|
2009-02-28 02:23:00 +01:00
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
|
|
|
assert_instance_of TZInfo::TimezonePeriod, zone.period_for_local(Time.utc(2000))
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
|
|
|
ActiveSupport::TimeZone::MAPPING.keys.each do |name|
|
2008-05-18 06:22:34 +02:00
|
|
|
define_method("test_map_#{name.downcase.gsub(/[^a-z]/, '_')}_to_tzinfo") do
|
2009-02-28 02:23:00 +01:00
|
|
|
zone = ActiveSupport::TimeZone[name]
|
|
|
|
assert zone.tzinfo.respond_to?(:period_for_local)
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_from_integer_to_map
|
2008-09-07 07:54:05 +02:00
|
|
|
assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[-28800] # PST
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_from_duration_to_map
|
2008-09-07 07:54:05 +02:00
|
|
|
assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[-480.minutes] # PST
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2008-09-07 07:54:05 +02:00
|
|
|
ActiveSupport::TimeZone.all.each do |zone|
|
2008-05-18 06:22:34 +02:00
|
|
|
name = zone.name.downcase.gsub(/[^a-z]/, '_')
|
|
|
|
define_method("test_from_#{name}_to_map") do
|
2009-02-28 02:23:00 +01:00
|
|
|
assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[zone.name]
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
define_method("test_utc_offset_for_#{name}") do
|
2009-02-28 02:23:00 +01:00
|
|
|
period = zone.tzinfo.current_period
|
|
|
|
assert_equal period.utc_offset, zone.utc_offset
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def test_now
|
|
|
|
with_env_tz 'US/Eastern' do
|
|
|
|
Time.stubs(:now).returns(Time.local(2000))
|
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
|
|
|
assert_instance_of ActiveSupport::TimeWithZone, zone.now
|
|
|
|
assert_equal Time.utc(2000,1,1,5), zone.now.utc
|
|
|
|
assert_equal Time.utc(2000), zone.now.time
|
|
|
|
assert_equal zone, zone.now.time_zone
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2009-02-04 21:26:08 +01:00
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def test_now_enforces_spring_dst_rules
|
|
|
|
with_env_tz 'US/Eastern' do
|
|
|
|
Time.stubs(:now).returns(Time.local(2006,4,2,2)) # 2AM springs forward to 3AM
|
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
|
|
|
assert_equal Time.utc(2006,4,2,3), zone.now.time
|
|
|
|
assert_equal true, zone.now.dst?
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2009-02-04 21:26:08 +01:00
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def test_now_enforces_fall_dst_rules
|
|
|
|
with_env_tz 'US/Eastern' do
|
|
|
|
Time.stubs(:now).returns(Time.at(1162098000)) # equivalent to 1AM DST
|
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
|
|
|
assert_equal Time.utc(2006,10,29,1), zone.now.time
|
|
|
|
assert_equal true, zone.now.dst?
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2009-02-04 21:26:08 +01:00
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def test_today
|
|
|
|
Time.stubs(:now).returns(Time.utc(2000, 1, 1, 4, 59, 59)) # 1 sec before midnight Jan 1 EST
|
|
|
|
assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
|
|
|
|
Time.stubs(:now).returns(Time.utc(2000, 1, 1, 5)) # midnight Jan 1 EST
|
|
|
|
assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
|
|
|
|
Time.stubs(:now).returns(Time.utc(2000, 1, 2, 4, 59, 59)) # 1 sec before midnight Jan 2 EST
|
|
|
|
assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
|
|
|
|
Time.stubs(:now).returns(Time.utc(2000, 1, 2, 5)) # midnight Jan 2 EST
|
|
|
|
assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_local
|
2009-02-28 02:23:00 +01:00
|
|
|
time = ActiveSupport::TimeZone["Hawaii"].local(2007, 2, 5, 15, 30, 45)
|
|
|
|
assert_equal Time.utc(2007, 2, 5, 15, 30, 45), time.time
|
|
|
|
assert_equal ActiveSupport::TimeZone["Hawaii"], time.time_zone
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_local_with_old_date
|
2009-02-28 02:23:00 +01:00
|
|
|
time = ActiveSupport::TimeZone["Hawaii"].local(1850, 2, 5, 15, 30, 45)
|
|
|
|
assert_equal [45,30,15,5,2,1850], time.to_a[0,6]
|
|
|
|
assert_equal ActiveSupport::TimeZone["Hawaii"], time.time_zone
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_local_enforces_spring_dst_rules
|
2008-09-07 07:54:05 +02:00
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
2008-05-18 06:22:34 +02:00
|
|
|
twz = zone.local(2006,4,2,1,59,59) # 1 second before DST start
|
|
|
|
assert_equal Time.utc(2006,4,2,1,59,59), twz.time
|
|
|
|
assert_equal Time.utc(2006,4,2,6,59,59), twz.utc
|
|
|
|
assert_equal false, twz.dst?
|
|
|
|
assert_equal 'EST', twz.zone
|
|
|
|
twz2 = zone.local(2006,4,2,2) # 2AM does not exist because at 2AM, time springs forward to 3AM
|
|
|
|
assert_equal Time.utc(2006,4,2,3), twz2.time # twz is created for 3AM
|
|
|
|
assert_equal Time.utc(2006,4,2,7), twz2.utc
|
|
|
|
assert_equal true, twz2.dst?
|
|
|
|
assert_equal 'EDT', twz2.zone
|
|
|
|
twz3 = zone.local(2006,4,2,2,30) # 2:30AM does not exist because at 2AM, time springs forward to 3AM
|
|
|
|
assert_equal Time.utc(2006,4,2,3,30), twz3.time # twz is created for 3:30AM
|
|
|
|
assert_equal Time.utc(2006,4,2,7,30), twz3.utc
|
|
|
|
assert_equal true, twz3.dst?
|
|
|
|
assert_equal 'EDT', twz3.zone
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_local_enforces_fall_dst_rules
|
|
|
|
# 1AM during fall DST transition is ambiguous, it could be either DST or non-DST 1AM
|
|
|
|
# Mirroring Time.local behavior, this method selects the DST time
|
2008-09-07 07:54:05 +02:00
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
2008-05-18 06:22:34 +02:00
|
|
|
twz = zone.local(2006,10,29,1)
|
|
|
|
assert_equal Time.utc(2006,10,29,1), twz.time
|
|
|
|
assert_equal Time.utc(2006,10,29,5), twz.utc
|
2008-09-07 07:54:05 +02:00
|
|
|
assert_equal true, twz.dst?
|
2008-05-18 06:22:34 +02:00
|
|
|
assert_equal 'EDT', twz.zone
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_at
|
2008-09-07 07:54:05 +02:00
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
2008-05-18 06:22:34 +02:00
|
|
|
secs = 946684800.0
|
|
|
|
twz = zone.at(secs)
|
|
|
|
assert_equal Time.utc(1999,12,31,19), twz.time
|
|
|
|
assert_equal Time.utc(2000), twz.utc
|
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
assert_equal secs, twz.to_f
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_at_with_old_date
|
2008-09-07 07:54:05 +02:00
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
2008-05-18 06:22:34 +02:00
|
|
|
secs = DateTime.civil(1850).to_f
|
|
|
|
twz = zone.at(secs)
|
|
|
|
assert_equal [1850, 1, 1, 0], [twz.utc.year, twz.utc.mon, twz.utc.day, twz.utc.hour]
|
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
assert_equal secs, twz.to_f
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_parse
|
2008-09-07 07:54:05 +02:00
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
2008-05-18 06:22:34 +02:00
|
|
|
twz = zone.parse('1999-12-31 19:00:00')
|
|
|
|
assert_equal Time.utc(1999,12,31,19), twz.time
|
|
|
|
assert_equal Time.utc(2000), twz.utc
|
|
|
|
assert_equal zone, twz.time_zone
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_parse_string_with_timezone
|
|
|
|
(-11..13).each do |timezone_offset|
|
2008-09-07 07:54:05 +02:00
|
|
|
zone = ActiveSupport::TimeZone[timezone_offset]
|
2008-05-18 06:22:34 +02:00
|
|
|
twz = zone.parse('1999-12-31 19:00:00')
|
|
|
|
assert_equal twz, zone.parse(twz.to_s)
|
|
|
|
end
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_parse_with_old_date
|
2009-02-28 02:23:00 +01:00
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
|
|
|
twz = zone.parse('1850-12-31 19:00:00')
|
|
|
|
assert_equal [0,0,19,31,12,1850], twz.to_a[0,6]
|
|
|
|
assert_equal zone, twz.time_zone
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_parse_far_future_date_with_time_zone_offset_in_string
|
2009-02-28 02:23:00 +01:00
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
|
|
|
twz = zone.parse('2050-12-31 19:00:00 -10:00') # i.e., 2050-01-01 05:00:00 UTC
|
|
|
|
assert_equal [0,0,0,1,1,2051], twz.to_a[0,6]
|
|
|
|
assert_equal zone, twz.time_zone
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_parse_returns_nil_when_string_without_date_information_is_passed_in
|
2009-02-28 02:23:00 +01:00
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
|
|
|
assert_nil zone.parse('foobar')
|
|
|
|
assert_nil zone.parse(' ')
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def test_parse_with_incomplete_date
|
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
|
|
|
zone.stubs(:now).returns zone.local(1999,12,31)
|
|
|
|
twz = zone.parse('19:00:00')
|
|
|
|
assert_equal Time.utc(1999,12,31,19), twz.time
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_utc_offset_lazy_loaded_from_tzinfo_when_not_passed_in_to_initialize
|
2009-02-28 02:23:00 +01:00
|
|
|
tzinfo = TZInfo::Timezone.get('America/New_York')
|
|
|
|
zone = ActiveSupport::TimeZone.create(tzinfo.name, nil, tzinfo)
|
|
|
|
assert_equal nil, zone.instance_variable_get('@utc_offset')
|
|
|
|
assert_equal(-18_000, zone.utc_offset)
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_formatted_offset_positive
|
2008-09-07 07:54:05 +02:00
|
|
|
zone = ActiveSupport::TimeZone['Moscow']
|
2008-05-18 06:22:34 +02:00
|
|
|
assert_equal "+03:00", zone.formatted_offset
|
|
|
|
assert_equal "+0300", zone.formatted_offset(false)
|
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_formatted_offset_negative
|
2008-09-07 07:54:05 +02:00
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
2008-05-18 06:22:34 +02:00
|
|
|
assert_equal "-05:00", zone.formatted_offset
|
|
|
|
assert_equal "-0500", zone.formatted_offset(false)
|
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_formatted_offset_zero
|
2008-09-07 07:54:05 +02:00
|
|
|
zone = ActiveSupport::TimeZone['London']
|
2008-05-18 06:22:34 +02:00
|
|
|
assert_equal "+00:00", zone.formatted_offset
|
|
|
|
assert_equal "UTC", zone.formatted_offset(true, 'UTC')
|
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_zone_compare
|
2008-09-07 07:54:05 +02:00
|
|
|
zone1 = ActiveSupport::TimeZone['Central Time (US & Canada)'] # offset -0600
|
|
|
|
zone2 = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] # offset -0500
|
2007-01-22 14:43:50 +01:00
|
|
|
assert zone1 < zone2
|
|
|
|
assert zone2 > zone1
|
|
|
|
assert zone1 == zone1
|
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def test_zone_match
|
|
|
|
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
|
|
|
|
assert zone =~ /Eastern/
|
|
|
|
assert zone =~ /New_York/
|
|
|
|
assert zone !~ /Nonexistent_Place/
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_to_s
|
2008-09-07 07:54:05 +02:00
|
|
|
assert_equal "(GMT+03:00) Moscow", ActiveSupport::TimeZone['Moscow'].to_s
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_all_sorted
|
2008-09-07 07:54:05 +02:00
|
|
|
all = ActiveSupport::TimeZone.all
|
2007-01-22 14:43:50 +01:00
|
|
|
1.upto( all.length-1 ) do |i|
|
|
|
|
assert all[i-1] < all[i]
|
|
|
|
end
|
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_index
|
2008-09-07 07:54:05 +02:00
|
|
|
assert_nil ActiveSupport::TimeZone["bogus"]
|
|
|
|
assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone["Central Time (US & Canada)"]
|
|
|
|
assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[8]
|
|
|
|
assert_raises(ArgumentError) { ActiveSupport::TimeZone[false] }
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def test_new
|
2008-09-07 07:54:05 +02:00
|
|
|
assert_equal ActiveSupport::TimeZone["Central Time (US & Canada)"], ActiveSupport::TimeZone.new("Central Time (US & Canada)")
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2008-09-07 07:54:05 +02:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_us_zones
|
2008-09-07 07:54:05 +02:00
|
|
|
assert ActiveSupport::TimeZone.us_zones.include?(ActiveSupport::TimeZone["Hawaii"])
|
|
|
|
assert !ActiveSupport::TimeZone.us_zones.include?(ActiveSupport::TimeZone["Kuala Lumpur"])
|
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
protected
|
|
|
|
def with_env_tz(new_tz = 'US/Eastern')
|
|
|
|
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
|
2008-09-07 07:54:05 +02:00
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|