instance method change

Ruby on Rails 5.2.8.1

Since v5.2.8.1

Available in: v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

change(options)

Returns a new ActiveSupport::TimeWithZone where one or more of the elements have been changed according to the options parameter. The time options (:hour, :min, :sec, :usec, :nsec) reset cascadingly, so if only the hour is passed, then minute, sec, usec and nsec is set to 0. If the hour and minute is passed, then sec, usec and nsec is set to 0. The options parameter takes a hash with any of these keys: :year, :month, :day, :hour, :min, :sec, :usec, :nsec, :offset, :zone. Pass either :usec or :nsec, not both. Similarly, pass either :zone or :offset, not both.

t = Time.zone.now          # => Fri, 14 Apr 2017 11:45:15 EST -05:00
t.change(year: 2020)       # => Tue, 14 Apr 2020 11:45:15 EST -05:00
t.change(hour: 12)         # => Fri, 14 Apr 2017 12:00:00 EST -05:00
t.change(min: 30)          # => Fri, 14 Apr 2017 11:30:00 EST -05:00
t.change(offset: "-10:00") # => Fri, 14 Apr 2017 11:45:15 HST -10:00
t.change(zone: "Hawaii")   # => Fri, 14 Apr 2017 11:45:15 HST -10:00

Parameters

options req
Source
# File activesupport/lib/active_support/time_with_zone.rb, line 352
    def change(options)
      if options[:zone] && options[:offset]
        raise ArgumentError, "Can't change both :offset and :zone at the same time: #{options.inspect}"
      end

      new_time = time.change(options)

      if options[:zone]
        new_zone = ::Time.find_zone(options[:zone])
      elsif options[:offset]
        new_zone = ::Time.find_zone(new_time.utc_offset)
      end

      new_zone ||= time_zone
      periods = new_zone.periods_for_local(new_time)

      self.class.new(nil, new_zone, new_time, periods.include?(period) ? period : nil)
    end

Defined in activesupport/lib/active_support/time_with_zone.rb line 352 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveSupport::TimeWithZone

Type at least 2 characters to search.

↑↓ navigate · open · esc close