Skip to content

Commit

Permalink
Improve Time.find_zone
Browse files Browse the repository at this point in the history
  • Loading branch information
ksss committed Jul 16, 2024
1 parent 8307713 commit 912bc07
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
1 change: 1 addition & 0 deletions gems/activesupport/6.0/_test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
23 changes: 23 additions & 0 deletions gems/activesupport/6.0/activesupport-6.0.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tt>Time.zone=</tt>.
# Raises an +ArgumentError+ for invalid time zones.
#
# Time.find_zone! "America/New_York" # => #<ActiveSupport::TimeZone @name="America/New_York" ...>
# Time.find_zone! "EST" # => #<ActiveSupport::TimeZone @name="EST" ...>
# Time.find_zone! -5.hours # => #<ActiveSupport::TimeZone @name="Bogota" ...>
# 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 <tt>Time.zone=</tt>.
# Returns +nil+ for invalid time zones.
#
# Time.find_zone "America/New_York" # => #<ActiveSupport::TimeZone @name="America/New_York" ...>
# Time.find_zone "NOT-A-TIMEZONE" # => nil
def self.find_zone: (String | real | ActiveSupport::Duration | nil) -> ActiveSupport::TimeZone?
end
20 changes: 0 additions & 20 deletions gems/activesupport/6.0/activesupport-generated.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tt>Time.zone=</tt>.
# Raises an +ArgumentError+ for invalid time zones.
#
# Time.find_zone! "America/New_York" # => #<ActiveSupport::TimeZone @name="America/New_York" ...>
# Time.find_zone! "EST" # => #<ActiveSupport::TimeZone @name="EST" ...>
# Time.find_zone! -5.hours # => #<ActiveSupport::TimeZone @name="Bogota" ...>
# 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 <tt>Time.zone=</tt>.
# Returns +nil+ for invalid time zones.
#
# Time.find_zone "America/New_York" # => #<ActiveSupport::TimeZone @name="America/New_York" ...>
# Time.find_zone "NOT-A-TIMEZONE" # => nil
def self.find_zone: (untyped time_zone) -> untyped
end

module URI
Expand Down
1 change: 1 addition & 0 deletions gems/activesupport/7.0/_test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@

"Id".downcase_first
ActiveSupport::TimeZone['Asia/Tokyo'].to_s
Time.find_zone(Object.name)
23 changes: 23 additions & 0 deletions gems/activesupport/7.0/activesupport-7.0.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tt>Time.zone=</tt>.
# Raises an +ArgumentError+ for invalid time zones.
#
# Time.find_zone! "America/New_York" # => #<ActiveSupport::TimeZone @name="America/New_York" ...>
# Time.find_zone! "EST" # => #<ActiveSupport::TimeZone @name="EST" ...>
# Time.find_zone! -5.hours # => #<ActiveSupport::TimeZone @name="Bogota" ...>
# 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 <tt>Time.zone=</tt>.
# Returns +nil+ for invalid time zones.
#
# Time.find_zone "America/New_York" # => #<ActiveSupport::TimeZone @name="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.
Expand Down

0 comments on commit 912bc07

Please sign in to comment.