aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorAkira Matsuda <ronnie@dio.jp>2019-07-29 14:05:18 +0900
committerAkira Matsuda <ronnie@dio.jp>2019-07-29 14:17:36 +0900
commit8f90ac7827787a84b38055e1011ef9aedd89fe91 (patch)
treeda113b79ba98d60e5c4ee0705d03044254c22c84 /activesupport
parent381e8cb67abec7d2ceb812fde587d924667de122 (diff)
downloadrails-8f90ac7827787a84b38055e1011ef9aedd89fe91.tar.gz
rails-8f90ac7827787a84b38055e1011ef9aedd89fe91.tar.bz2
rails-8f90ac7827787a84b38055e1011ef9aedd89fe91.zip
Add AS::TimeZone#match?
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/values/time_zone.rb7
-rw-r--r--activesupport/test/time_zone_test.rb7
2 files changed, 14 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb
index d9e033e23b..90830b89bd 100644
--- a/activesupport/lib/active_support/values/time_zone.rb
+++ b/activesupport/lib/active_support/values/time_zone.rb
@@ -334,6 +334,13 @@ module ActiveSupport
re === name || re === MAPPING[name]
end
+ # Compare #name and TZInfo identifier to a supplied regexp, returning +true+
+ # if a match is found.
+ def match?(re)
+ (re == name) || (re == MAPPING[name]) ||
+ ((Regexp === re) && (re.match?(name) || re.match?(MAPPING[name])))
+ end
+
# Returns a textual representation of this time zone.
def to_s
"(GMT#{formatted_offset}) #{name}"
diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb
index 0c3a10995c..eccacce32a 100644
--- a/activesupport/test/time_zone_test.rb
+++ b/activesupport/test/time_zone_test.rb
@@ -717,6 +717,13 @@ class TimeZoneTest < ActiveSupport::TestCase
assert_no_match zone, /Nonexistent_Place/
end
+ def test_zone_match?
+ zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
+ assert zone.match?(/Eastern/)
+ assert zone.match?(/New_York/)
+ assert_not zone.match?(/Nonexistent_Place/)
+ end
+
def test_to_s
assert_equal "(GMT+05:30) New Delhi", ActiveSupport::TimeZone["New Delhi"].to_s
end