Skip to content

Commit a472403

Browse files
authored
Merge pull request rails#52034 from jhawthorn/ruby_time_zone_object_support
Improve support for using ActiveSupport::TimeZone as a ::Time object's timezone
2 parents 9fdaea5 + a376cc2 commit a472403

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

activesupport/lib/active_support/values/time_zone.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -552,22 +552,26 @@ def local_to_utc(time, dst = true)
552552
tzinfo.local_to_utc(time, dst)
553553
end
554554

555-
# Available so that TimeZone instances respond like +TZInfo::Timezone+
556-
# instances.
557-
def period_for_utc(time)
555+
def period_for_utc(time) # :nodoc:
558556
tzinfo.period_for_utc(time)
559557
end
560558

561-
# Available so that TimeZone instances respond like +TZInfo::Timezone+
562-
# instances.
563-
def period_for_local(time, dst = true)
559+
def period_for_local(time, dst = true) # :nodoc:
564560
tzinfo.period_for_local(time, dst) { |periods| periods.last }
565561
end
566562

567563
def periods_for_local(time) # :nodoc:
568564
tzinfo.periods_for_local(time)
569565
end
570566

567+
def abbr(time) # :nodoc:
568+
tzinfo.abbr(time)
569+
end
570+
571+
def dst?(time) # :nodoc:
572+
tzinfo.dst?(time)
573+
end
574+
571575
def init_with(coder) # :nodoc:
572576
initialize(coder["name"])
573577
end

activesupport/test/time_zone_test.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,4 +876,48 @@ def test_yaml_load
876876
loaded = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(payload) : YAML.load(payload)
877877
assert_equal(ActiveSupport::TimeZone["Pacific/Honolulu"], loaded)
878878
end
879+
880+
def test_abbr
881+
zone = ActiveSupport::TimeZone["America/Toronto"]
882+
assert_equal "EST", zone.abbr(Time.utc(2000, 4, 2, 6))
883+
assert_equal "EDT", zone.abbr(Time.utc(2000, 4, 2, 7))
884+
assert_equal "EDT", zone.abbr(Time.utc(2000, 4, 2, 8))
885+
assert_equal "EDT", zone.abbr(Time.utc(2000, 10, 29, 5))
886+
assert_equal "EST", zone.abbr(Time.utc(2000, 10, 29, 6))
887+
assert_equal "EST", zone.abbr(Time.utc(2000, 10, 29, 7))
888+
end
889+
890+
def test_dst
891+
zone = ActiveSupport::TimeZone["America/Toronto"]
892+
assert_equal false, zone.dst?(Time.utc(2000, 4, 2, 6))
893+
assert_equal true, zone.dst?(Time.utc(2000, 4, 2, 7))
894+
assert_equal true, zone.dst?(Time.utc(2000, 4, 2, 8))
895+
assert_equal true, zone.dst?(Time.utc(2000, 10, 29, 5))
896+
assert_equal false, zone.dst?(Time.utc(2000, 10, 29, 6))
897+
assert_equal false, zone.dst?(Time.utc(2000, 10, 29, 7))
898+
end
899+
900+
def test_works_as_ruby_time_zone
901+
zone = ActiveSupport::TimeZone["America/Toronto"]
902+
time = Time.new(2000, 1, 1, 1, in: zone)
903+
assert_same zone, time.zone
904+
assert_equal "2000-01-01T01:00:00-05:00", time.iso8601
905+
assert_equal(-18000, time.utc_offset)
906+
assert_equal "EST", time.strftime("%Z")
907+
assert_equal false, time.isdst
908+
909+
time = Time.new(2000, 6, 1, 1, in: zone)
910+
assert_same zone, time.zone
911+
assert_equal "2000-06-01T01:00:00-04:00", time.iso8601
912+
assert_equal(-14400, time.utc_offset)
913+
assert_equal "EDT", time.strftime("%Z")
914+
assert_equal true, time.isdst
915+
916+
time = Time.at(959835600, in: zone)
917+
assert_same zone, time.zone
918+
assert_equal "2000-06-01T01:00:00-04:00", time.iso8601
919+
assert_equal(-14400, time.utc_offset)
920+
assert_equal "EDT", time.strftime("%Z")
921+
assert_equal true, time.isdst
922+
end
879923
end

0 commit comments

Comments
 (0)