mirror of https://github.com/rails/rails
Merge pull request #16758 from Agis-/time-change-nsec
Add support for `:nsec` option to `Time#change`
This commit is contained in:
commit
4a7d889ff4
|
@ -1,3 +1,8 @@
|
|||
* Time#change can now change nanoseconds (:nsec) as a higher-precision
|
||||
alternative to microseconds (:usec).
|
||||
|
||||
*Agis Anastasooulos*
|
||||
|
||||
* `MessageVerifier.new` raises an appropriate exception if the secret is `nil`.
|
||||
This prevents `MessageVerifier#generate` from raising a cryptic error later on.
|
||||
|
||||
|
|
|
@ -64,11 +64,12 @@ class Time
|
|||
|
||||
# Returns a new Time where one or more of the elements have been changed according
|
||||
# to the +options+ parameter. The time options (<tt>:hour</tt>, <tt>:min</tt>,
|
||||
# <tt>:sec</tt>, <tt>:usec</tt>) reset cascadingly, so if only the hour is passed,
|
||||
# then minute, sec, and usec is set to 0. If the hour and minute is passed, then
|
||||
# sec and usec is set to 0. The +options+ parameter takes a hash with any of these
|
||||
# keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>, <tt>:min</tt>,
|
||||
# <tt>:sec</tt>, <tt>:usec</tt>.
|
||||
# <tt>:sec</tt>, <tt>:usec</tt>, <tt>:nsec</tt>) 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: <tt>:year</tt>, <tt>:month</tt>,
|
||||
# <tt>:day</tt>, <tt>:hour</tt>, <tt>:min</tt>, <tt>:sec</tt>, <tt>:usec</tt>
|
||||
# <tt>:nsec</tt>. Path either <tt>:usec</tt> or <tt>:nsec</tt>, 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)
|
||||
|
@ -80,7 +81,13 @@ class Time
|
|||
new_hour = options.fetch(:hour, hour)
|
||||
new_min = options.fetch(:min, options[:hour] ? 0 : min)
|
||||
new_sec = options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec)
|
||||
new_usec = options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000))
|
||||
|
||||
if new_nsec = options[:nsec]
|
||||
raise ArgumentError, "Can't change both :nsec and :usec at the same time: #{options.inspect}" if options[:usec]
|
||||
new_usec = Rational(new_nsec, 1000)
|
||||
else
|
||||
new_usec = options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000))
|
||||
end
|
||||
|
||||
if utc?
|
||||
::Time.utc(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec)
|
||||
|
|
|
@ -387,6 +387,8 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
|
|||
assert_equal Time.local(2005,1,2,11, 6, 0, 0), Time.local(2005,1,2,11,22,33,44).change(:min => 6)
|
||||
assert_equal Time.local(2005,1,2,11,22, 7, 0), Time.local(2005,1,2,11,22,33,44).change(:sec => 7)
|
||||
assert_equal Time.local(2005,1,2,11,22,33, 8), Time.local(2005,1,2,11,22,33,44).change(:usec => 8)
|
||||
assert_equal Time.local(2005,1,2,11,22,33, 8), Time.local(2005,1,2,11,22,33,2).change(:nsec => 8000)
|
||||
assert_raise(ArgumentError) { Time.local(2005,1,2,11,22,33, 8).change(:usec => 1, :nsec => 1) }
|
||||
end
|
||||
|
||||
def test_utc_change
|
||||
|
@ -396,6 +398,7 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
|
|||
assert_equal Time.utc(2005,2,22,16), Time.utc(2005,2,22,15,15,10).change(:hour => 16)
|
||||
assert_equal Time.utc(2005,2,22,16,45), Time.utc(2005,2,22,15,15,10).change(:hour => 16, :min => 45)
|
||||
assert_equal Time.utc(2005,2,22,15,45), Time.utc(2005,2,22,15,15,10).change(:min => 45)
|
||||
assert_equal Time.utc(2005,1,2,11,22,33,8), Time.utc(2005,1,2,11,22,33,2).change(:nsec => 8000)
|
||||
end
|
||||
|
||||
def test_offset_change
|
||||
|
@ -405,7 +408,11 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
|
|||
assert_equal Time.new(2005,2,22,16,0,0,"-08:00"), Time.new(2005,2,22,15,15,10,"-08:00").change(:hour => 16)
|
||||
assert_equal Time.new(2005,2,22,16,45,0,"-08:00"), Time.new(2005,2,22,15,15,10,"-08:00").change(:hour => 16, :min => 45)
|
||||
assert_equal Time.new(2005,2,22,15,45,0,"-08:00"), Time.new(2005,2,22,15,15,10,"-08:00").change(:min => 45)
|
||||
assert_equal Time.new(2005,2,22,15,15,10,"-08:00"), Time.new(2005,2,22,15,15,0,"-08:00").change(:sec => 10)
|
||||
assert_equal 10, Time.new(2005,2,22,15,15,0,"-08:00").change(:usec => 10).usec
|
||||
assert_equal 10, Time.new(2005,2,22,15,15,0,"-08:00").change(:nsec => 10).nsec
|
||||
assert_raise(ArgumentError) { Time.new(2005, 2, 22, 15, 15, 45, "-08:00").change(:usec => 1000000) }
|
||||
assert_raise(ArgumentError) { Time.new(2005, 2, 22, 15, 15, 45, "-08:00").change(:nsec => 1000000000) }
|
||||
end
|
||||
|
||||
def test_advance
|
||||
|
|
Loading…
Reference in New Issue