Skip to content

Commit edc7018

Browse files
committed
Add ActiveSupport::TimeZone#dst?
Like abbr, this is used by Ruby when specifying creating a ::Time with a zone object.
1 parent 2627134 commit edc7018

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

activesupport/lib/active_support/values/time_zone.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,12 @@ def abbr(time)
574574
tzinfo.abbr(time)
575575
end
576576

577+
# Available so that TimeZone instances respond like +TZInfo::Timezone+
578+
# instances.
579+
def dst?(time)
580+
tzinfo.dst?(time)
581+
end
582+
577583
def init_with(coder) # :nodoc:
578584
initialize(coder["name"])
579585
end

activesupport/test/time_zone_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,4 +886,38 @@ def test_abbr
886886
assert_equal "EST", zone.abbr(Time.utc(2000, 10, 29, 6))
887887
assert_equal "EST", zone.abbr(Time.utc(2000, 10, 29, 7))
888888
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
889923
end

0 commit comments

Comments
 (0)