Merge pull request #52034 from jhawthorn/ruby_time_zone_object_support

Improve support for using ActiveSupport::TimeZone as a ::Time object's timezone
This commit is contained in:
John Hawthorn 2024-06-08 15:40:46 -07:00 committed by GitHub
commit a472403d55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 6 deletions

View File

@ -552,15 +552,11 @@ module ActiveSupport
tzinfo.local_to_utc(time, dst)
end
# Available so that TimeZone instances respond like +TZInfo::Timezone+
# instances.
def period_for_utc(time)
def period_for_utc(time) # :nodoc:
tzinfo.period_for_utc(time)
end
# Available so that TimeZone instances respond like +TZInfo::Timezone+
# instances.
def period_for_local(time, dst = true)
def period_for_local(time, dst = true) # :nodoc:
tzinfo.period_for_local(time, dst) { |periods| periods.last }
end
@ -568,6 +564,14 @@ module ActiveSupport
tzinfo.periods_for_local(time)
end
def abbr(time) # :nodoc:
tzinfo.abbr(time)
end
def dst?(time) # :nodoc:
tzinfo.dst?(time)
end
def init_with(coder) # :nodoc:
initialize(coder["name"])
end

View File

@ -876,4 +876,48 @@ class TimeZoneTest < ActiveSupport::TestCase
loaded = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(payload) : YAML.load(payload)
assert_equal(ActiveSupport::TimeZone["Pacific/Honolulu"], loaded)
end
def test_abbr
zone = ActiveSupport::TimeZone["America/Toronto"]
assert_equal "EST", zone.abbr(Time.utc(2000, 4, 2, 6))
assert_equal "EDT", zone.abbr(Time.utc(2000, 4, 2, 7))
assert_equal "EDT", zone.abbr(Time.utc(2000, 4, 2, 8))
assert_equal "EDT", zone.abbr(Time.utc(2000, 10, 29, 5))
assert_equal "EST", zone.abbr(Time.utc(2000, 10, 29, 6))
assert_equal "EST", zone.abbr(Time.utc(2000, 10, 29, 7))
end
def test_dst
zone = ActiveSupport::TimeZone["America/Toronto"]
assert_equal false, zone.dst?(Time.utc(2000, 4, 2, 6))
assert_equal true, zone.dst?(Time.utc(2000, 4, 2, 7))
assert_equal true, zone.dst?(Time.utc(2000, 4, 2, 8))
assert_equal true, zone.dst?(Time.utc(2000, 10, 29, 5))
assert_equal false, zone.dst?(Time.utc(2000, 10, 29, 6))
assert_equal false, zone.dst?(Time.utc(2000, 10, 29, 7))
end
def test_works_as_ruby_time_zone
zone = ActiveSupport::TimeZone["America/Toronto"]
time = Time.new(2000, 1, 1, 1, in: zone)
assert_same zone, time.zone
assert_equal "2000-01-01T01:00:00-05:00", time.iso8601
assert_equal(-18000, time.utc_offset)
assert_equal "EST", time.strftime("%Z")
assert_equal false, time.isdst
time = Time.new(2000, 6, 1, 1, in: zone)
assert_same zone, time.zone
assert_equal "2000-06-01T01:00:00-04:00", time.iso8601
assert_equal(-14400, time.utc_offset)
assert_equal "EDT", time.strftime("%Z")
assert_equal true, time.isdst
time = Time.at(959835600, in: zone)
assert_same zone, time.zone
assert_equal "2000-06-01T01:00:00-04:00", time.iso8601
assert_equal(-14400, time.utc_offset)
assert_equal "EDT", time.strftime("%Z")
assert_equal true, time.isdst
end
end