2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/time"
|
|
|
|
require "time_zone_test_helpers"
|
|
|
|
require "yaml"
|
2005-02-23 07:56:16 -05:00
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class TimeZoneTest < ActiveSupport::TestCase
|
2014-06-18 07:46:04 -04:00
|
|
|
include TimeZoneTestHelpers
|
|
|
|
|
2008-03-30 19:01:06 -04:00
|
|
|
def test_utc_to_local
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2009-02-08 14:07:10 -05:00
|
|
|
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-03-30 19:01:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_local_to_utc
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2009-02-08 14:07:10 -05:00
|
|
|
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-03-30 19:01:06 -04:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2008-03-30 19:01:06 -04:00
|
|
|
def test_period_for_local
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2009-02-08 14:07:10 -05:00
|
|
|
assert_instance_of TZInfo::TimezonePeriod, zone.period_for_local(Time.utc(2000))
|
2008-03-30 19:01:06 -04:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2014-09-29 10:07:18 -04:00
|
|
|
ActiveSupport::TimeZone::MAPPING.each_key do |name|
|
2008-03-30 19:01:06 -04:00
|
|
|
define_method("test_map_#{name.downcase.gsub(/[^a-z]/, '_')}_to_tzinfo") do
|
2009-02-08 14:07:10 -05:00
|
|
|
zone = ActiveSupport::TimeZone[name]
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_respond_to zone.tzinfo, :period_for_local
|
2005-02-24 06:56:09 -05:00
|
|
|
end
|
2008-03-30 19:01:06 -04:00
|
|
|
end
|
2005-02-23 07:56:16 -05:00
|
|
|
|
2017-11-11 10:19:29 -05:00
|
|
|
def test_period_for_local_with_ambigiuous_time
|
|
|
|
zone = ActiveSupport::TimeZone["Moscow"]
|
|
|
|
period = zone.period_for_local(Time.utc(2015, 1, 1))
|
|
|
|
assert_equal period, zone.period_for_local(Time.utc(2014, 10, 26, 1, 0, 0))
|
|
|
|
end
|
|
|
|
|
2008-03-30 19:01:06 -04:00
|
|
|
def test_from_integer_to_map
|
2008-06-03 14:32:53 -04:00
|
|
|
assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[-28800] # PST
|
2008-03-30 19:01:06 -04:00
|
|
|
end
|
2008-01-28 20:09:49 -05:00
|
|
|
|
2008-03-30 19:01:06 -04:00
|
|
|
def test_from_duration_to_map
|
2008-06-03 14:32:53 -04:00
|
|
|
assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[-480.minutes] # PST
|
2008-03-30 19:01:06 -04:00
|
|
|
end
|
2008-01-28 20:09:49 -05:00
|
|
|
|
2008-06-03 14:32:53 -04:00
|
|
|
ActiveSupport::TimeZone.all.each do |zone|
|
2016-08-06 12:03:25 -04:00
|
|
|
name = zone.name.downcase.gsub(/[^a-z]/, "_")
|
2008-03-30 19:01:06 -04:00
|
|
|
define_method("test_from_#{name}_to_map") do
|
2009-02-08 14:07:10 -05:00
|
|
|
assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[zone.name]
|
2008-03-30 19:01:06 -04:00
|
|
|
end
|
2008-01-20 22:55:54 -05:00
|
|
|
|
2008-03-30 19:01:06 -04:00
|
|
|
define_method("test_utc_offset_for_#{name}") do
|
2009-02-08 14:07:10 -05:00
|
|
|
period = zone.tzinfo.current_period
|
|
|
|
assert_equal period.utc_offset, zone.utc_offset
|
2008-01-20 22:55:54 -05:00
|
|
|
end
|
2008-03-30 19:01:06 -04:00
|
|
|
end
|
2005-02-23 07:56:16 -05:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_now
|
2016-08-06 12:03:25 -04:00
|
|
|
with_env_tz "US/Eastern" do
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"].dup
|
2012-03-16 14:24:52 -04:00
|
|
|
def zone.time_now; Time.local(2000); end
|
2008-11-22 18:19:19 -05:00
|
|
|
assert_instance_of ActiveSupport::TimeWithZone, zone.now
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal Time.utc(2000, 1, 1, 5), zone.now.utc
|
2008-11-22 18:19:19 -05:00
|
|
|
assert_equal Time.utc(2000), zone.now.time
|
|
|
|
assert_equal zone, zone.now.time_zone
|
2008-03-30 19:01:06 -04:00
|
|
|
end
|
2008-11-22 18:19:19 -05:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_now_enforces_spring_dst_rules
|
2016-08-06 12:03:25 -04:00
|
|
|
with_env_tz "US/Eastern" do
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"].dup
|
2012-03-16 14:24:52 -04:00
|
|
|
def zone.time_now
|
2016-10-28 23:05:58 -04:00
|
|
|
Time.local(2006, 4, 2, 2) # 2AM springs forward to 3AM
|
2012-03-16 14:24:52 -04:00
|
|
|
end
|
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal Time.utc(2006, 4, 2, 3), zone.now.time
|
2008-11-22 18:19:19 -05:00
|
|
|
assert_equal true, zone.now.dst?
|
2008-03-21 18:48:00 -04:00
|
|
|
end
|
2008-11-22 18:19:19 -05:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_now_enforces_fall_dst_rules
|
2016-08-06 12:03:25 -04:00
|
|
|
with_env_tz "US/Eastern" do
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"].dup
|
2012-03-16 14:24:52 -04:00
|
|
|
def zone.time_now
|
|
|
|
Time.at(1162098000) # equivalent to 1AM DST
|
|
|
|
end
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal Time.utc(2006, 10, 29, 1), zone.now.time
|
2008-11-22 18:19:19 -05:00
|
|
|
assert_equal true, zone.now.dst?
|
2008-03-21 18:48:00 -04:00
|
|
|
end
|
2008-11-22 18:19:19 -05:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2010-04-02 15:44:38 -04:00
|
|
|
def test_unknown_timezones_delegation_to_tzinfo
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["America/Montevideo"]
|
2010-04-02 15:44:38 -04:00
|
|
|
assert_equal ActiveSupport::TimeZone, zone.class
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal zone.object_id, ActiveSupport::TimeZone["America/Montevideo"].object_id
|
2010-04-02 15:44:38 -04:00
|
|
|
assert_equal Time.utc(2010, 1, 31, 22), zone.utc_to_local(Time.utc(2010, 2)) # daylight saving offset -0200
|
|
|
|
assert_equal Time.utc(2010, 3, 31, 21), zone.utc_to_local(Time.utc(2010, 4)) # standard offset -0300
|
|
|
|
end
|
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_today
|
2013-12-03 10:36:33 -05:00
|
|
|
travel_to(Time.utc(2000, 1, 1, 4, 59, 59)) # 1 sec before midnight Jan 1 EST
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeZone["Eastern Time (US & Canada)"].today
|
2013-12-03 10:36:33 -05:00
|
|
|
travel_to(Time.utc(2000, 1, 1, 5)) # midnight Jan 1 EST
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone["Eastern Time (US & Canada)"].today
|
2013-12-03 10:36:33 -05:00
|
|
|
travel_to(Time.utc(2000, 1, 2, 4, 59, 59)) # 1 sec before midnight Jan 2 EST
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone["Eastern Time (US & Canada)"].today
|
2013-12-03 10:36:33 -05:00
|
|
|
travel_to(Time.utc(2000, 1, 2, 5)) # midnight Jan 2 EST
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeZone["Eastern Time (US & Canada)"].today
|
2008-03-30 19:01:06 -04:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2013-11-09 09:23:15 -05:00
|
|
|
def test_tomorrow
|
|
|
|
travel_to(Time.utc(2000, 1, 1, 4, 59, 59)) # 1 sec before midnight Jan 1 EST
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone["Eastern Time (US & Canada)"].tomorrow
|
2013-11-09 09:23:15 -05:00
|
|
|
travel_to(Time.utc(2000, 1, 1, 5)) # midnight Jan 1 EST
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeZone["Eastern Time (US & Canada)"].tomorrow
|
2013-11-09 09:23:15 -05:00
|
|
|
travel_to(Time.utc(2000, 1, 2, 4, 59, 59)) # 1 sec before midnight Jan 2 EST
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeZone["Eastern Time (US & Canada)"].tomorrow
|
2013-11-09 09:23:15 -05:00
|
|
|
travel_to(Time.utc(2000, 1, 2, 5)) # midnight Jan 2 EST
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal Date.new(2000, 1, 3), ActiveSupport::TimeZone["Eastern Time (US & Canada)"].tomorrow
|
2013-11-09 09:23:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_yesterday
|
|
|
|
travel_to(Time.utc(2000, 1, 1, 4, 59, 59)) # 1 sec before midnight Jan 1 EST
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal Date.new(1999, 12, 30), ActiveSupport::TimeZone["Eastern Time (US & Canada)"].yesterday
|
2013-11-09 09:23:15 -05:00
|
|
|
travel_to(Time.utc(2000, 1, 1, 5)) # midnight Jan 1 EST
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeZone["Eastern Time (US & Canada)"].yesterday
|
2013-11-09 09:23:15 -05:00
|
|
|
travel_to(Time.utc(2000, 1, 2, 4, 59, 59)) # 1 sec before midnight Jan 2 EST
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeZone["Eastern Time (US & Canada)"].yesterday
|
2013-11-09 09:23:15 -05:00
|
|
|
travel_to(Time.utc(2000, 1, 2, 5)) # midnight Jan 2 EST
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone["Eastern Time (US & Canada)"].yesterday
|
2013-11-09 09:23:15 -05:00
|
|
|
end
|
|
|
|
|
2014-02-18 03:35:11 -05:00
|
|
|
def test_travel_to_a_date
|
|
|
|
with_env_tz do
|
2016-08-06 12:03:25 -04:00
|
|
|
Time.use_zone("Hawaii") do
|
2014-02-18 03:35:11 -05:00
|
|
|
date = Date.new(2014, 2, 18)
|
|
|
|
time = date.midnight
|
|
|
|
|
2014-02-18 03:52:42 -05:00
|
|
|
travel_to date do
|
|
|
|
assert_equal date, Date.current
|
|
|
|
assert_equal time, Time.current
|
|
|
|
end
|
2014-02-18 03:35:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-18 04:22:43 -05:00
|
|
|
def test_travel_to_travels_back_and_reraises_if_the_block_raises
|
|
|
|
ts = Time.current - 1.second
|
|
|
|
|
|
|
|
travel_to ts do
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
|
|
|
flunk # ensure travel_to re-raises
|
|
|
|
rescue
|
|
|
|
assert_not_equal ts, Time.current
|
|
|
|
end
|
|
|
|
|
2008-03-30 19:01:06 -04:00
|
|
|
def test_local
|
2009-02-08 14:07:10 -05: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-03-30 19:01:06 -04:00
|
|
|
end
|
2008-03-21 18:48:00 -04:00
|
|
|
|
2008-03-30 19:01:06 -04:00
|
|
|
def test_local_with_old_date
|
2009-02-08 14:07:10 -05:00
|
|
|
time = ActiveSupport::TimeZone["Hawaii"].local(1850, 2, 5, 15, 30, 45)
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal [45, 30, 15, 5, 2, 1850], time.to_a[0, 6]
|
2009-02-08 14:07:10 -05:00
|
|
|
assert_equal ActiveSupport::TimeZone["Hawaii"], time.time_zone
|
2008-03-30 19:01:06 -04:00
|
|
|
end
|
2008-03-21 18:48:00 -04:00
|
|
|
|
2008-03-30 19:01:06 -04:00
|
|
|
def test_local_enforces_spring_dst_rules
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2016-10-28 23:05:58 -04: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
|
2008-03-30 19:01:06 -04:00
|
|
|
assert_equal false, twz.dst?
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "EST", twz.zone
|
2016-10-28 23:05:58 -04:00
|
|
|
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
|
2008-03-30 19:01:06 -04:00
|
|
|
assert_equal true, twz2.dst?
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "EDT", twz2.zone
|
2016-10-28 23:05:58 -04:00
|
|
|
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
|
2008-03-30 19:01:06 -04:00
|
|
|
assert_equal true, twz3.dst?
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "EDT", twz3.zone
|
2008-03-30 19:01:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2016-10-28 23:05:58 -04: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-06-03 14:32:53 -04:00
|
|
|
assert_equal true, twz.dst?
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "EDT", twz.zone
|
2008-03-30 19:01:06 -04:00
|
|
|
end
|
2008-03-21 18:48:00 -04:00
|
|
|
|
2017-11-11 10:19:29 -05:00
|
|
|
def test_local_with_ambiguous_time
|
|
|
|
zone = ActiveSupport::TimeZone["Moscow"]
|
|
|
|
assert_equal Time.utc(2014, 10, 25, 22, 0, 0), zone.local(2014, 10, 26, 1, 0, 0)
|
|
|
|
end
|
|
|
|
|
2008-03-30 19:01:06 -04:00
|
|
|
def test_at
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2008-03-30 19:01:06 -04:00
|
|
|
secs = 946684800.0
|
|
|
|
twz = zone.at(secs)
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal Time.utc(1999, 12, 31, 19), twz.time
|
2008-03-30 19:01:06 -04:00
|
|
|
assert_equal Time.utc(2000), twz.utc
|
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
assert_equal secs, twz.to_f
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_at_with_old_date
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2008-03-30 19:01:06 -04: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
|
|
|
|
end
|
|
|
|
|
2017-03-03 09:58:35 -05:00
|
|
|
def test_iso8601
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.iso8601("1999-12-31T19: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
|
|
|
|
end
|
|
|
|
|
2017-03-03 14:21:08 -05:00
|
|
|
def test_iso8601_with_fractional_seconds
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.iso8601("1999-12-31T19:00:00.750")
|
|
|
|
assert_equal 750000, twz.time.usec
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 19, 0, 0 + Rational(3, 4)), twz.time
|
|
|
|
assert_equal Time.utc(2000, 1, 1, 0, 0, 0 + Rational(3, 4)), twz.utc
|
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_iso8601_with_zone
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.iso8601("1999-12-31T14:00:00-10:00")
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 19), twz.time
|
|
|
|
assert_equal Time.utc(2000), twz.utc
|
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
end
|
|
|
|
|
2017-03-03 09:58:35 -05:00
|
|
|
def test_iso8601_with_invalid_string
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
|
|
|
|
exception = assert_raises(ArgumentError) do
|
|
|
|
zone.iso8601("foobar")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "invalid date", exception.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_iso8601_with_missing_time_components
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.iso8601("1999-12-31")
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 0, 0, 0), twz.time
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 5, 0, 0), twz.utc
|
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_iso8601_with_old_date
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.iso8601("1883-12-31T19:00:00")
|
|
|
|
assert_equal [0, 0, 19, 31, 12, 1883], twz.to_a[0, 6]
|
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_iso8601_far_future_date_with_time_zone_offset_in_string
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.iso8601("2050-12-31T19: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
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_iso8601_should_not_black_out_system_timezone_dst_jump
|
|
|
|
with_env_tz("EET") do
|
|
|
|
zone = ActiveSupport::TimeZone["Pacific Time (US & Canada)"]
|
|
|
|
twz = zone.iso8601("2012-03-25T03:29:00")
|
|
|
|
assert_equal [0, 29, 3, 25, 3, 2012], twz.to_a[0, 6]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_iso8601_should_black_out_app_timezone_dst_jump
|
|
|
|
with_env_tz("EET") do
|
|
|
|
zone = ActiveSupport::TimeZone["Pacific Time (US & Canada)"]
|
|
|
|
twz = zone.iso8601("2012-03-11T02:29:00")
|
|
|
|
assert_equal [0, 29, 3, 11, 3, 2012], twz.to_a[0, 6]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_iso8601_doesnt_use_local_dst
|
|
|
|
with_env_tz "US/Eastern" do
|
|
|
|
zone = ActiveSupport::TimeZone["UTC"]
|
|
|
|
twz = zone.iso8601("2013-03-10T02:00:00")
|
|
|
|
assert_equal Time.utc(2013, 3, 10, 2, 0, 0), twz.time
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_iso8601_handles_dst_jump
|
|
|
|
with_env_tz "US/Eastern" do
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.iso8601("2013-03-10T02:00:00")
|
|
|
|
assert_equal Time.utc(2013, 3, 10, 3, 0, 0), twz.time
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-11 10:19:29 -05:00
|
|
|
def test_iso8601_with_ambiguous_time
|
|
|
|
zone = ActiveSupport::TimeZone["Moscow"]
|
|
|
|
assert_equal Time.utc(2014, 10, 25, 22, 0, 0), zone.parse("2014-10-26T01:00:00")
|
|
|
|
end
|
|
|
|
|
2008-03-30 19:01:06 -04:00
|
|
|
def test_parse
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.parse("1999-12-31 19:00:00")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal Time.utc(1999, 12, 31, 19), twz.time
|
2008-03-30 19:01:06 -04:00
|
|
|
assert_equal Time.utc(2000), twz.utc
|
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
end
|
|
|
|
|
2008-05-07 09:54:07 -04:00
|
|
|
def test_parse_string_with_timezone
|
|
|
|
(-11..13).each do |timezone_offset|
|
2008-06-03 14:32:53 -04:00
|
|
|
zone = ActiveSupport::TimeZone[timezone_offset]
|
2016-08-06 12:03:25 -04:00
|
|
|
twz = zone.parse("1999-12-31 19:00:00")
|
2008-05-07 09:54:07 -04:00
|
|
|
assert_equal twz, zone.parse(twz.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-03-30 19:01:06 -04:00
|
|
|
def test_parse_with_old_date
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.parse("1883-12-31 19:00:00")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal [0, 0, 19, 31, 12, 1883], twz.to_a[0, 6]
|
2009-02-08 14:07:10 -05:00
|
|
|
assert_equal zone, twz.time_zone
|
2008-03-30 19:01:06 -04:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2008-05-08 21:56:38 -04:00
|
|
|
def test_parse_far_future_date_with_time_zone_offset_in_string
|
2016-08-06 12:03:25 -04: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
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal [0, 0, 0, 1, 1, 2051], twz.to_a[0, 6]
|
2009-02-08 14:07:10 -05:00
|
|
|
assert_equal zone, twz.time_zone
|
2008-05-08 21:56:38 -04:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2008-05-08 22:30:17 -04:00
|
|
|
def test_parse_returns_nil_when_string_without_date_information_is_passed_in
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
assert_nil zone.parse("foobar")
|
|
|
|
assert_nil zone.parse(" ")
|
2008-05-08 22:30:17 -04:00
|
|
|
end
|
2008-03-21 18:48:00 -04:00
|
|
|
|
2008-11-22 18:19:19 -05:00
|
|
|
def test_parse_with_incomplete_date
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2016-10-28 23:05:58 -04:00
|
|
|
zone.stub(:now, zone.local(1999, 12, 31)) do
|
2016-08-06 12:03:25 -04:00
|
|
|
twz = zone.parse("19:00:00")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal Time.utc(1999, 12, 31, 19), twz.time
|
2015-06-04 14:25:45 -04:00
|
|
|
end
|
2008-03-30 19:01:06 -04:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2014-05-07 12:12:25 -04:00
|
|
|
def test_parse_with_day_omitted
|
2016-08-06 12:03:25 -04:00
|
|
|
with_env_tz "US/Eastern" do
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
assert_equal Time.local(2000, 2, 1), zone.parse("Feb", Time.local(2000, 1, 1))
|
|
|
|
assert_equal Time.local(2005, 2, 1), zone.parse("Feb 2005", Time.local(2000, 1, 1))
|
|
|
|
assert_equal Time.local(2005, 2, 2), zone.parse("2 Feb 2005", Time.local(2000, 1, 1))
|
2014-05-11 11:00:55 -04:00
|
|
|
end
|
2014-05-07 12:12:25 -04:00
|
|
|
end
|
|
|
|
|
2012-03-23 05:10:14 -04:00
|
|
|
def test_parse_should_not_black_out_system_timezone_dst_jump
|
2016-08-06 12:03:25 -04:00
|
|
|
with_env_tz("EET") do
|
|
|
|
zone = ActiveSupport::TimeZone["Pacific Time (US & Canada)"]
|
|
|
|
twz = zone.parse("2012-03-25 03:29:00")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal [0, 29, 3, 25, 3, 2012], twz.to_a[0, 6]
|
2012-12-01 06:15:29 -05:00
|
|
|
end
|
2012-03-23 05:10:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_should_black_out_app_timezone_dst_jump
|
2016-08-06 12:03:25 -04:00
|
|
|
with_env_tz("EET") do
|
|
|
|
zone = ActiveSupport::TimeZone["Pacific Time (US & Canada)"]
|
|
|
|
twz = zone.parse("2012-03-11 02:29:00")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal [0, 29, 3, 11, 3, 2012], twz.to_a[0, 6]
|
2012-12-01 06:15:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-01 15:43:07 -05:00
|
|
|
def test_parse_with_missing_time_components
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2015-06-04 14:25:45 -04:00
|
|
|
zone.stub(:now, zone.local(1999, 12, 31, 12, 59, 59)) do
|
2016-08-06 12:03:25 -04:00
|
|
|
twz = zone.parse("2012-12-01")
|
2015-06-04 14:25:45 -04:00
|
|
|
assert_equal Time.utc(2012, 12, 1), twz.time
|
|
|
|
end
|
2012-12-01 15:43:07 -05:00
|
|
|
end
|
|
|
|
|
2012-12-01 06:15:29 -05:00
|
|
|
def test_parse_with_javascript_date
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2012-12-01 06:15:29 -05:00
|
|
|
twz = zone.parse("Mon May 28 2012 00:00:00 GMT-0700 (PDT)")
|
|
|
|
assert_equal Time.utc(2012, 5, 28, 7, 0, 0), twz.utc
|
2012-03-23 05:10:14 -04:00
|
|
|
end
|
|
|
|
|
2013-03-13 01:46:34 -04:00
|
|
|
def test_parse_doesnt_use_local_dst
|
2016-08-06 12:03:25 -04:00
|
|
|
with_env_tz "US/Eastern" do
|
|
|
|
zone = ActiveSupport::TimeZone["UTC"]
|
|
|
|
twz = zone.parse("2013-03-10 02:00:00")
|
2013-03-13 01:46:34 -04:00
|
|
|
assert_equal Time.utc(2013, 3, 10, 2, 0, 0), twz.time
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_handles_dst_jump
|
2016-08-06 12:03:25 -04:00
|
|
|
with_env_tz "US/Eastern" do
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.parse("2013-03-10 02:00:00")
|
2013-03-13 01:46:34 -04:00
|
|
|
assert_equal Time.utc(2013, 3, 10, 3, 0, 0), twz.time
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-04 17:37:02 -04:00
|
|
|
def test_parse_with_invalid_date
|
|
|
|
zone = ActiveSupport::TimeZone["UTC"]
|
|
|
|
|
|
|
|
exception = assert_raises(ArgumentError) do
|
|
|
|
zone.parse("9000")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "argument out of range", exception.message
|
|
|
|
end
|
|
|
|
|
2017-11-11 10:19:29 -05:00
|
|
|
def test_parse_with_ambiguous_time
|
|
|
|
zone = ActiveSupport::TimeZone["Moscow"]
|
|
|
|
assert_equal Time.utc(2014, 10, 25, 22, 0, 0), zone.parse("2014-10-26 01:00:00")
|
|
|
|
end
|
|
|
|
|
2017-03-03 14:21:08 -05:00
|
|
|
def test_rfc3339
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.rfc3339("1999-12-31T14:00:00-10:00")
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 19), twz.time
|
|
|
|
assert_equal Time.utc(2000), twz.utc
|
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rfc3339_with_fractional_seconds
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.iso8601("1999-12-31T14:00:00.750-10:00")
|
|
|
|
assert_equal 750000, twz.time.usec
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 19, 0, 0 + Rational(3, 4)), twz.time
|
|
|
|
assert_equal Time.utc(2000, 1, 1, 0, 0, 0 + Rational(3, 4)), twz.utc
|
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rfc3339_with_missing_time
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
|
|
|
|
exception = assert_raises(ArgumentError) do
|
|
|
|
zone.rfc3339("1999-12-31")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "invalid date", exception.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rfc3339_with_missing_offset
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
|
|
|
|
exception = assert_raises(ArgumentError) do
|
|
|
|
zone.rfc3339("1999-12-31T19:00:00")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "invalid date", exception.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rfc3339_with_invalid_string
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
|
|
|
|
exception = assert_raises(ArgumentError) do
|
|
|
|
zone.rfc3339("foobar")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "invalid date", exception.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rfc3339_with_old_date
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.rfc3339("1883-12-31T19:00:00-05:00")
|
|
|
|
assert_equal [0, 0, 19, 31, 12, 1883], twz.to_a[0, 6]
|
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rfc3339_far_future_date_with_time_zone_offset_in_string
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.rfc3339("2050-12-31T19: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
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rfc3339_should_not_black_out_system_timezone_dst_jump
|
|
|
|
with_env_tz("EET") do
|
|
|
|
zone = ActiveSupport::TimeZone["Pacific Time (US & Canada)"]
|
|
|
|
twz = zone.rfc3339("2012-03-25T03:29:00-07:00")
|
|
|
|
assert_equal [0, 29, 3, 25, 3, 2012], twz.to_a[0, 6]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rfc3339_should_black_out_app_timezone_dst_jump
|
|
|
|
with_env_tz("EET") do
|
|
|
|
zone = ActiveSupport::TimeZone["Pacific Time (US & Canada)"]
|
|
|
|
twz = zone.rfc3339("2012-03-11T02:29:00-08:00")
|
|
|
|
assert_equal [0, 29, 3, 11, 3, 2012], twz.to_a[0, 6]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rfc3339_doesnt_use_local_dst
|
|
|
|
with_env_tz "US/Eastern" do
|
|
|
|
zone = ActiveSupport::TimeZone["UTC"]
|
|
|
|
twz = zone.rfc3339("2013-03-10T02:00:00Z")
|
|
|
|
assert_equal Time.utc(2013, 3, 10, 2, 0, 0), twz.time
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rfc3339_handles_dst_jump
|
|
|
|
with_env_tz "US/Eastern" do
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.iso8601("2013-03-10T02:00:00-05:00")
|
|
|
|
assert_equal Time.utc(2013, 3, 10, 3, 0, 0), twz.time
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-02 01:15:38 -04:00
|
|
|
def test_strptime
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.strptime("1999-12-31 12:00:00", "%Y-%m-%d %H:%M:%S")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal Time.utc(1999, 12, 31, 17), twz
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 12), twz.time
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 17), twz.utc
|
2015-04-02 01:15:38 -04:00
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_strptime_with_nondefault_time_zone
|
2016-08-06 12:03:25 -04:00
|
|
|
with_tz_default ActiveSupport::TimeZone["Pacific Time (US & Canada)"] do
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.strptime("1999-12-31 12:00:00", "%Y-%m-%d %H:%M:%S")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal Time.utc(1999, 12, 31, 17), twz
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 12), twz.time
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 17), twz.utc
|
2015-04-02 01:15:38 -04:00
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_strptime_with_explicit_time_zone_as_abbrev
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.strptime("1999-12-31 12:00:00 PST", "%Y-%m-%d %H:%M:%S %Z")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal Time.utc(1999, 12, 31, 20), twz
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 15), twz.time
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 20), twz.utc
|
2015-04-02 01:15:38 -04:00
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_strptime_with_explicit_time_zone_as_h_offset
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.strptime("1999-12-31 12:00:00 -08", "%Y-%m-%d %H:%M:%S %:::z")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal Time.utc(1999, 12, 31, 20), twz
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 15), twz.time
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 20), twz.utc
|
2015-04-02 01:15:38 -04:00
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_strptime_with_explicit_time_zone_as_hm_offset
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.strptime("1999-12-31 12:00:00 -08:00", "%Y-%m-%d %H:%M:%S %:z")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal Time.utc(1999, 12, 31, 20), twz
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 15), twz.time
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 20), twz.utc
|
2015-04-02 01:15:38 -04:00
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_strptime_with_explicit_time_zone_as_hms_offset
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.strptime("1999-12-31 12:00:00 -08:00:00", "%Y-%m-%d %H:%M:%S %::z")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal Time.utc(1999, 12, 31, 20), twz
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 15), twz.time
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 20), twz.utc
|
2015-04-02 01:15:38 -04:00
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_strptime_with_almost_explicit_time_zone
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
twz = zone.strptime("1999-12-31 12:00:00 %Z", "%Y-%m-%d %H:%M:%S %%Z")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal Time.utc(1999, 12, 31, 17), twz
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 12), twz.time
|
|
|
|
assert_equal Time.utc(1999, 12, 31, 17), twz.utc
|
2015-04-02 01:15:38 -04:00
|
|
|
assert_equal zone, twz.time_zone
|
|
|
|
end
|
|
|
|
|
2015-04-22 12:01:58 -04:00
|
|
|
def test_strptime_with_day_omitted
|
2016-08-06 12:03:25 -04:00
|
|
|
with_env_tz "US/Eastern" do
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
assert_equal Time.local(2000, 2, 1), zone.strptime("Feb", "%b", Time.local(2000, 1, 1))
|
|
|
|
assert_equal Time.local(2005, 2, 1), zone.strptime("Feb 2005", "%b %Y", Time.local(2000, 1, 1))
|
|
|
|
assert_equal Time.local(2005, 2, 2), zone.strptime("2 Feb 2005", "%e %b %Y", Time.local(2000, 1, 1))
|
2015-04-22 12:01:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-11 23:48:56 -04:00
|
|
|
def test_strptime_with_malformed_string
|
2016-08-06 12:03:25 -04:00
|
|
|
with_env_tz "US/Eastern" do
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
assert_raise(ArgumentError) { zone.strptime("1999-12-31", "%Y/%m/%d") }
|
2016-07-11 23:48:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-20 08:44:22 -04:00
|
|
|
def test_strptime_with_timestamp_seconds
|
|
|
|
with_env_tz "US/Eastern" do
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
time_str = "1470272280"
|
|
|
|
time = zone.strptime(time_str, "%s")
|
|
|
|
assert_equal Time.at(1470272280), time
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_strptime_with_timestamp_milliseconds
|
|
|
|
with_env_tz "US/Eastern" do
|
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
|
|
|
time_str = "1470272280000"
|
|
|
|
time = zone.strptime(time_str, "%Q")
|
|
|
|
assert_equal Time.at(1470272280), time
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-11 10:19:29 -05:00
|
|
|
def test_strptime_with_ambiguous_time
|
|
|
|
zone = ActiveSupport::TimeZone["Moscow"]
|
|
|
|
assert_equal Time.utc(2014, 10, 25, 22, 0, 0), zone.strptime("2014-10-26 01:00:00", "%Y-%m-%d %H:%M:%S")
|
|
|
|
end
|
|
|
|
|
2008-03-30 19:01:06 -04:00
|
|
|
def test_utc_offset_lazy_loaded_from_tzinfo_when_not_passed_in_to_initialize
|
2016-08-06 12:03:25 -04:00
|
|
|
tzinfo = TZInfo::Timezone.get("America/New_York")
|
2009-02-08 14:07:10 -05:00
|
|
|
zone = ActiveSupport::TimeZone.create(tzinfo.name, nil, tzinfo)
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil zone.instance_variable_get("@utc_offset")
|
2009-02-08 14:07:10 -05:00
|
|
|
assert_equal(-18_000, zone.utc_offset)
|
2008-01-20 22:55:54 -05:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2016-04-20 18:53:48 -04:00
|
|
|
def test_utc_offset_is_not_cached_when_current_period_gets_stale
|
2016-08-06 12:03:25 -04:00
|
|
|
tz = ActiveSupport::TimeZone.create("Moscow")
|
2016-04-20 18:53:48 -04:00
|
|
|
travel_to(Time.utc(2014, 10, 25, 21)) do # 1 hour before TZ change
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal 14400, tz.utc_offset, "utc_offset should be initialized according to current_period"
|
2016-04-20 18:53:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
travel_to(Time.utc(2014, 10, 25, 22)) do # after TZ change
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal 10800, tz.utc_offset, "utc_offset should not be cached when current_period gets stale"
|
2016-04-20 18:53:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-24 01:01:51 -04:00
|
|
|
def test_seconds_to_utc_offset_with_colon
|
2009-03-26 22:02:11 -04:00
|
|
|
assert_equal "-06:00", ActiveSupport::TimeZone.seconds_to_utc_offset(-21_600)
|
|
|
|
assert_equal "+00:00", ActiveSupport::TimeZone.seconds_to_utc_offset(0)
|
|
|
|
assert_equal "+05:00", ActiveSupport::TimeZone.seconds_to_utc_offset(18_000)
|
2009-03-24 01:01:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_seconds_to_utc_offset_without_colon
|
2009-03-26 22:02:11 -04:00
|
|
|
assert_equal "-0600", ActiveSupport::TimeZone.seconds_to_utc_offset(-21_600, false)
|
|
|
|
assert_equal "+0000", ActiveSupport::TimeZone.seconds_to_utc_offset(0, false)
|
|
|
|
assert_equal "+0500", ActiveSupport::TimeZone.seconds_to_utc_offset(18_000, false)
|
2009-03-24 01:01:51 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-01-18 13:55:48 -05:00
|
|
|
def test_seconds_to_utc_offset_with_negative_offset
|
|
|
|
assert_equal "-01:00", ActiveSupport::TimeZone.seconds_to_utc_offset(-3_600)
|
|
|
|
assert_equal "-00:59", ActiveSupport::TimeZone.seconds_to_utc_offset(-3_599)
|
|
|
|
assert_equal "-05:30", ActiveSupport::TimeZone.seconds_to_utc_offset(-19_800)
|
|
|
|
end
|
2009-03-24 01:01:51 -04:00
|
|
|
|
2005-02-23 07:56:16 -05:00
|
|
|
def test_formatted_offset_positive
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["New Delhi"]
|
2011-06-28 16:17:56 -04:00
|
|
|
assert_equal "+05:30", zone.formatted_offset
|
|
|
|
assert_equal "+0530", zone.formatted_offset(false)
|
2005-02-23 07:56:16 -05:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2005-02-23 07:56:16 -05:00
|
|
|
def test_formatted_offset_negative
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2008-01-20 22:55:54 -05:00
|
|
|
assert_equal "-05:00", zone.formatted_offset
|
|
|
|
assert_equal "-0500", zone.formatted_offset(false)
|
2005-02-23 07:56:16 -05:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2012-09-19 11:51:28 -04:00
|
|
|
def test_z_format_strings
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Tokyo"]
|
2012-09-19 11:51:28 -04:00
|
|
|
twz = zone.now
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "+0900", twz.strftime("%z")
|
|
|
|
assert_equal "+09:00", twz.strftime("%:z")
|
|
|
|
assert_equal "+09:00:00", twz.strftime("%::z")
|
2012-09-19 11:51:28 -04:00
|
|
|
end
|
|
|
|
|
2008-01-22 21:11:00 -05:00
|
|
|
def test_formatted_offset_zero
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["London"]
|
2008-01-22 21:11:00 -05:00
|
|
|
assert_equal "+00:00", zone.formatted_offset
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "UTC", zone.formatted_offset(true, "UTC")
|
2008-01-22 21:11:00 -05:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2005-02-23 07:56:16 -05:00
|
|
|
def test_zone_compare
|
2016-08-06 12:03:25 -04:00
|
|
|
zone1 = ActiveSupport::TimeZone["Central Time (US & Canada)"] # offset -0600
|
|
|
|
zone2 = ActiveSupport::TimeZone["Eastern Time (US & Canada)"] # offset -0500
|
2005-02-23 07:56:16 -05:00
|
|
|
assert zone1 < zone2
|
|
|
|
assert zone2 > zone1
|
|
|
|
assert zone1 == zone1
|
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2008-06-28 22:27:32 -04:00
|
|
|
def test_zone_match
|
2016-08-06 12:03:25 -04:00
|
|
|
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
2008-06-28 22:27:32 -04:00
|
|
|
assert zone =~ /Eastern/
|
|
|
|
assert zone =~ /New_York/
|
|
|
|
assert zone !~ /Nonexistent_Place/
|
|
|
|
end
|
|
|
|
|
2005-02-23 07:56:16 -05:00
|
|
|
def test_to_s
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "(GMT+05:30) New Delhi", ActiveSupport::TimeZone["New Delhi"].to_s
|
2005-02-23 07:56:16 -05:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2005-02-23 07:56:16 -05:00
|
|
|
def test_all_sorted
|
2008-06-03 14:32:53 -04:00
|
|
|
all = ActiveSupport::TimeZone.all
|
2016-10-28 23:05:58 -04:00
|
|
|
1.upto(all.length - 1) do |i|
|
|
|
|
assert all[i - 1] < all[i]
|
2005-02-23 07:56:16 -05:00
|
|
|
end
|
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2017-11-22 12:12:08 -05:00
|
|
|
def test_all_uninfluenced_by_time_zone_lookups_delegated_to_tzinfo
|
|
|
|
ActiveSupport::TimeZone.clear
|
|
|
|
galapagos = ActiveSupport::TimeZone["Pacific/Galapagos"]
|
|
|
|
all_zones = ActiveSupport::TimeZone.all
|
|
|
|
assert_not_includes all_zones, galapagos
|
|
|
|
end
|
|
|
|
|
2018-04-19 14:19:51 -04:00
|
|
|
def test_all_doesnt_raise_exception_with_missing_tzinfo_data
|
2018-04-17 16:03:02 -04:00
|
|
|
mappings = {
|
|
|
|
"Puerto Rico" => "America/Unknown",
|
|
|
|
"Pittsburgh" => "America/New_York"
|
|
|
|
}
|
|
|
|
|
|
|
|
with_tz_mappings(mappings) do
|
|
|
|
assert_nil ActiveSupport::TimeZone["Puerto Rico"]
|
|
|
|
assert_nil ActiveSupport::TimeZone[-9]
|
|
|
|
assert_nothing_raised do
|
|
|
|
ActiveSupport::TimeZone.all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-02-23 07:56:16 -05:00
|
|
|
def test_index
|
2010-06-28 13:01:48 -04:00
|
|
|
assert_nil ActiveSupport::TimeZone["bogus"]
|
2008-06-03 14:32:53 -04:00
|
|
|
assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone["Central Time (US & Canada)"]
|
|
|
|
assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[8]
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(ArgumentError) { ActiveSupport::TimeZone[false] }
|
2005-02-23 07:56:16 -05:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2015-01-04 11:39:49 -05:00
|
|
|
def test_unknown_zone_raises_exception
|
|
|
|
assert_raise TZInfo::InvalidTimezoneIdentifier do
|
|
|
|
ActiveSupport::TimeZone.create("bogus")
|
|
|
|
end
|
2010-04-14 23:25:47 -04:00
|
|
|
end
|
|
|
|
|
2010-06-28 13:04:11 -04:00
|
|
|
def test_unknown_zones_dont_store_mapping_keys
|
2014-03-03 02:13:10 -05:00
|
|
|
assert_nil ActiveSupport::TimeZone["bogus"]
|
2010-06-28 13:04:11 -04:00
|
|
|
end
|
|
|
|
|
2005-02-23 07:56:16 -05:00
|
|
|
def test_new
|
2008-06-03 14:32:53 -04:00
|
|
|
assert_equal ActiveSupport::TimeZone["Central Time (US & Canada)"], ActiveSupport::TimeZone.new("Central Time (US & Canada)")
|
2005-02-23 07:56:16 -05:00
|
|
|
end
|
2008-06-03 14:32:53 -04:00
|
|
|
|
2007-06-25 14:22:31 -04:00
|
|
|
def test_us_zones
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes ActiveSupport::TimeZone.us_zones, ActiveSupport::TimeZone["Hawaii"]
|
|
|
|
assert_not_includes ActiveSupport::TimeZone.us_zones, ActiveSupport::TimeZone["Kuala Lumpur"]
|
2008-06-03 14:32:53 -04:00
|
|
|
end
|
2014-10-20 04:12:35 -04:00
|
|
|
|
2015-06-18 16:13:15 -04:00
|
|
|
def test_country_zones
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes ActiveSupport::TimeZone.country_zones("ru"), ActiveSupport::TimeZone["Moscow"]
|
|
|
|
assert_not_includes ActiveSupport::TimeZone.country_zones(:ru), ActiveSupport::TimeZone["Kuala Lumpur"]
|
2015-06-18 16:13:15 -04:00
|
|
|
end
|
|
|
|
|
2018-02-19 05:24:13 -05:00
|
|
|
def test_country_zones_with_and_without_mappings
|
|
|
|
assert_includes ActiveSupport::TimeZone.country_zones("au"), ActiveSupport::TimeZone["Adelaide"]
|
|
|
|
assert_includes ActiveSupport::TimeZone.country_zones("au"), ActiveSupport::TimeZone["Australia/Lord_Howe"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_country_zones_with_multiple_mappings
|
|
|
|
assert_includes ActiveSupport::TimeZone.country_zones("gb"), ActiveSupport::TimeZone["Edinburgh"]
|
|
|
|
assert_includes ActiveSupport::TimeZone.country_zones("gb"), ActiveSupport::TimeZone["London"]
|
|
|
|
end
|
|
|
|
|
2017-03-28 08:11:55 -04:00
|
|
|
def test_country_zones_without_mappings
|
|
|
|
assert_includes ActiveSupport::TimeZone.country_zones(:sv), ActiveSupport::TimeZone["America/El_Salvador"]
|
|
|
|
end
|
|
|
|
|
2014-10-20 04:12:35 -04:00
|
|
|
def test_to_yaml
|
|
|
|
assert_equal("--- !ruby/object:ActiveSupport::TimeZone\nname: Pacific/Honolulu\n", ActiveSupport::TimeZone["Hawaii"].to_yaml)
|
|
|
|
assert_equal("--- !ruby/object:ActiveSupport::TimeZone\nname: Europe/London\n", ActiveSupport::TimeZone["Europe/London"].to_yaml)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_yaml_load
|
|
|
|
assert_equal(ActiveSupport::TimeZone["Pacific/Honolulu"], YAML.load("--- !ruby/object:ActiveSupport::TimeZone\nname: Pacific/Honolulu\n"))
|
|
|
|
end
|
2008-03-11 01:39:13 -04:00
|
|
|
end
|