Method: Time#change
- Defined in:
- lib/active_support/core_ext/time/calculations.rb
#change(options) ⇒ Object
Returns a new Time 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
. Pass either :usec
or :nsec
, not both.
Time.new(2012, 8, 29, 22, 35, 0).change(day: 1) # => Time.new(2012, 8, 1, 22, 35, 0)
Time.new(2012, 8, 29, 22, 35, 0).change(year: 1981, day: 1) # => Time.new(1981, 8, 1, 22, 35, 0)
Time.new(2012, 8, 29, 22, 35, 0).change(year: 1981, hour: 0) # => Time.new(1981, 8, 29, 0, 0, 0)
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/active_support/core_ext/time/calculations.rb', line 118 def change() new_year = .fetch(:year, year) new_month = .fetch(:month, month) new_day = .fetch(:day, day) new_hour = .fetch(:hour, hour) new_min = .fetch(:min, [:hour] ? 0 : min) new_sec = .fetch(:sec, ([:hour] || [:min]) ? 0 : sec) if new_nsec = [:nsec] raise ArgumentError, "Can't change both :nsec and :usec at the same time: #{.inspect}" if [:usec] new_usec = Rational(new_nsec, 1000) else new_usec = .fetch(:usec, ([:hour] || [:min] || [:sec]) ? 0 : Rational(nsec, 1000)) end if utc? ::Time.utc(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec) elsif zone ::Time.local(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec) else raise ArgumentError, "argument out of range" if new_usec >= 1000000 ::Time.new(new_year, new_month, new_day, new_hour, new_min, new_sec + (new_usec.to_r / 1000000), utc_offset) end end |