From 912bc0752fad5a845f5e0b7f7ad83e421745f7cc Mon Sep 17 00:00:00 2001 From: ksss Date: Tue, 16 Jul 2024 22:42:44 +0900 Subject: [PATCH] Improve Time.find_zone --- gems/activesupport/6.0/_test/test.rb | 1 + gems/activesupport/6.0/activesupport-6.0.rbs | 23 +++++++++++++++++++ .../6.0/activesupport-generated.rbs | 20 ---------------- gems/activesupport/7.0/_test/test.rb | 1 + gems/activesupport/7.0/activesupport-7.0.rbs | 23 +++++++++++++++++++ 5 files changed, 48 insertions(+), 20 deletions(-) diff --git a/gems/activesupport/6.0/_test/test.rb b/gems/activesupport/6.0/_test/test.rb index 6bd57b8b..cf44b7cd 100644 --- a/gems/activesupport/6.0/_test/test.rb +++ b/gems/activesupport/6.0/_test/test.rb @@ -69,3 +69,4 @@ class TestAttrInternal "Matz".presence_in("Matzuyama")&.in?(%w[Yukihiro Matz Matsumoto]) ActiveSupport::TimeZone['Asia/Tokyo'].to_s +Time.find_zone(Object.name) diff --git a/gems/activesupport/6.0/activesupport-6.0.rbs b/gems/activesupport/6.0/activesupport-6.0.rbs index 1dc66e2d..fb2d0de0 100644 --- a/gems/activesupport/6.0/activesupport-6.0.rbs +++ b/gems/activesupport/6.0/activesupport-6.0.rbs @@ -23,3 +23,26 @@ module ActiveSupport def self.[]: (String | real | Duration) -> instance? end end + +# activesupport/lib/active_support/core_ext/time/zones.rb +class Time + # Returns a TimeZone instance matching the time zone provided. + # Accepts the time zone in any format supported by Time.zone=. + # Raises an +ArgumentError+ for invalid time zones. + # + # Time.find_zone! "America/New_York" # => # + # Time.find_zone! "EST" # => # + # Time.find_zone! -5.hours # => # + # Time.find_zone! nil # => nil + # Time.find_zone! false # => false + # Time.find_zone! "NOT-A-TIMEZONE" # => ArgumentError: Invalid Timezone: NOT-A-TIMEZONE + def self.find_zone!: (String | real | ActiveSupport::Duration) -> ActiveSupport::TimeZone + + # Returns a TimeZone instance matching the time zone provided. + # Accepts the time zone in any format supported by Time.zone=. + # Returns +nil+ for invalid time zones. + # + # Time.find_zone "America/New_York" # => # + # Time.find_zone "NOT-A-TIMEZONE" # => nil + def self.find_zone: (String | real | ActiveSupport::Duration | nil) -> ActiveSupport::TimeZone? +end diff --git a/gems/activesupport/6.0/activesupport-generated.rbs b/gems/activesupport/6.0/activesupport-generated.rbs index 69fbe3bb..025a76d7 100644 --- a/gems/activesupport/6.0/activesupport-generated.rbs +++ b/gems/activesupport/6.0/activesupport-generated.rbs @@ -5658,26 +5658,6 @@ class Time # attributes that have been read before the block will remain in # the application's default timezone. def self.use_zone: (untyped time_zone) { () -> untyped } -> untyped - - # Returns a TimeZone instance matching the time zone provided. - # Accepts the time zone in any format supported by Time.zone=. - # Raises an +ArgumentError+ for invalid time zones. - # - # Time.find_zone! "America/New_York" # => # - # Time.find_zone! "EST" # => # - # Time.find_zone! -5.hours # => # - # Time.find_zone! nil # => nil - # Time.find_zone! false # => false - # Time.find_zone! "NOT-A-TIMEZONE" # => ArgumentError: Invalid Timezone: NOT-A-TIMEZONE - def self.find_zone!: (untyped time_zone) -> untyped - - # Returns a TimeZone instance matching the time zone provided. - # Accepts the time zone in any format supported by Time.zone=. - # Returns +nil+ for invalid time zones. - # - # Time.find_zone "America/New_York" # => # - # Time.find_zone "NOT-A-TIMEZONE" # => nil - def self.find_zone: (untyped time_zone) -> untyped end module URI diff --git a/gems/activesupport/7.0/_test/test.rb b/gems/activesupport/7.0/_test/test.rb index dfb2126c..a222c898 100644 --- a/gems/activesupport/7.0/_test/test.rb +++ b/gems/activesupport/7.0/_test/test.rb @@ -20,3 +20,4 @@ "Id".downcase_first ActiveSupport::TimeZone['Asia/Tokyo'].to_s +Time.find_zone(Object.name) diff --git a/gems/activesupport/7.0/activesupport-7.0.rbs b/gems/activesupport/7.0/activesupport-7.0.rbs index d4660d59..a40b3e19 100644 --- a/gems/activesupport/7.0/activesupport-7.0.rbs +++ b/gems/activesupport/7.0/activesupport-7.0.rbs @@ -59,6 +59,29 @@ module ActiveSupport end end +# activesupport/lib/active_support/core_ext/time/zones.rb +class Time + # Returns a TimeZone instance matching the time zone provided. + # Accepts the time zone in any format supported by Time.zone=. + # Raises an +ArgumentError+ for invalid time zones. + # + # Time.find_zone! "America/New_York" # => # + # Time.find_zone! "EST" # => # + # Time.find_zone! -5.hours # => # + # Time.find_zone! nil # => nil + # Time.find_zone! false # => false + # Time.find_zone! "NOT-A-TIMEZONE" # => ArgumentError: Invalid Timezone: NOT-A-TIMEZONE + def self.find_zone!: (ActiveSupport::TimeZone | TZInfo::Timezone | String | real | ActiveSupport::Duration) -> ActiveSupport::TimeZone + + # Returns a TimeZone instance matching the time zone provided. + # Accepts the time zone in any format supported by Time.zone=. + # Returns +nil+ for invalid time zones. + # + # Time.find_zone "America/New_York" # => # + # Time.find_zone "NOT-A-TIMEZONE" # => nil + def self.find_zone: (ActiveSupport::TimeZone | TZInfo::Timezone | String | real | ActiveSupport::Duration | nil) -> ActiveSupport::TimeZone? +end + module Enumerable[unchecked out Elem] # Returns a new +Array+ without the blank items. # Uses Object#blank? for determining if an item is blank.