Skip to content

Commit 2ca2634

Browse files
authored
Merge pull request rails#53199 from bogdan/time-zone-standard-name
Add AS::TimeZone#standard_name method
2 parents dd6fcc4 + 0a97be3 commit 2ca2634

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

activesupport/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
* Add `ActiveSupport::TimeZone#standard_name` method.
2+
3+
``` ruby
4+
zone = ActiveSupport::TimeZone['Hawaii']
5+
# Old way
6+
ActiveSupport::TimeZone::MAPPING[zone.name]
7+
# New way
8+
zone.standard_name # => 'Pacific/Honolulu'
9+
```
10+
11+
*Bogdan Gusiev*
12+
113
* Add Structured Event Reporter, accessible via `Rails.event`.
214

315
The Event Reporter provides a unified interface for producing structured events in Rails

activesupport/lib/active_support/values/time_zone.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@ def initialize(name, utc_offset = nil, tzinfo = nil)
314314
end
315315
# :startdoc:
316316

317+
# Returns a standard time zone name defined by IANA
318+
# https://www.iana.org/time-zones
319+
def standard_name
320+
MAPPING[name] || name
321+
end
322+
317323
# Returns the offset of this time zone from UTC in seconds.
318324
def utc_offset
319325
@utc_offset || tzinfo&.current_period&.base_utc_offset

activesupport/test/time_zone_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,4 +920,10 @@ def test_works_as_ruby_time_zone
920920
assert_equal "EDT", time.strftime("%Z")
921921
assert_equal true, time.isdst
922922
end
923+
924+
def test_standard_name
925+
assert_equal "America/New_York", ActiveSupport::TimeZone["Eastern Time (US & Canada)"].standard_name
926+
assert_equal "America/Montevideo", ActiveSupport::TimeZone["Montevideo"].standard_name
927+
assert_equal "America/Toronto", ActiveSupport::TimeZone["America/Toronto"].standard_name
928+
end
923929
end

0 commit comments

Comments
 (0)